Skip to content

Commit c011e58

Browse files
committed
[stringbuilder] performance: remove std.stdout.isatty call in loop
based on profiling of segments sub-command
1 parent b80aed5 commit c011e58

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

lib/pathins/stringbuilder.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@
2525
bright_cyan_start: Text = ansicolors["BRIGHT_BOLD_CYAN"]
2626
reset: Text = ansicolors["RESET"]
2727

28+
if sys.stdout.isatty():
29+
IS_A_TTY = True
30+
else:
31+
IS_A_TTY = False
32+
2833

2934
def bold_text(text: str, nocolor: bool = False) -> str:
3035
"""
3136
Returns bold text ANSI escape code text string
3237
"""
33-
if not nocolor and sys.stdout.isatty():
38+
if not nocolor and IS_A_TTY:
3439
return f"{bold_start}{text}{reset}"
3540
else:
3641
return text
@@ -40,7 +45,7 @@ def cyan_text(text: str, nocolor: bool = False) -> str:
4045
"""
4146
Returns cyan ANSI escape code colored text string
4247
"""
43-
if not nocolor and sys.stdout.isatty():
48+
if not nocolor and IS_A_TTY:
4449
return f"{cyan_start}{text}{reset}"
4550
else:
4651
return text
@@ -51,7 +56,7 @@ def cyan_bright_text(text: str, nocolor: bool = False) -> str:
5156
Returns cyan ANSI escape code colored text string
5257
with bold weight
5358
"""
54-
if not nocolor and sys.stdout.isatty():
59+
if not nocolor and IS_A_TTY:
5560
return f"{bright_cyan_start}{text}{reset}"
5661
else:
5762
return text
@@ -61,7 +66,7 @@ def green_text(text: str, nocolor: bool = False) -> str:
6166
"""
6267
Returns green ANSI escape code colored text string
6368
"""
64-
if not nocolor and sys.stdout.isatty():
69+
if not nocolor and IS_A_TTY:
6570
return f"{green_start}{text}{reset}"
6671
else:
6772
return text
@@ -71,7 +76,7 @@ def red_text(text: str, nocolor: bool = False) -> str:
7176
"""
7277
Returns red ANSI escape code colored text string
7378
"""
74-
if not nocolor and sys.stdout.isatty():
79+
if not nocolor and IS_A_TTY:
7580
return f"{red_start}{text}{reset}"
7681
else:
7782
return text
@@ -80,7 +85,7 @@ def red_text(text: str, nocolor: bool = False) -> str:
8085
def report_header(header: str, nocolor: bool = False) -> str:
8186
header_len = len(header) + 1
8287
divider_char = "-"
83-
if not nocolor and sys.stdout.isatty():
88+
if not nocolor and IS_A_TTY:
8489
header_string = (
8590
f"{divider_char * header_len}{os.linesep}"
8691
f"{bright_cyan_start}{header}{reset}{os.linesep}"
@@ -97,7 +102,7 @@ def report_header(header: str, nocolor: bool = False) -> str:
97102

98103
def overlap_result(glyphname: str, test_pass: bool, nocolor: bool = False) -> str:
99104
# color
100-
if not nocolor and sys.stdout.isatty():
105+
if not nocolor and IS_A_TTY:
101106
if test_pass:
102107
result_pre = f"[ {red_start}{glyphname}{reset} ]: "
103108
else:
@@ -119,7 +124,7 @@ def direction_result(
119124
components_with_transforms: Sequence[Tuple] = [],
120125
nocolor: bool = False,
121126
) -> str:
122-
if not nocolor and sys.stdout.isatty():
127+
if not nocolor and IS_A_TTY:
123128
if contours == 0:
124129
return f"[ {bright_cyan_start}{glyphname}{reset} ]: no contours"
125130
if direction_clockwise:
@@ -171,7 +176,7 @@ def _transformed_component(components_with_transforms: Sequence[Tuple]) -> str:
171176
def segment_line(
172177
coord1: Coordinate, coord2: Coordinate, distance: float, nocolor: bool
173178
) -> str:
174-
if not nocolor and sys.stdout.isatty():
179+
if not nocolor and IS_A_TTY:
175180
# color coord1 start and end points
176181
if coord1.startpoint:
177182
coordinates1 = f"{green_start}({coord1.x},{coord1.y}){reset}"
@@ -204,7 +209,7 @@ def segment_quadratic_curve(
204209
distance: float,
205210
nocolor: bool,
206211
) -> str:
207-
if not nocolor and sys.stdout.isatty():
212+
if not nocolor and IS_A_TTY:
208213
if coord1.startpoint:
209214
coordinates1 = f"{green_start}({coord1.x},{coord1.y}){reset}"
210215
elif coord1.endpoint:

0 commit comments

Comments
 (0)