diff options
author | Amjith Ramanujam <amjith.r@gmail.com> | 2023-10-03 10:03:03 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-03 10:03:03 -0700 |
commit | f4246296016943b3533f00679efec7a4d4bfa075 (patch) | |
tree | e1a47a443a4568cb635aae451a1f6c47c3986fc8 | |
parent | a9f4b27e8d8bef03a77bbf7cddbd6deb0a7e9531 (diff) | |
parent | f5bf97a82fc9b60acedb894281329fcfbeeb9f25 (diff) |
Merge pull request #168 from mjpieters/fix_once
Open once_file once per use
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | litecli/packages/special/iocommands.py | 37 |
2 files changed, 22 insertions, 18 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bd0f99..81d0ba2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,9 +12,12 @@ failing queries get `successful = False` in `query_history`. * Changed `master` to `main` in CONTRIBUTING.md to reflect GitHubs new default branch naming. +* Fixed `.once -o <file>` by opening the output file once per statement instead + of for every line of output ([#148](https://github.com/dbcli/litecli/issues/148)). * Use the sqlite3 API to cancel a running query on interrupt ([#164](https://github.com/dbcli/litecli/issues/164)). + ## 1.9.0 - 2022-06-06 ### Features diff --git a/litecli/packages/special/iocommands.py b/litecli/packages/special/iocommands.py index 1f77885..ee254f0 100644 --- a/litecli/packages/special/iocommands.py +++ b/litecli/packages/special/iocommands.py @@ -21,7 +21,7 @@ from litecli.packages.prompt_utils import confirm_destructive_query use_expanded_output = False PAGER_ENABLED = True tee_file = None -once_file = written_to_once_file = None +once_file = once_file_args = written_to_once_file = None favoritequeries = FavoriteQueries(ConfigObj()) @@ -377,9 +377,9 @@ def write_tee(output): aliases=("\\o", "\\once"), ) def set_once(arg, **_): - global once_file + global once_file_args - once_file = parseargfile(arg) + once_file_args = parseargfile(arg) return [(None, None, None, "")] @@ -387,27 +387,28 @@ def set_once(arg, **_): @export def write_once(output): global once_file, written_to_once_file - if output and once_file: - try: - f = open(**once_file) - except (IOError, OSError) as e: - once_file = None - raise OSError( - "Cannot write to file '{}': {}".format(e.filename, e.strerror) - ) - - with f: - click.echo(output, file=f, nl=False) - click.echo("\n", file=f, nl=False) + if output and once_file_args: + if once_file is None: + try: + once_file = open(**once_file_args) + except (IOError, OSError) as e: + once_file = None + raise OSError( + "Cannot write to file '{}': {}".format(e.filename, e.strerror) + ) + + click.echo(output, file=once_file, nl=False) + click.echo("\n", file=once_file, nl=False) written_to_once_file = True @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 + global once_file, once_file_args, written_to_once_file + if once_file and written_to_once_file: + once_file.close() + once_file = once_file_args = written_to_once_file = None @special_command( |