diff options
author | kotfu <kotfu@kotfu.net> | 2019-11-29 22:23:35 -0700 |
---|---|---|
committer | kotfu <kotfu@kotfu.net> | 2019-11-29 22:23:35 -0700 |
commit | 69b108a8865ba96fd5959a3f1a8237c5e8f18732 (patch) | |
tree | fd8f8cdf7b1cc1a7e6859a5de22cc26e42db8833 /docs/features/generating_output.rst | |
parent | de9e778481069b6d23d5b8ed639b328172633552 (diff) | |
download | cmd2-git-69b108a8865ba96fd5959a3f1a8237c5e8f18732.tar.gz |
Finish generating_output and document output related settings
Diffstat (limited to 'docs/features/generating_output.rst')
-rw-r--r-- | docs/features/generating_output.rst | 76 |
1 files changed, 46 insertions, 30 deletions
diff --git a/docs/features/generating_output.rst b/docs/features/generating_output.rst index 58e53962..34416092 100644 --- a/docs/features/generating_output.rst +++ b/docs/features/generating_output.rst @@ -22,19 +22,6 @@ also adds a number of output related methods to ``Cmd2.Cmd`` which you may use to enhance the output your application produces. -TODO: - - -- perror -- pwarning -- pexcept -- ppaging - -- column formatting? -- wcswidth? - -- exceptions - Ordinary Output --------------- @@ -91,22 +78,27 @@ want it. Output generated by any of these methods will honor the :ref:`features/settings:allow_ansi` setting: -- :meth:`cmd2.cmd2.Cmd.poutput` -- :meth:`cmd2.cmd2.Cmd.perror` -- :meth:`cmd2.cmd2.Cmd.pwarning` -- :meth:`cmd2.cmd2.Cmd.pexcept` -- :meth:`cmd2.cmd2.Cmd.pfeedback` -- :meth:`cmd2.cmd2.Cmd.ppaged` +- :meth:`~.cmd2.Cmd.poutput` +- :meth:`~.cmd2.Cmd.perror` +- :meth:`~.cmd2.Cmd.pwarning` +- :meth:`~.cmd2.Cmd.pexcept` +- :meth:`~.cmd2.Cmd.pfeedback` +- :meth:`~.cmd2.Cmd.ppaged` Error Messages -------------- +When an error occurs in your program, you can display it on ``sys.stderr`` by +calling the :meth:`~.cmd2.Cmd.perror` method. Warning Messages ---------------- +:meth:`~.cmd2.Cmd.pwarning` is just like :meth:`~.cmd2.Cmd.perror` but applies +:meth:`cmd2.ansi.style_warning` to the output. + Feedback -------- @@ -116,33 +108,57 @@ to be part of the generated output. This could be debugging information or status information about the progress of long running commands. It's not output, it's not error messages, it's feedback. If you use the :ref:`features/settings:Timing` setting, the output of how long it took the -command to run will be output as feedback. ``cmd2`` has a ``self.pfeedback()`` -method to produce this type of output, and several -:ref:`features/settings:Settings` to control how this output is handled. +command to run will be output as feedback. You can use the +:meth:`~.cmd2.Cmd.pfeedback` method to produce this type of output, and +several :ref:`features/settings:Settings` control how it is handled. -If the ``quiet`` setting is ``True``, then calling ``self.pfeedback()`` -produces no output. If ``quiet`` is ``False``, then the ``feedback_to_output`` -setting is consulted to determine which file descriptor the feedback will be -sent to. The default value of ``False`` means all feedback is sent to -``sys.stderr``. If set to ``True``, then the feedback output will be sent to -``self.stdout`` along with the rest of the generated output. +If the :ref:`features/settings:quiet` setting is ``True``, then calling +:meth:`~.cmd2.Cmd.pfeedback` produces no output. If +:ref:`features/settings:quiet` is ``False``, the +:ref:`features/settings:feedback_to_output` setting is consulted to determine +whether to send the output to ``stdout`` or ``stderr``. Exceptions ---------- +If your app catches an exception and you would like to display the exception to +the user, the :meth:`~.cmd2.Cmd.pexcept` method can help. The default behavior +is to just display the message contained within the exception. However, if the +:ref:`features/settings:debug` setting is ``True``, then the entire stack trace +will be displayed. + Paging Output ------------- +If you know you are going to generate a lot of output, you may want to display +it in a way that the user can scroll forwards and backwards through it. If you +pass all of the output to be displayed in a single call to +:meth:`~.cmd2.Cmd.ppaged`, it will be piped to an operating system appropriate +shell command to page the output. On Windows, the output is piped to ``more``; +on Unix-like operating systems like MacOS and Linux, it is piped to ``less``. + Centering Text -------------- -utils.center_text() +If you would like to generate output which is centered in the user's terminal, +the :meth:`cmd2.utils.center_text` method can help. Pass it a string and it +will figure out the width of the terminal and return you a new string, +appropriately padded so it will be centered. Columnar Output --------------- -Using wcswidth() and ansi.ansi_safe_wcswidth() +When generating output in multiple columns, you often need to calculate the +width of each item so you can pad it appropriately with spaces. However, there +are categories of Unicode characters that occupy 2 cells, and other that occupy +0. To further complicate matters, you might have included ANSI escape sequences +in the output to generate colors on the terminal. + +The :meth:`cmd2.ansi.ansi_safe_wcswidth` function solves both of these +problems. Pass it a string, and regardless of which Unicode characters and ANSI +escape sequences it contains, it will tell you how many characters on the +screen that string will consume when printed. |