diff --git a/WeeNu.py b/WeeNu.py index 5cf151a..698d27e 100644 --- a/WeeNu.py +++ b/WeeNu.py @@ -1,75 +1,83 @@ import rumps from datetime import datetime import webbrowser +import os def get_week_number(): - """ - Calculate the current week number according to the ISO 8601 standard, - which is used in Sweden. - """ today = datetime.now() week_number = today.isocalendar()[1] return week_number +class PrefixFormatter: + def __init__(self): + self.prefix_formats = self.load_prefixes_from_file() + self.current_format = self.load_format() + + def format_with_week(self, week_number): + self.current_format_formatted = f"{self.current_format} {week_number}" + return self.current_format_formatted + + def save_format(self, format_str): + try: + with open('user_settings', 'w') as f: + f.write(format_str) + self.current_format = format_str + except IOError as e: + rumps.alert(f"Error saving current prefix format: {e}") + + def load_format(self): + if not os.path.exists('user_settings'): + return self.prefix_formats[0] # Return the first format if the file doesn't exist + try: + with open('user_settings', 'r') as f: + format_str = f.read().strip() + if format_str in self.prefix_formats: + return format_str + except IOError as e: + rumps.alert(f"Error loading prefix format: {e}") + return self.prefix_formats[0] + + def load_prefixes_from_file(self): + try: + with open('prefix_list', 'r') as file: + prefixes = [line.strip() for line in file.readlines()] + except IOError as e: + raise Exception(f"Error reading file: 'prefix_list'") + return prefixes + class WeeNu(rumps.App): def __init__(self): super(WeeNu, self).__init__("WeeNu") - self.title_format = "w. {week_number}" + self.prefix_formatter = PrefixFormatter() - # Version info - self.menu = ["WeeNu v0.1", None] - - # Creating a submenu for format toggling - self.settings_menu = rumps.MenuItem("Settings") - self.toggle_week_prefix_item = rumps.MenuItem("Show prefix", callback=self.toggle_format) - - self.settings_menu.add(self.toggle_week_prefix_item) + self.menu = ["WeeNu v0.2", None] - # Initializing the menu item's state to True - self.toggle_week_prefix_item.state = True + self.format_menu = rumps.MenuItem("Prefix") + for format_option in self.prefix_formatter.prefix_formats: + menu_item = rumps.MenuItem(format_option, callback=self.change_prefix_format) + menu_item.state = format_option == self.prefix_formatter.current_format + self.format_menu.add(menu_item) - # Add a divider/separator here - self.menu = ["Manual update", self.settings_menu, None] # 'None' acts as a separator - - # Add a GitHub link menu item + self.menu.add(self.format_menu) + self.menu.add(None) self.github_link_item = rumps.MenuItem("About WeeNu", callback=self.open_github) self.menu.add(self.github_link_item) self.update_title() - @rumps.timer(60) # Update every 60 seconds + @rumps.timer(60) def update_title(self, _=None): - """ - Updates the menu bar title with the current week number, according to the selected format. - """ week_number = get_week_number() - self.title = self.title_format.format(week_number=week_number) + self.title = self.prefix_formatter.format_with_week(week_number) - @rumps.clicked("Manual update") - def on_update(self, _): - """ - Manually updates the week number when the 'Update' menu item is clicked. - """ + def change_prefix_format(self, sender): + literal_format = sender.title + self.prefix_formatter.save_format(literal_format) + for item in self.format_menu.values(): + item.state = item.title == literal_format self.update_title() - def toggle_format(self, sender): - """ - Toggles the title format and the checkmark state based on the current state. - """ - # Toggle the state and adjust the format based on the new state - sender.state = not sender.state - if sender.state: - self.title_format = "w. {week_number}" - sender.title = "Hide prefix" - else: - self.title_format = "{week_number}" - sender.title = "Show prefix" - self.update_title() - def open_github(self, _): - """ - Opens the GitHub repository URL in the default web browser. - """ webbrowser.open('https://github.com/mullweisser/mac-weenu') if __name__ == "__main__": diff --git a/prefix_list b/prefix_list new file mode 100644 index 0000000..2c257b6 --- /dev/null +++ b/prefix_list @@ -0,0 +1,11 @@ +Week +WK. +Wk. +wk. +W. +w. +WK +Wk +wk +W +w \ No newline at end of file diff --git a/user_settings b/user_settings new file mode 100644 index 0000000..dc7d5fb --- /dev/null +++ b/user_settings @@ -0,0 +1 @@ +Week \ No newline at end of file