Skip to content

Commit ff22f0b

Browse files
committed
code refactoring
Fixes #59
1 parent 3111d0d commit ff22f0b

File tree

1 file changed

+45
-74
lines changed

1 file changed

+45
-74
lines changed

eutmux.py

Lines changed: 45 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import os
44

55
import yaml
6-
from yaml import Loader
76

87
from utils import get_tmux_option, run_shell_command
98

@@ -66,28 +65,18 @@ def __init__(self, theme_config):
6665
"""Constructor."""
6766
status_line = theme_config.get("status_line")
6867
status_left = theme_config.get("status_left")
69-
self.fg_format = get(
70-
status_left, "fg_format", status_line.get("foreground")
71-
)
72-
self.bg_format = get(
73-
status_left, "bg_format", status_line.get("background")
74-
)
75-
self.fg_icon = get(
76-
status_left, "fg_icon", status_line.get("foreground")
77-
)
78-
self.bg_icon = get(
79-
status_left, "bg_icon", status_line.get("background")
80-
)
68+
self.fg_format = get(status_left, "fg_format", status_line.get("foreground"))
69+
self.bg_format = get(status_left, "bg_format", status_line.get("background"))
70+
self.fg_icon = get(status_left, "fg_icon", status_line.get("foreground"))
71+
self.bg_icon = get(status_left, "bg_icon", status_line.get("background"))
8172
self.fg_decorator = status_left.get(
8273
"fg_decorator", status_line.get("foreground")
8374
)
8475
self.bg_decorator = get(
8576
status_left, "bg_decorator", status_line.get("background")
8677
)
8778
self.icon = get(status_left, "icon", status_line.get("left_icon"))
88-
self.decorator = status_left.get(
89-
"decorator", status_line.get("left_decorator")
90-
)
79+
self.decorator = status_left.get("decorator", status_line.get("left_decorator"))
9180
self.style = get(status_left, "style", status_line.get("style"))
9281
super().__init__(
9382
fg_format=self.fg_format,
@@ -182,18 +171,10 @@ def __init__(self, theme_config):
182171
"""Constructor."""
183172
status_line = theme_config.get("status_line")
184173
status_right = theme_config.get("status_right")
185-
self.fg_format = get(
186-
status_right, "fg_format", status_line.get("foreground")
187-
)
188-
self.bg_format = get(
189-
status_right, "bg_format", status_line.get("background")
190-
)
191-
self.fg_icon = get(
192-
status_right, "fg_icon", status_line.get("foreground")
193-
)
194-
self.bg_icon = get(
195-
status_right, "bg_icon", status_line.get("background")
196-
)
174+
self.fg_format = get(status_right, "fg_format", status_line.get("foreground"))
175+
self.bg_format = get(status_right, "bg_format", status_line.get("background"))
176+
self.fg_icon = get(status_right, "fg_icon", status_line.get("foreground"))
177+
self.bg_icon = get(status_right, "bg_icon", status_line.get("background"))
197178
self.fg_decorator = get(
198179
status_right, "fg_decorator", status_line.get("foreground")
199180
)
@@ -247,9 +228,7 @@ def produce_general_options_commands(self):
247228
foreground = component.get("fg", self.terminal.get("foreground"))
248229
background = component.get("bg", self.terminal.get("background"))
249230
style = component.get("style", self.status_line.get("style"))
250-
style_command = self.get_style_command(
251-
foreground, background, style, name
252-
)
231+
style_command = self.get_style_command(foreground, background, style, name)
253232
if style_command is not None:
254233
general.append(style_command)
255234

@@ -280,12 +259,8 @@ def produce_status_left(self):
280259
bg_format = component.get(
281260
"bg_format", self.theme.status_left.get("bg_format")
282261
)
283-
fg_icon = component.get(
284-
"fg_icon", self.theme.status_left.get("fg_icon")
285-
)
286-
bg_icon = component.get(
287-
"bg_icon", self.theme.status_left.get("bg_icon")
288-
)
262+
fg_icon = component.get("fg_icon", self.theme.status_left.get("fg_icon"))
263+
bg_icon = component.get("bg_icon", self.theme.status_left.get("bg_icon"))
289264
fg_decorator = component.get(
290265
"fg_decorator", self.theme.status_left.get("fg_decorator")
291266
)
@@ -294,10 +269,10 @@ def produce_status_left(self):
294269
)
295270
style = component.get("style", self.theme.status_left.get("style"))
296271
_format = component.get("format", EMPTY)
297-
format_style = f"{self.get_style_for_option(fg_format, bg_format, style, _format)}"
298-
icon_style = (
299-
f"{self.get_style_for_option(fg_icon, bg_icon, style, icon)}"
272+
format_style = (
273+
f"{self.get_style_for_option(fg_format, bg_format, style, _format)}"
300274
)
275+
icon_style = f"{self.get_style_for_option(fg_icon, bg_icon, style, icon)}"
301276
decorator_style = f"{self.get_style_for_option(fg_decorator, bg_decorator, style, decorator)}"
302277
component_value = f"{icon_style}{decorator_style}{format_style}"
303278
status_left.append(component_value)
@@ -308,12 +283,8 @@ def produce_window(self):
308283
"""Return tuple with active window and inactive window option strings."""
309284
windows = {}
310285
for name, component in self.window.items():
311-
style = component.get(
312-
"style", self.theme.window.get(name).get("style")
313-
)
314-
icon = component.get(
315-
"icon", self.theme.window.get(name).get("icon")
316-
)
286+
style = component.get("style", self.theme.window.get(name).get("style"))
287+
icon = component.get("icon", self.theme.window.get(name).get("icon"))
317288
window_name = component.get(
318289
"window_name", self.theme.window.get(name).get("window_name")
319290
)
@@ -358,13 +329,13 @@ def produce_window(self):
358329
window_index_style = self.get_style_for_option(
359330
fg_window_index, bg_window_index, style, window_index
360331
)
361-
icon_style = self.get_style_for_option(
362-
fg_icon, bg_icon, style, icon
363-
)
332+
icon_style = self.get_style_for_option(fg_icon, bg_icon, style, icon)
364333
decorator_style = self.get_style_for_option(
365334
fg_decorator, bg_decorator, style, decorator
366335
)
367-
component_value = f"{window_style}{window_index_style}{icon_style}{decorator_style} "
336+
component_value = (
337+
f"{window_style}{window_index_style}{icon_style}{decorator_style} "
338+
)
368339
windows[name] = component_value
369340

370341
return windows
@@ -386,12 +357,8 @@ def produce_status_right(self):
386357
bg_format = options.get(
387358
"bg_format", self.theme.status_right.get("bg_format")
388359
)
389-
fg_icon = options.get(
390-
"fg_icon", self.theme.status_right.get("fg_icon")
391-
)
392-
bg_icon = options.get(
393-
"bg_icon", self.theme.status_right.get("bg_icon")
394-
)
360+
fg_icon = options.get("fg_icon", self.theme.status_right.get("fg_icon"))
361+
bg_icon = options.get("bg_icon", self.theme.status_right.get("bg_icon"))
395362
fg_decorator = options.get(
396363
"fg_decorator", self.theme.status_right.get("fg_decorator")
397364
)
@@ -400,10 +367,10 @@ def produce_status_right(self):
400367
)
401368
style = options.get("style", self.theme.status_right.get("style"))
402369
_format = options.get("format", EMPTY)
403-
format_style = f"{self.get_style_for_option(fg_format, bg_format, style, _format)}"
404-
icon_style = (
405-
f"{self.get_style_for_option(fg_icon, bg_icon, style, icon)}"
370+
format_style = (
371+
f"{self.get_style_for_option(fg_format, bg_format, style, _format)}"
406372
)
373+
icon_style = f"{self.get_style_for_option(fg_icon, bg_icon, style, icon)}"
407374
decorator_style = f"{self.get_style_for_option(fg_decorator, bg_decorator, style, decorator)}"
408375
component_value = f"{decorator_style}{icon_style}{format_style}"
409376
status_right.append(component_value)
@@ -432,6 +399,20 @@ def get_style_for_option(self, foreground, background, style, option):
432399
return f"{_style}{_default_style}"
433400

434401
def get_style_command(self, foreground, background, style, style_name):
402+
"""Return tmux set style option command.
403+
404+
Parameters:
405+
foreground: The foreground color.
406+
background: The background color.
407+
style: The style.
408+
style_name: The name of the style.
409+
e.g. status-style, window-status-current-format, window-status-format
410+
411+
Return:
412+
The tmux command string to set the style.
413+
Example:
414+
set-option -gq status-style 'fg=green,bg=black,italics'
415+
"""
435416
style_content = None
436417
if foreground and foreground.strip() != EMPTY:
437418
style_content = f"fg={foreground},"
@@ -454,21 +435,15 @@ def produce_option_commands(self):
454435
window = self.produce_window()
455436
status_right = self.produce_status_right()
456437

457-
status_line_cmd = self.produce_option_command(
458-
"status-style", status_line
459-
)
460-
status_left_cmd = self.produce_option_command(
461-
"status-left", status_left
462-
)
438+
status_line_cmd = self.produce_option_command("status-style", status_line)
439+
status_left_cmd = self.produce_option_command("status-left", status_left)
463440
active_window_cmd = self.produce_option_command(
464441
"window-status-current-format", window["active"]
465442
)
466443
inactive_window_cmd = self.produce_option_command(
467444
"window-status-format", window["inactive"]
468445
)
469-
status_right_cmd = self.produce_option_command(
470-
"status-right", status_right
471-
)
446+
status_right_cmd = self.produce_option_command("status-right", status_right)
472447
general_commands = self.produce_general_options_commands()
473448
option_commands = []
474449
option_commands.append(general_commands)
@@ -484,9 +459,7 @@ def init(config_file="eutmux.yaml"):
484459
"""Load config file, overwrite options by value from tmux.conf."""
485460

486461
# user can set customized config file under EUTMUX_CONFIG_HOME
487-
xdg_config_home = os.getenv(
488-
"XDG_CONFIG_HOME", f'{os.getenv("HOME")}/.config'
489-
)
462+
xdg_config_home = os.getenv("XDG_CONFIG_HOME", f'{os.getenv("HOME")}/.config')
490463
eutmux_config_home = f"{xdg_config_home}/eutmux"
491464
_config_file = f"{eutmux_config_home}/{config_file}"
492465
if os.path.exists(_config_file):
@@ -507,9 +480,7 @@ def init(config_file="eutmux.yaml"):
507480
theme_name = eutmux.get("theme", "eutmux")
508481

509482
# if dynamic theme is set, then use dynamic theme.
510-
dynamic_theme_name = get_tmux_option(
511-
"@eutmux_dynamic_theme_name", theme_name
512-
)
483+
dynamic_theme_name = get_tmux_option("@eutmux_dynamic_theme_name", theme_name)
513484
theme_filename = f"{dynamic_theme_name}.theme.yaml"
514485

515486
# if dynamic theme file doesn't exist under project, then check if it

0 commit comments

Comments
 (0)