Skip to content

Commit

Permalink
Merge pull request #335 from gmelodie/edit-config
Browse files Browse the repository at this point in the history
Add options to edit config file
  • Loading branch information
JaDogg authored Feb 20, 2024
2 parents 24a6936 + f491ef3 commit bf38528
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
11 changes: 10 additions & 1 deletion pydoro/pydoro_core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
import os
import ast

from pydoro.pydoro_core.util import in_app_path
from pydoro.pydoro_core.util import in_app_path, open_file_in_default_editor


class Configuration:
def __init__(self):
self._cli_parse()

# Check if user wants to edit config before doing anything else
if self.cli_args.edit_config:
config_file_path = os.environ.get(
"PYDORO_CONFIG_FILE", os.path.expanduser("~/.config/pydoro/pydoro.ini")
)
open_file_in_default_editor(config_file_path)

self._ini_parse()
self._ini_load()
self._cli_load()
Expand Down Expand Up @@ -41,6 +49,7 @@ def _cli_parse(self):
"--version", help="display version and exit", action="store_true"
)
parser.add_argument("--audio-file", metavar="path", help="custom audio file")
parser.add_argument("--edit-config", help="open config file in editor", action="store_true")
self.cli_args = parser.parse_args()

def _ini_parse(self):
Expand Down
33 changes: 22 additions & 11 deletions pydoro/pydoro_core/tomato.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import itertools
import subprocess
import sys
import os
from enum import IntEnum
from timeit import default_timer

from pydoro.pydoro_core import sound
from pydoro.pydoro_core.util import open_file_in_default_editor
from pydoro.pydoro_core.config import Configuration

from prompt_toolkit.application.current import get_app

SECONDS_PER_MIN = 60

PLACEHOLDER_TIME = "time"
Expand Down Expand Up @@ -271,14 +275,14 @@ def start(self):
@property
def time_remaining(self):
self._calc_remainder()
if self._tomato.no_clock:
if self._tomato.configs.no_clock:
return ""
return self._format_time(self._remainder)

@property
def next_state(self):
self._tomato.tomatoes += 1
if self._tomato.tomatoes % self._tomato.tomatoes_per_set == 0:
if self._tomato.tomatoes % self._tomato.configs.tomatoes_per_set == 0:
return IntermediateState.transition_to(LongBreakState, self._tomato)
return IntermediateState.transition_to(SmallBreakState, self._tomato)

Expand Down Expand Up @@ -436,12 +440,8 @@ def return_to(tomato, state):
class Tomato:
def __init__(self, configs: Configuration):
self.configs = configs
self.no_sound = configs.no_sound
self.emoji = configs.emoji
self.tomatoes_per_set = configs.tomatoes_per_set
self.no_clock = configs.no_clock
self.reload_configs = False
self.tomatoes = 0

self._state = InitialState(self)

def start(self):
Expand All @@ -457,12 +457,23 @@ def reset_all(self):
self._state = InitialState(self)
self.tomatoes = 0

def edit_configs(self):
config_file_path = os.environ.get(
"PYDORO_CONFIG_FILE", os.path.expanduser("~/.config/pydoro/pydoro.ini")
)
open_file_in_default_editor(config_file_path)
# load config changes
self.configs = Configuration()
self._state = InitialState(self)
self.tomatoes = 0
get_app().reset()

def update(self):
if self._state.done:
self._state = self._state.next_state

def play_alarm(self):
if self.no_sound:
if self.configs.no_sound:
return
# noinspection PyBroadException
try:
Expand All @@ -472,7 +483,7 @@ def play_alarm(self):

def tomato_symbol(self):
ascii_tomato = "(`) "
if self.emoji:
if self.configs.emoji:
try:
"🍅".encode(sys.stdout.encoding)
return "🍅 "
Expand All @@ -486,7 +497,7 @@ def render(self) -> (list, int):
if not task:
task = [""] * 4

