Skip to content

Commit d02b850

Browse files
committed
Refactor some codes by adding utils.to_tuple function.
1 parent ef404f1 commit d02b850

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

visualdialog/box.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import List, Literal, Sequence, Tuple, Union
1010

1111
from .type import CursesKey, CursesTextAttribute, CursesTextAttributes, CursesWindow
12-
from .utils import TextAttr
12+
from .utils import TextAttr, to_tuple
1313

1414

1515
class PanicError(Exception):
@@ -101,12 +101,8 @@ def __init__(
101101
self.title = title
102102
if title:
103103
self.title_colors = curses.color_pair(title_colors_pair_nb)
104-
105-
# Test if only one argument is passed instead of a tuple
106-
if isinstance(title_text_attr, int):
107-
self.title_text_attr = (title_text_attr, )
108-
else:
109-
self.title_text_attr = title_text_attr
104+
# Test if only one argument is passed instead of a sequence.
105+
self.title_text_attr = to_tuple(title_text_attr)
110106

111107
self.downtime_chars = downtime_chars
112108
self.downtime_chars_delay = downtime_chars_delay

visualdialog/dialog.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from .box import BaseTextBox
1212
from .type import CursesTextAttribute, CursesTextAttributes, CursesWindow
13-
from .utils import TextAttr, chunked
13+
from .utils import TextAttr, chunked, to_tuple
1414

1515

1616
class DialogBox(BaseTextBox):
@@ -176,10 +176,7 @@ def _one_by_one(self,
176176
offsetting_x = 0
177177
for word in line.split(word_delimiter):
178178
if word in words_attr:
179-
attr = words_attr[word]
180-
181-
if isinstance(attr, int):
182-
attr = (attr, )
179+
attr = to_tuple(words_attr[word])
183180
else:
184181
attr = (colors_pair, *text_attr)
185182

visualdialog/utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
__all__ = ["TextAttr"]
55

66
from contextlib import ContextDecorator
7-
from typing import Iterable, NoReturn, Sequence
7+
from typing import Iterable, NoReturn, Sequence, Tuple, Union
88

99
from .type import CursesTextAttribute, CursesWindow
1010

@@ -21,6 +21,16 @@ def chunked(seq: Sequence,
2121
for chunk in range(0, len(seq), chunk_length))
2222

2323

24+
def to_tuple(obj: Union[object, Sequence]) -> Union[Tuple, Sequence]:
25+
"""Check if the given object is a sequence, if so returns it,
26+
otherwise returns it as a tuple.
27+
"""
28+
if isinstance(obj, Sequence):
29+
return obj
30+
else:
31+
return (obj, )
32+
33+
2434
class TextAttr(ContextDecorator):
2535
"""A context manager to manage ``curses`` text attributes.
2636

0 commit comments

Comments
 (0)