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