sets = self.tomatoes // self.tomatoes_per_set
sets = self.tomatoes // self.configs.tomatoes_per_set
if sets == 1:
sets = "1 set completed"
elif sets >= 2:
Expand All @@ -497,7 +508,7 @@ def render(self) -> (list, int):
status = TEXT[self._state.status]
time = self._state.time_remaining
count = self.tomato_symbol() * (
self.tomatoes_per_set - self.tomatoes % self.tomatoes_per_set
self.configs.tomatoes_per_set - self.tomatoes % self.configs.tomatoes_per_set
)

ftext = TOMATO[:]
Expand Down
24 changes: 22 additions & 2 deletions pydoro/pydoro_core/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import time
import sys
import subprocess


def every(delay, task):
Expand All @@ -11,8 +13,6 @@ def every(delay, task):


def in_app_path(path):
import sys

try:
wd = sys._MEIPASS
return os.path.abspath(os.path.join(wd, path))
Expand All @@ -27,3 +27,23 @@ def _from_resource(path):
if not os.path.exists(res_path):
res_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), path)
return res_path

def open_file_in_default_editor(file_path):
'''Opens config file in another process using default editor'''
try:
if sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
# Unix, Linux, macOS
editor = os.environ.get('EDITOR', 'nano') # Use EDITOR env if available, else default to nano
process = subprocess.Popen([editor, file_path])
elif sys.platform.startswith('win32'):
# Windows
process = subprocess.Popen(['cmd', '/c', 'start', '/wait', file_path], shell=True)
else:
print(f"Platform {sys.platform} not supported")
return
# wait for editor to close
process.wait()

except Exception as e:
print(f"Failed to open file: {e}")

25 changes: 14 additions & 11 deletions pydoro/pydoro_tui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
__version__ = "0.2.3"

import sys
import os
import threading
import subprocess

Expand All @@ -29,12 +30,12 @@


class UserInterface:
def __init__(self, config: Configuration):
self.config = config
self.tomato = Tomato(self.config)
def __init__(self, configs: Configuration):
self.configs = configs
self.tomato = Tomato(self.configs)
self.prev_hash = None

self.helpwindow = HelpContainer(self.config._conf["KeyBindings"])
self.helpwindow = HelpContainer(self.configs._conf["KeyBindings"])

self._create_ui()

Expand All @@ -43,6 +44,7 @@ def _create_ui(self):
btn_pause = Button("Pause", handler=self.tomato.pause)
btn_reset = Button("Reset", handler=self.tomato.reset)
btn_reset_all = Button("Reset All", handler=self.tomato.reset_all)
btn_edit_configs = Button("Configs", handler=self.tomato.edit_configs)
btn_exit = Button("Exit", handler=self._exit_clicked)
# All the widgets for the UI.
self.text_area = FormattedTextControl(focusable=False, show_cursor=False)
Expand All @@ -64,6 +66,7 @@ def _create_ui(self):
btn_pause,
btn_reset,
btn_reset_all,
btn_edit_configs,
btn_exit,
],
padding=1,
Expand Down Expand Up @@ -111,7 +114,7 @@ def _set_key_bindings(self):
"help": lambda _=None: self.toggle_help_window_state(),
}

for action, keys in self.config.key_bindings.items():
for action, keys in self.configs.key_bindings.items():
for key in keys.split(","):
try:
self.kb.add(key.strip())(actions[action])
Expand Down Expand Up @@ -180,18 +183,18 @@ def hide(self):


def main():
config = Configuration()
if config.audio_check:
configs = Configuration()
if configs.audio_check:
# WHY twice: to catch more issues
sound.play(in_app_path("b15.wav"), block=True)
sound.play(in_app_path("b15.wav"), block=True)
sys.exit(0)
if config.show_version:
if configs.show_version:
print("pydoro : version - {0}".format(__version__))
sys.exit(0)
UserInterface(config).run()
if config.exit_cmd:
subprocess.run(config.exit_cmd)
UserInterface(configs).run()
if configs.exit_cmd:
subprocess.run(configs.exit_cmd)


if __name__ == "__main__":
Expand Down

0 comments on commit bf38528

Please sign in to comment.