Skip to content

Commit 2b42671

Browse files
authored
Merge pull request #829 from python-cmd2/line_buffering
Enabled line buffering when redirecting output to a file
2 parents 367f9f1 + 8862e27 commit 2b42671

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
## 0.9.22 (TBD, 2019)
22
* Bug Fixes
33
* Fixed bug where a redefined `ansi.style_error` was not being used in all `cmd2` files
4-
* Other
5-
* Removed `bold=True` from `ansi.style_success` because it was difficult for red-greed colorblind users to
6-
distinguish that color from the `ansi.style_warning` color in certain terminals.
4+
* Enhancements
5+
* Enabled line buffering when redirecting output to a file
76

87
## 0.9.21 (November 26, 2019)
98
* Bug Fixes

cmd2/cmd2.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,22 +1874,25 @@ def _redirect_output(self, statement: Statement) -> Tuple[bool, utils.Redirectio
18741874
self.perror("Cannot redirect to paste buffer; missing 'pyperclip' and/or pyperclip dependencies")
18751875
redir_error = True
18761876

1877+
# Redirecting to a file
18771878
elif statement.output_to:
1878-
# going to a file
1879-
mode = 'w'
1880-
# statement.output can only contain
1881-
# REDIRECTION_APPEND or REDIRECTION_OUTPUT
1879+
# statement.output can only contain REDIRECTION_APPEND or REDIRECTION_OUTPUT
18821880
if statement.output == constants.REDIRECTION_APPEND:
18831881
mode = 'a'
1882+
else:
1883+
mode = 'w'
1884+
18841885
try:
1885-
new_stdout = open(utils.strip_quotes(statement.output_to), mode)
1886+
# Use line buffering
1887+
new_stdout = open(utils.strip_quotes(statement.output_to), mode=mode, buffering=1)
18861888
saved_state.redirecting = True
18871889
sys.stdout = self.stdout = new_stdout
18881890
except OSError as ex:
18891891
self.pexcept('Failed to redirect because - {}'.format(ex))
18901892
redir_error = True
1893+
1894+
# Redirecting to a paste buffer
18911895
else:
1892-
# going to a paste buffer
18931896
new_stdout = tempfile.TemporaryFile(mode="w+")
18941897
saved_state.redirecting = True
18951898
sys.stdout = self.stdout = new_stdout

0 commit comments

Comments
 (0)