Skip to content

Commit

Permalink
feat: auto select config option on start
Browse files Browse the repository at this point in the history
  • Loading branch information
kraanzu committed Dec 12, 2023
1 parent 79f7839 commit b034bd1
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions termtyper/ui/widgets/option.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def __init__(self, setting_name: str):
super().__init__()
self.setting_name = setting_name

def on_mount(self):
self.load_current_setting()

@property
def value(self):
raise NotImplementedError
Expand Down Expand Up @@ -46,6 +49,9 @@ def select_prev_option(self):
def save(self):
config_parser.set(self.setting_name, self.value)

def load_current_setting(self):
raise NotImplementedError


class OptionItem(Widget):
DEFAULT_CSS = """
Expand All @@ -66,9 +72,18 @@ def render(self) -> RenderableType:

class Option(BaseOption):
def __init__(self, setting_name: str, options: List[str]):
super().__init__(setting_name)
self.options = [OptionItem(option) for option in options]
self._value = 0
super().__init__(setting_name)

def load_current_setting(self):
setting = config_parser.get(self.setting_name)
if isinstance(setting, bool):
setting = ["off", "on"][setting]

option = [i for i in self.options if i.value == setting][0]
self._value = self.options.index(option)
self.update_highlight()

@property
def value(self):
Expand All @@ -80,15 +95,13 @@ def update_highlight(self):

def _select_next_option(self):
n = len(self.options)
self.options[self._value].remove_class("selected")
self._value = (self._value + 1) % n
self.options[self._value].add_class("selected")
self.update_highlight()

def _select_prev_option(self):
n = len(self.options)
self.options[self._value].remove_class("selected")
self._value = (self._value - 1 + n) % n
self.options[self._value].add_class("selected")
self.update_highlight()

def compose(self) -> ComposeResult:
for option in self.options:
Expand All @@ -104,6 +117,10 @@ def __init__(self, setting_name: str):
def value(self):
return self._value

def load_current_setting(self):
value = config_parser.get(self.setting_name)
self._value = value

def _select_next_option(self):
self._value = min(self._value + 1, 100)
self.refresh()
Expand Down

0 comments on commit b034bd1

Please sign in to comment.