dbcli / litecli

CLI for SQLite Databases with auto-completion and syntax highlighting
https://litecli.com
BSD 3-Clause "New" or "Revised" License
2.06k stars 67 forks source link

Can't use .once more than once. #146

Closed mjpieters closed 1 year ago

mjpieters commented 1 year ago

I expect the .once command to work more than once in a session, each time resulting in the next query output to be written to a designated file.

Instead, you can use .once just once in a session. After that you have to restart litecli. It doesn't matter what the arguments are; executing .once a second time in the same session has no effect.

This is caused by the written_to_once_file flag; it is set to True when output is sent to a file, but then never set back to False.

With written_to_once_file set to True, the next time you execute .once, the set_once() command still sets once_file, but immediately afterwards, unset_once_if_written() is executed after the command completes, which sets once_file back to None, so by the time your next output-producing command is run, nothing will be written out to the file.

mjpieters commented 1 year ago

I can confirm that changing unset_once_if_written to:

@export
def unset_once_if_written():
    """Unset the once file, if it has been written to."""
    global once_file, written_to_once_file
    if written_to_once_file:
        once_file = written_to_once_file = None

fixes the issue. Clearing written_to_once_file makes sure that the trigger doesn't fire forever for later commands.