dgelessus / pythonista_startup

Bits and pieces from my pythonista_startup folder.
MIT License
24 stars 3 forks source link

idea: restore command history #1

Open jsbain opened 8 years ago

jsbain commented 8 years ago
[str(h) for h in UIApplication.sharedApplication().keyWindow().rootViewController().accessoryViewController().consoleViewController().history()]

exposes the current console command history as a list of strings. This can also be set via

UIApplication.sharedApplication().keyWindow().rootViewController().accessoryViewController().consoleViewController().history=['test','ing']

is it possible to enable a stdin hook which stores history to a file, which is then restored on reload? or perhaps an atexit which reads and stores current history.

dgelessus commented 8 years ago
  1. I don't know anything about Objective-C, Swift or iOS app development, if this involves more than getting/setting the history property (?) then I may be the wrong person for the job :)
  2. I'm quite certain that interactive Python input does not go through normal stdin in any way, so I'm not sure what a stdin hook would be. (And would it be necessary if the history list is accessible via objc_util?) Of course "commands" can be implemented by saying builtins.save_history = object() and making the displayhook react specially to that, but then you might as well make it a normal function.
  3. Last I checked Pythonista wouldn't run atexits, I'll need to check that though. (Edit: still doesn't, tested with atexit.register(lambda: open(os.path.expanduser("~/Documents/atexit_test.txt"), "wb").close()).)
jsbain commented 8 years ago

I thought in a recent beta that atexit was supported, though I don't think it runs when killed by the os for memory reasons, or when crashing, so maybe has little value.

I thought you had done some work monkey patching the _stdoutcatcher, but maybe my memory is fuzzy. My thinking would be to log to a file each console command as it is run, then restore in startup. I am not sure if there is another "standard" method to hook into console commands? I guess, for example, whenever you append to Out, we could store the new history commands.... Okay, I guess I know where to look now.

dgelessus commented 8 years ago

Ah, atexit hooks run when you do sys.exit(), not when the app exits.

What I did "back then" was related to the autocompletion in the interactive console. Before Pythonista 2 the completion list was built by injecting a bit of Python code that printed the completions to stdout, which was then captured by Pythonista and shown in the completion list. I managed to patch that so it would show a custom suggestion at the top of the list every time, but there was no way to get the full text of the partial line typed at that point (and no way to tell whether stdout was currently in "normal" or "autocomplete list" mode, so any patching may also have affected normal output).