Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Add simple __getitem__ implementation to fix error in #8 and accompanying test #141

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clint/textui/colored.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ def __radd__(self, other):
def __mul__(self, other):
return (self.color_str * other)

def __getitem__(self, key):
left, right = self.color_str.split(self.s)
return "%s%s%s" % (left, self.s[key], right)

def _new(self, s):
return ColoredString(self.color, s)

Expand Down
17 changes: 12 additions & 5 deletions test_clint.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ def tearDown(self):
pass

class ColoredStringTestCase(unittest.TestCase):

def setUp(self):
from clint.textui.colored import ColoredString

def tearDown(self):
pass

def test_split(self):
from clint.textui.colored import ColoredString
new_str = ColoredString('red', "hello world")
output = new_str.split()
assert output[0].s == "hello"

def test_find(self):
from clint.textui.colored import ColoredString
new_str = ColoredString('blue', "hello world")
output = new_str.find('h')
self.assertEqual(output, 0)

def test_replace(self):
from clint.textui.colored import ColoredString
new_str = ColoredString('green', "hello world")
Expand All @@ -57,6 +57,13 @@ def test_clint_force_color_env_var(self):
new_str = ColoredString('RED', 'hello world')
assert new_str.always_color == True

def test_color_slice(self):
from clint.textui.colored import ColoredString
new_str = ColoredString('GREEN', 'hello world')
hello = new_str[:6]
world = new_str[-6:]
assert "world" not in hello
assert "hello" not in world

if __name__ == '__main__':
unittest.main()