@@ -1194,123 +1194,67 @@ def visible_prompt(self) -> str:
1194
1194
1195
1195
def print_to (
1196
1196
self ,
1197
- dest : Union [ TextIO , IO [str ] ],
1197
+ dest : IO [str ],
1198
1198
msg : Any ,
1199
1199
* ,
1200
1200
end : str = '\n ' ,
1201
1201
style : Optional [Callable [[str ], str ]] = None ,
1202
- paged : bool = False ,
1203
- chop : bool = False ,
1204
1202
) -> None :
1205
- final_msg = style (msg ) if style is not None else msg
1206
- if paged :
1207
- self .ppaged (final_msg , end = end , chop = chop , dest = dest )
1208
- else :
1209
- try :
1210
- ansi .style_aware_write (dest , f'{ final_msg } { end } ' )
1211
- except BrokenPipeError :
1212
- # This occurs if a command's output is being piped to another
1213
- # process and that process closes before the command is
1214
- # finished. If you would like your application to print a
1215
- # warning message, then set the broken_pipe_warning attribute
1216
- # to the message you want printed.
1217
- if self .broken_pipe_warning :
1218
- sys .stderr .write (self .broken_pipe_warning )
1203
+ """
1204
+ Print message to a given file object.
1219
1205
1220
- def poutput (
1221
- self ,
1222
- msg : Any = '' ,
1223
- * ,
1224
- end : str = '\n ' ,
1225
- apply_style : bool = True ,
1226
- paged : bool = False ,
1227
- chop : bool = False ,
1228
- ) -> None :
1206
+ :param dest: the file object being written to
1207
+ :param msg: object to print
1208
+ :param end: string appended after the end of the message, default a newline
1209
+ :param style: optional style function to format msg with (e.g. ansi.style_success)
1210
+ """
1211
+ final_msg = style (msg ) if style is not None else msg
1212
+ try :
1213
+ ansi .style_aware_write (dest , f'{ final_msg } { end } ' )
1214
+ except BrokenPipeError :
1215
+ # This occurs if a command's output is being piped to another
1216
+ # process and that process closes before the command is
1217
+ # finished. If you would like your application to print a
1218
+ # warning message, then set the broken_pipe_warning attribute
1219
+ # to the message you want printed.
1220
+ if self .broken_pipe_warning :
1221
+ sys .stderr .write (self .broken_pipe_warning )
1222
+
1223
+ def poutput (self , msg : Any = '' , * , end : str = '\n ' ) -> None :
1229
1224
"""Print message to self.stdout and appends a newline by default
1230
1225
1231
- Also handles BrokenPipeError exceptions for when a command's output has
1232
- been piped to another process and that process terminates before the
1233
- cmd2 command is finished executing.
1234
-
1235
1226
:param msg: object to print
1236
1227
:param end: string appended after the end of the message, default a newline
1237
- :param apply_style: If True, then ansi.style_output will be applied to the message text. Set to False in cases
1238
- where the message text already has the desired style. Defaults to True.
1239
- :param paged: If True, pass the output through the configured pager.
1240
- :param chop: If paged is True, True to truncate long lines or False to wrap long lines.
1241
1228
"""
1242
- self .print_to (self .stdout , msg , end = end , style = ansi . style_output if apply_style else None , paged = paged , chop = chop )
1229
+ self .print_to (self .stdout , msg , end = end )
1243
1230
1244
- def perror (
1245
- self ,
1246
- msg : Any = '' ,
1247
- * ,
1248
- end : str = '\n ' ,
1249
- apply_style : bool = True ,
1250
- paged : bool = False ,
1251
- chop : bool = False ,
1252
- ) -> None :
1231
+ def perror (self , msg : Any = '' , * , end : str = '\n ' , apply_style : bool = True ) -> None :
1253
1232
"""Print message to sys.stderr
1254
1233
1255
1234
:param msg: object to print
1256
1235
:param end: string appended after the end of the message, default a newline
1257
1236
:param apply_style: If True, then ansi.style_error will be applied to the message text. Set to False in cases
1258
1237
where the message text already has the desired style. Defaults to True.
1259
- :param paged: If True, pass the output through the configured pager.
1260
- :param chop: If paged is True, True to truncate long lines or False to wrap long lines.
1261
1238
"""
1262
- self .print_to (sys .stderr , msg , end = end , style = ansi .style_error if apply_style else None , paged = paged , chop = chop )
1239
+ self .print_to (sys .stderr , msg , end = end , style = ansi .style_error if apply_style else None )
1263
1240
1264
- def psuccess (
1265
- self ,
1266
- msg : Any = '' ,
1267
- * ,
1268
- end : str = '\n ' ,
1269
- paged : bool = False ,
1270
- chop : bool = False ,
1271
- ) -> None :
1272
- """Writes to stdout applying ansi.style_success by default
1241
+ def psuccess (self , msg : Any = '' , * , end : str = '\n ' ) -> None :
1242
+ """Wraps poutput, but applies ansi.style_success by default
1273
1243
1274
1244
:param msg: object to print
1275
1245
:param end: string appended after the end of the message, default a newline
1276
- :param paged: If True, pass the output through the configured pager.
1277
- :param chop: If paged is True, True to truncate long lines or False to wrap long lines.
1278
1246
"""
1279
- self .print_to (self .stdout , msg , end = end , style = ansi .style_success , paged = paged , chop = chop )
1247
+ msg = ansi .style_success (msg )
1248
+ self .poutput (msg , end = end )
1280
1249
1281
- def pwarning (
1282
- self ,
1283
- msg : Any = '' ,
1284
- * ,
1285
- end : str = '\n ' ,
1286
- paged : bool = False ,
1287
- chop : bool = False ,
1288
- ) -> None :
1250
+ def pwarning (self , msg : Any = '' , * , end : str = '\n ' ) -> None :
1289
1251
"""Wraps perror, but applies ansi.style_warning by default
1290
1252
1291
1253
:param msg: object to print
1292
1254
:param end: string appended after the end of the message, default a newline
1293
- :param paged: If True, pass the output through the configured pager.
1294
- :param chop: If paged is True, True to truncate long lines or False to wrap long lines.
1295
- """
1296
- self .print_to (sys .stderr , msg , end = end , style = ansi .style_warning , paged = paged , chop = chop )
1297
-
1298
- def pfailure (
1299
- self ,
1300
- msg : Any = '' ,
1301
- * ,
1302
- end : str = '\n ' ,
1303
- paged : bool = False ,
1304
- chop : bool = False ,
1305
- ) -> None :
1306
- """Writes to stderr applying ansi.style_error by default
1307
-
1308
- :param msg: object to print
1309
- :param end: string appended after the end of the message, default a newline
1310
- :param paged: If True, pass the output through the configured pager.
1311
- :param chop: If paged is True, True to truncate long lines or False to wrap long lines.
1312
1255
"""
1313
- self .print_to (sys .stderr , msg , end = end , style = ansi .style_error , paged = paged , chop = chop )
1256
+ msg = ansi .style_warning (msg )
1257
+ self .perror (msg , end = end , apply_style = False )
1314
1258
1315
1259
def pexcept (self , msg : Any , * , end : str = '\n ' , apply_style : bool = True ) -> None :
1316
1260
"""Print Exception message to sys.stderr. If debug is true, print exception traceback if one exists.
@@ -1339,36 +1283,20 @@ def pexcept(self, msg: Any, *, end: str = '\n', apply_style: bool = True) -> Non
1339
1283
1340
1284
self .perror (final_msg , end = end , apply_style = False )
1341
1285
1342
- def pfeedback (
1343
- self ,
1344
- msg : Any ,
1345
- * ,
1346
- end : str = '\n ' ,
1347
- apply_style : bool = True ,
1348
- paged : bool = False ,
1349
- chop : bool = False ,
1350
- ) -> None :
1286
+ def pfeedback (self , msg : Any , * , end : str = '\n ' ) -> None :
1351
1287
"""For printing nonessential feedback. Can be silenced with `quiet`.
1352
1288
Inclusion in redirected output is controlled by `feedback_to_output`.
1353
1289
1354
1290
:param msg: object to print
1355
1291
:param end: string appended after the end of the message, default a newline
1356
- :param apply_style: If True, then ansi.style_output will be applied to the message text. Set to False in cases
1357
- where the message text already has the desired style. Defaults to True.
1358
- :param paged: If True, pass the output through the configured pager.
1359
- :param chop: If paged is True, True to truncate long lines or False to wrap long lines.
1360
1292
"""
1361
1293
if not self .quiet :
1362
- self .print_to (
1363
- self .stdout if self .feedback_to_output else sys .stderr ,
1364
- msg ,
1365
- end = end ,
1366
- style = ansi .style_output if apply_style else None ,
1367
- paged = paged ,
1368
- chop = chop ,
1369
- )
1294
+ if self .feedback_to_output :
1295
+ self .poutput (msg , end = end )
1296
+ else :
1297
+ self .perror (msg , end = end , apply_style = False )
1370
1298
1371
- def ppaged (self , msg : Any , * , end : str = '\n ' , chop : bool = False , dest : Optional [ Union [ TextIO , IO [ str ]]] = None ) -> None :
1299
+ def ppaged (self , msg : Any , * , end : str = '\n ' , chop : bool = False ) -> None :
1372
1300
"""Print output using a pager if it would go off screen and stdout isn't currently being redirected.
1373
1301
1374
1302
Never uses a pager inside a script (Python or text) or when output is being redirected or piped or when
@@ -1381,17 +1309,14 @@ def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False, dest: Optiona
1381
1309
- chopping is ideal for displaying wide tabular data as is done in utilities like pgcli
1382
1310
False -> causes lines longer than the screen width to wrap to the next line
1383
1311
- wrapping is ideal when you want to keep users from having to use horizontal scrolling
1384
- :param dest: Optionally specify the destination stream to write to. If unspecified, defaults to self.stdout
1385
1312
1386
1313
WARNING: On Windows, the text always wraps regardless of what the chop argument is set to
1387
1314
"""
1388
- dest = self .stdout if dest is None else dest
1389
-
1390
1315
# Attempt to detect if we are not running within a fully functional terminal.
1391
1316
# Don't try to use the pager when being run by a continuous integration system like Jenkins + pexpect.
1392
1317
functional_terminal = False
1393
1318
1394
- if self .stdin .isatty () and dest .isatty ():
1319
+ if self .stdin .isatty () and self . stdout .isatty ():
1395
1320
if sys .platform .startswith ('win' ) or os .environ .get ('TERM' ) is not None :
1396
1321
functional_terminal = True
1397
1322
@@ -1412,7 +1337,7 @@ def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False, dest: Optiona
1412
1337
with self .sigint_protection :
1413
1338
import subprocess
1414
1339
1415
- pipe_proc = subprocess .Popen (pager , shell = True , stdin = subprocess .PIPE , stdout = dest )
1340
+ pipe_proc = subprocess .Popen (pager , shell = True , stdin = subprocess .PIPE , stdout = self . stdout )
1416
1341
pipe_proc .communicate (final_msg .encode ('utf-8' , 'replace' ))
1417
1342
except BrokenPipeError :
1418
1343
# This occurs if a command's output is being piped to another process and that process closes before the
@@ -1421,7 +1346,7 @@ def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False, dest: Optiona
1421
1346
if self .broken_pipe_warning :
1422
1347
sys .stderr .write (self .broken_pipe_warning )
1423
1348
else :
1424
- self .print_to ( dest , msg , end = end , paged = False )
1349
+ self .poutput ( msg , end = end )
1425
1350
1426
1351
# ----- Methods related to tab completion -----
1427
1352
0 commit comments