Skip to content

Commit 5096a2b

Browse files
committed
Updated plumbum example
1 parent 0d7d533 commit 5096a2b

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

cmd2/ansi.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
Support for ANSI escape sequences which are used for things like applying style to text,
44
setting the window title, and asynchronous alerts.
55
"""
6-
from enum import Enum, unique
76
import functools
87
import re
8+
from enum import Enum
99
from typing import Any, IO, List, Union
1010

1111
import colorama
@@ -29,16 +29,18 @@
2929

3030
class ColorBase(Enum):
3131
"""
32-
Base class for fg and bg classes
33-
This expects the child classes to define enums of: color name -> ANSI color sequence
32+
Base class used for defining color enums. See fg and bg classes for examples.
33+
This expects the child classes to define enums of the follow structure
34+
key: color name (e.g. black)
35+
value: anything that when cast to a string returns an ANSI sequence
3436
"""
3537
def __str__(self) -> str:
3638
"""
3739
Return ANSI color sequence instead of enum name
3840
This is helpful when using a ColorBase in an f-string or format() call
3941
e.g. my_str = "{}hello{}".format(fg.blue, fg.reset)
4042
"""
41-
return self.value
43+
return str(self.value)
4244

4345
def __add__(self, other: Any) -> str:
4446
"""
@@ -57,12 +59,12 @@ def __radd__(self, other: Any) -> str:
5759
@classmethod
5860
def colors(cls) -> List[str]:
5961
"""Return a list of color names."""
60-
return [color.name for color in cls]
62+
# Use __members__ to ensure we get all key names, including those which are aliased
63+
return [color for color in cls.__members__]
6164

6265

6366
# Foreground colors
64-
# noinspection PyPep8Naming,DuplicatedCode
65-
@unique
67+
# noinspection PyPep8Naming
6668
class fg(ColorBase):
6769
"""Enum class for foreground colors"""
6870
black = Fore.BLACK
@@ -85,8 +87,7 @@ class fg(ColorBase):
8587

8688

8789
# Background colors
88-
# noinspection PyPep8Naming,DuplicatedCode
89-
@unique
90+
# noinspection PyPep8Naming
9091
class bg(ColorBase):
9192
"""Enum class for background colors"""
9293
black = Back.BLACK

examples/plumbum_colors.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,37 @@
3131
from cmd2 import ansi
3232
from plumbum.colors import fg, bg
3333

34-
FG_COLORS = {
35-
'black': fg.Black,
36-
'red': fg.DarkRedA,
37-
'green': fg.MediumSpringGreen,
38-
'yellow': fg.LightYellow,
39-
'blue': fg.RoyalBlue1,
40-
'magenta': fg.Purple,
41-
'cyan': fg.SkyBlue1,
42-
'white': fg.White,
43-
'purple': fg.Purple,
44-
}
45-
46-
BG_COLORS = {
47-
'black': bg.BLACK,
48-
'red': bg.DarkRedA,
49-
'green': bg.MediumSpringGreen,
50-
'yellow': bg.LightYellow,
51-
'blue': bg.RoyalBlue1,
52-
'magenta': bg.Purple,
53-
'cyan': bg.SkyBlue1,
54-
'white': bg.White,
55-
}
56-
57-
58-
def get_fg(fg):
59-
return str(FG_COLORS[fg])
60-
61-
62-
def get_bg(bg):
63-
return str(BG_COLORS[bg])
34+
35+
class FgColors(ansi.ColorBase):
36+
black = fg.Black
37+
red = fg.DarkRedA
38+
green = fg.MediumSpringGreen
39+
yellow = fg.LightYellow
40+
blue = fg.RoyalBlue1
41+
magenta = fg.Purple
42+
cyan = fg.SkyBlue1
43+
white = fg.White
44+
purple = fg.Purple
45+
46+
47+
class BgColors(ansi.ColorBase):
48+
black = bg.BLACK
49+
red = bg.DarkRedA
50+
green = bg.MediumSpringGreen
51+
yellow = bg.LightYellow
52+
blue = bg.RoyalBlue1
53+
magenta = bg.Purple
54+
cyan = bg.SkyBlue1
55+
white = bg.White
56+
purple = bg.Purple
57+
58+
59+
def get_fg(name: str):
60+
return str(FgColors[name])
61+
62+
63+
def get_bg(name: str):
64+
return str(BgColors[name])
6465

6566

6667
ansi.fg_lookup = get_fg
@@ -84,8 +85,8 @@ def __init__(self):
8485
speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
8586
speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
8687
speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
87-
speak_parser.add_argument('-f', '--fg', choices=FG_COLORS, help='foreground color to apply to output')
88-
speak_parser.add_argument('-b', '--bg', choices=BG_COLORS, help='background color to apply to output')
88+
speak_parser.add_argument('-f', '--fg', choices=FgColors.colors(), help='foreground color to apply to output')
89+
speak_parser.add_argument('-b', '--bg', choices=BgColors.colors(), help='background color to apply to output')
8990
speak_parser.add_argument('-l', '--bold', action='store_true', help='bold the output')
9091
speak_parser.add_argument('-u', '--underline', action='store_true', help='underline the output')
9192
speak_parser.add_argument('words', nargs='+', help='words to say')

0 commit comments

Comments
 (0)