summaryrefslogtreecommitdiff
path: root/cmd2
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-06-26 01:20:48 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-06-26 01:20:48 -0400
commite6f65e607c6ad2350dfce05f3d5080d0ffbe35f1 (patch)
treed777863fc4f5143bb4e53b83ed27b18734945a46 /cmd2
parentdb984de915944d6421e70b2064400fb3c0ec2f9c (diff)
downloadcmd2-git-e6f65e607c6ad2350dfce05f3d5080d0ffbe35f1.tar.gz
Added bold and underline to style()
Diffstat (limited to 'cmd2')
-rw-r--r--cmd2/cmd2.py8
-rw-r--r--cmd2/constants.py10
-rw-r--r--cmd2/utils.py20
3 files changed, 29 insertions, 9 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index f89274f4..75cee55d 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -3792,7 +3792,6 @@ class Cmd(cmd.Cmd):
return
import shutil
- import colorama.ansi as ansi
from colorama import Cursor
# Sanity check that can't fail if self.terminal_lock was acquired before calling this function
@@ -3854,10 +3853,10 @@ class Cmd(cmd.Cmd):
# Clear each line from the bottom up so that the cursor ends up on the first prompt line
total_lines = num_prompt_terminal_lines + num_input_terminal_lines
- terminal_str += (ansi.clear_line() + Cursor.UP(1)) * (total_lines - 1)
+ terminal_str += (colorama.ansi.clear_line() + Cursor.UP(1)) * (total_lines - 1)
# Clear the first prompt line
- terminal_str += ansi.clear_line()
+ terminal_str += colorama.ansi.clear_line()
# Move the cursor to the beginning of the first prompt line and print the alert
terminal_str += '\r' + alert_msg
@@ -3914,8 +3913,7 @@ class Cmd(cmd.Cmd):
# Sanity check that can't fail if self.terminal_lock was acquired before calling this function
if self.terminal_lock.acquire(blocking=False):
try:
- import colorama.ansi as ansi
- sys.stderr.write(ansi.set_title(title))
+ sys.stderr.write(colorama.ansi.set_title(title))
except AttributeError:
# Debugging in Pycharm has issues with setting terminal title
pass
diff --git a/cmd2/constants.py b/cmd2/constants.py
index cfe7eb6d..f0cdf816 100644
--- a/cmd2/constants.py
+++ b/cmd2/constants.py
@@ -4,6 +4,7 @@
import re
+import colorama
from colorama import Fore, Back
# Used for command parsing, output redirection, tab completion and word
@@ -70,4 +71,13 @@ BG_COLORS = {
'reset': Back.RESET,
}
+
+# Underline style built upon Colorama class
+class UnderlineStyle(colorama.ansi.AnsiCodes):
+ ENABLE = 4
+ DISABLE = 24
+
+
+Underline = UnderlineStyle()
+
DEFAULT_SHORTCUTS = {'?': 'help', '!': 'shell', '@': 'run_script', '@@': '_relative_run_script'}
diff --git a/cmd2/utils.py b/cmd2/utils.py
index ae3bb535..a5b7f43f 100644
--- a/cmd2/utils.py
+++ b/cmd2/utils.py
@@ -12,6 +12,7 @@ import threading
import unicodedata
from typing import Any, Iterable, List, Optional, TextIO, Union
+from colorama import Style
from wcwidth import wcswidth
from . import constants
@@ -36,14 +37,15 @@ def ansi_safe_wcswidth(text: str) -> int:
return wcswidth(strip_ansi(text))
-def style(text: Any, *, fg: str = '', bg: str = '') -> str:
+def style(text: Any, *, fg: str = '', bg: str = '', bold: bool = False, underline: bool = False) -> str:
"""
Applies style to text
:param text: Any object compatible with str.format()
- :param fg: Foreground color. Accepts color names like 'red' or 'blue'
- :param bg: Background color. Accepts color names like 'red' or 'blue'
-
+ :param fg: foreground color. Accepts color names like 'red' or 'blue'
+ :param bg: background color. Accepts color names like 'red' or 'blue'
+ :param bold: apply the bold style if True. Defaults to False.
+ :param underline: apply the underline style if True. Defaults to False.
"""
values = []
text = "{}".format(text)
@@ -57,11 +59,21 @@ def style(text: Any, *, fg: str = '', bg: str = '') -> str:
values.append(constants.BG_COLORS[bg.lower()])
except KeyError:
raise ValueError('Color {} does not exist.'.format(bg))
+ if bold:
+ values.append(Style.BRIGHT)
+ if underline:
+ values.append(constants.Underline.ENABLE)
+
values.append(text)
if fg:
values.append(constants.FG_COLORS['reset'])
if bg:
values.append(constants.BG_COLORS['reset'])
+ if bold:
+ values.append(Style.NORMAL)
+ if underline:
+ values.append(constants.Underline.DISABLE)
+
return "".join(values)