summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-12-06 12:34:25 -0500
committerGitHub <noreply@github.com>2019-12-06 12:34:25 -0500
commit2b42671d462d6a538426f1f16a29c390c953b7e7 (patch)
treec505687765174b7899769eb116f36dc4a11728dd
parent367f9f156d58f434a57345515306b6d14e9f4e80 (diff)
parent8862e278cf30b9bb09e19c5e13024cf6f6d70825 (diff)
downloadcmd2-git-2b42671d462d6a538426f1f16a29c390c953b7e7.tar.gz
Merge pull request #829 from python-cmd2/line_buffering
Enabled line buffering when redirecting output to a file
-rw-r--r--CHANGELOG.md5
-rw-r--r--cmd2/cmd2.py15
2 files changed, 11 insertions, 9 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0c423836..d773f784 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,9 +1,8 @@
## 0.9.22 (TBD, 2019)
* Bug Fixes
* Fixed bug where a redefined `ansi.style_error` was not being used in all `cmd2` files
-* Other
- * Removed `bold=True` from `ansi.style_success` because it was difficult for red-greed colorblind users to
- distinguish that color from the `ansi.style_warning` color in certain terminals.
+* Enhancements
+ * Enabled line buffering when redirecting output to a file
## 0.9.21 (November 26, 2019)
* Bug Fixes
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 753fb5ab..bd581919 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -1874,22 +1874,25 @@ class Cmd(cmd.Cmd):
self.perror("Cannot redirect to paste buffer; missing 'pyperclip' and/or pyperclip dependencies")
redir_error = True
+ # Redirecting to a file
elif statement.output_to:
- # going to a file
- mode = 'w'
- # statement.output can only contain
- # REDIRECTION_APPEND or REDIRECTION_OUTPUT
+ # statement.output can only contain REDIRECTION_APPEND or REDIRECTION_OUTPUT
if statement.output == constants.REDIRECTION_APPEND:
mode = 'a'
+ else:
+ mode = 'w'
+
try:
- new_stdout = open(utils.strip_quotes(statement.output_to), mode)
+ # Use line buffering
+ new_stdout = open(utils.strip_quotes(statement.output_to), mode=mode, buffering=1)
saved_state.redirecting = True
sys.stdout = self.stdout = new_stdout
except OSError as ex:
self.pexcept('Failed to redirect because - {}'.format(ex))
redir_error = True
+
+ # Redirecting to a paste buffer
else:
- # going to a paste buffer
new_stdout = tempfile.TemporaryFile(mode="w+")
saved_state.redirecting = True
sys.stdout = self.stdout = new_stdout