Skip to content

Commit 4ee3746

Browse files
committed
Refactor Colorama code
1 parent 7e3761d commit 4ee3746

18 files changed

+132
-37
lines changed

seleniumbase/behave/behave_sb.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@
111111
from seleniumbase.fixtures import constants
112112
from seleniumbase.fixtures import shared_utils
113113

114+
is_linux = shared_utils.is_linux()
115+
is_windows = shared_utils.is_windows()
114116
sb_config.__base_class = None
115117

116118

@@ -829,10 +831,10 @@ def get_configured_sb(context):
829831
'\n (Your browser choice was: "%s")\n' % sb.browser
830832
)
831833
# The Xvfb virtual display server is for Linux OS Only.
832-
if sb.xvfb and not shared_utils.is_linux():
834+
if sb.xvfb and not is_linux:
833835
sb.xvfb = False
834836
if (
835-
shared_utils.is_linux()
837+
is_linux
836838
and not sb.headed
837839
and not sb.headless
838840
and not sb.headless2
@@ -1020,7 +1022,7 @@ def dashboard_pre_processing():
10201022
filename = None
10211023
feature_name = None
10221024
scenario_name = None
1023-
if shared_utils.is_windows():
1025+
if is_windows:
10241026
output = output.decode("latin1")
10251027
else:
10261028
output = output.decode("utf-8")
@@ -1129,8 +1131,11 @@ def behave_dashboard_prepare():
11291131
stars = "*" * star_len
11301132
c1 = ""
11311133
cr = ""
1132-
if not shared_utils.is_linux():
1133-
colorama.init(autoreset=True)
1134+
if not is_linux:
1135+
if is_windows and hasattr(colorama, "just_fix_windows_console"):
1136+
colorama.just_fix_windows_console()
1137+
else:
1138+
colorama.init(autoreset=True)
11341139
c1 = colorama.Fore.BLUE + colorama.Back.LIGHTCYAN_EX
11351140
cr = colorama.Style.RESET_ALL
11361141
print("Dashboard: %s%s%s\n%s" % (c1, dash_path, cr, stars))
@@ -1144,7 +1149,7 @@ def _perform_behave_unconfigure_():
11441149
if sb_config.shared_driver:
11451150
try:
11461151
if (
1147-
not shared_utils.is_windows()
1152+
not is_windows
11481153
or sb_config.browser == "ie"
11491154
or sb_config.shared_driver.service.process
11501155
):
@@ -1221,7 +1226,7 @@ def do_final_driver_cleanup_as_needed():
12211226
try:
12221227
if hasattr(sb_config, "last_driver") and sb_config.last_driver:
12231228
if (
1224-
not shared_utils.is_windows()
1229+
not is_windows
12251230
or sb_config.browser == "ie"
12261231
or sb_config.last_driver.service.process
12271232
):
@@ -1245,8 +1250,11 @@ def _perform_behave_terminal_summary_():
12451250
equals = "=" * (equals_len + 2)
12461251
c2 = ""
12471252
cr = ""
1248-
if not shared_utils.is_linux():
1249-
colorama.init(autoreset=True)
1253+
if not is_linux:
1254+
if is_windows and hasattr(colorama, "just_fix_windows_console"):
1255+
colorama.just_fix_windows_console()
1256+
else:
1257+
colorama.init(autoreset=True)
12501258
c2 = colorama.Fore.MAGENTA + colorama.Back.LIGHTYELLOW_EX
12511259
cr = colorama.Style.RESET_ALL
12521260
if sb_config.dashboard:

seleniumbase/console_scripts/logo_helper.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
http://www.patorjk.com/software/taag/#p=display&f=Slant&t=SeleniumBase """
44

55
import colorama
6+
import sys
67

78
r"""
89
______ __ _ ____
@@ -14,7 +15,13 @@
1415

1516

1617
def get_seleniumbase_logo():
17-
colorama.init(autoreset=True)
18+
if (
19+
"win32" in sys.platform
20+
and hasattr(colorama, "just_fix_windows_console")
21+
):
22+
colorama.just_fix_windows_console()
23+
else:
24+
colorama.init(autoreset=True)
1825
c1 = colorama.Fore.BLUE + colorama.Back.LIGHTCYAN_EX
1926
c2 = colorama.Fore.BLUE + colorama.Back.LIGHTGREEN_EX
2027
c3 = colorama.Back.CYAN

seleniumbase/console_scripts/run.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@
4141
import time
4242
from seleniumbase.config import settings
4343
from seleniumbase.fixtures import constants
44+
from seleniumbase.fixtures import shared_utils
4445

45-
colorama.init(autoreset=True)
46+
if shared_utils.is_windows() and hasattr(colorama, "just_fix_windows_console"):
47+
colorama.just_fix_windows_console()
48+
else:
49+
colorama.init(autoreset=True)
4650

4751

4852
def show_usage():
@@ -1330,7 +1334,6 @@ def main():
13301334
show_detailed_help()
13311335
else:
13321336
show_usage()
1333-
colorama.init(autoreset=True)
13341337
c5 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX
13351338
c7 = colorama.Fore.BLACK + colorama.Back.MAGENTA
13361339
cr = colorama.Style.RESET_ALL

seleniumbase/console_scripts/sb_behave_gui.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ def set_colors(use_colors):
3838
c6 = ""
3939
cr = ""
4040
if use_colors:
41-
colorama.init(autoreset=True)
41+
if (
42+
"win32" in sys.platform
43+
and hasattr(colorama, "just_fix_windows_console")
44+
):
45+
colorama.just_fix_windows_console()
46+
else:
47+
colorama.init(autoreset=True)
4248
c0 = colorama.Fore.BLUE + colorama.Back.LIGHTCYAN_EX
4349
c1 = colorama.Fore.BLUE + colorama.Back.LIGHTGREEN_EX
4450
c2 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX

seleniumbase/console_scripts/sb_caseplans.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ def set_colors(use_colors):
4242
c5 = ""
4343
cr = ""
4444
if use_colors:
45-
colorama.init(autoreset=True)
45+
if (
46+
"win32" in sys.platform
47+
and hasattr(colorama, "just_fix_windows_console")
48+
):
49+
colorama.just_fix_windows_console()
50+
else:
51+
colorama.init(autoreset=True)
4652
c0 = colorama.Fore.BLUE + colorama.Back.LIGHTCYAN_EX
4753
c1 = colorama.Fore.BLUE + colorama.Back.LIGHTGREEN_EX
4854
c2 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX

seleniumbase/console_scripts/sb_commander.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ def set_colors(use_colors):
4242
c5 = ""
4343
cr = ""
4444
if use_colors:
45-
colorama.init(autoreset=True)
45+
if (
46+
"win32" in sys.platform
47+
and hasattr(colorama, "just_fix_windows_console")
48+
):
49+
colorama.just_fix_windows_console()
50+
else:
51+
colorama.init(autoreset=True)
4652
c0 = colorama.Fore.BLUE + colorama.Back.LIGHTCYAN_EX
4753
c1 = colorama.Fore.BLUE + colorama.Back.LIGHTGREEN_EX
4854
c2 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX

seleniumbase/console_scripts/sb_install.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,10 @@ def main(override=None, intel_for_uc=None, force_uc=None):
293293
use_version = ""
294294
new_file = ""
295295
f_name = ""
296-
colorama.init(autoreset=True)
296+
if IS_WINDOWS and hasattr(colorama, "just_fix_windows_console"):
297+
colorama.just_fix_windows_console()
298+
else:
299+
colorama.init(autoreset=True)
297300
c1 = colorama.Fore.BLUE + colorama.Back.LIGHTCYAN_EX
298301
c2 = colorama.Fore.BLUE + colorama.Back.LIGHTGREEN_EX
299302
c3 = colorama.Fore.BLUE + colorama.Back.LIGHTYELLOW_EX

seleniumbase/console_scripts/sb_mkchart.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ def main():
6262
c7 = ""
6363
cr = ""
6464
if "linux" not in sys.platform:
65-
colorama.init(autoreset=True)
65+
if (
66+
"win32" in sys.platform
67+
and hasattr(colorama, "just_fix_windows_console")
68+
):
69+
colorama.just_fix_windows_console()
70+
else:
71+
colorama.init(autoreset=True)
6672
c1 = colorama.Fore.BLUE + colorama.Back.LIGHTCYAN_EX
6773
c5 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX
6874
c7 = colorama.Fore.BLACK + colorama.Back.MAGENTA

seleniumbase/console_scripts/sb_mkdir.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ def main():
5454
c7 = ""
5555
cr = ""
5656
if "linux" not in sys.platform:
57-
colorama.init(autoreset=True)
57+
if (
58+
"win32" in sys.platform
59+
and hasattr(colorama, "just_fix_windows_console")
60+
):
61+
colorama.just_fix_windows_console()
62+
else:
63+
colorama.init(autoreset=True)
5864
c1 = colorama.Fore.BLUE + colorama.Back.LIGHTCYAN_EX
5965
c5 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX
6066
c7 = colorama.Fore.BLACK + colorama.Back.MAGENTA

seleniumbase/console_scripts/sb_mkfile.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ def main():
9292
c7 = ""
9393
cr = ""
9494
if "linux" not in sys.platform:
95-
colorama.init(autoreset=True)
95+
if (
96+
"win32" in sys.platform
97+
and hasattr(colorama, "just_fix_windows_console")
98+
):
99+
colorama.just_fix_windows_console()
100+
else:
101+
colorama.init(autoreset=True)
96102
c1 = colorama.Fore.BLUE + colorama.Back.LIGHTCYAN_EX
97103
c5 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX
98104
c7 = colorama.Fore.BLACK + colorama.Back.MAGENTA

0 commit comments

Comments
 (0)