-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
99 lines (80 loc) · 2.98 KB
/
main.py
File metadata and controls
99 lines (80 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# ----- Build-In Modules -----
import sys
from pathlib import Path
from typing import Any, Dict
import pyperclip
plugindir: Path = Path.absolute(Path(__file__).parent)
paths = (".", "lib", "plugin")
sys.path = [str(plugindir / p) for p in paths] + sys.path
# ----- Py Flow Launcher Modules -----
from pyflowlauncher import Plugin, ResultResponse, send_results
from pyflowlauncher.result import Result
from pyflowlauncher.settings import settings
# ----- Configurable Constants -----
AppIcon: Path = Path(__file__).parent / "src" / "app.png"
DEFAULT_DECIMAL_PLACES = "2"
# Initialize plugin
plugin = Plugin()
def convert_time(query: str, settings_dict: Dict[str, Any]) -> ResultResponse:
"""
Converts a time string in "HH:MM" or "HH:MM:SS" format to a decimal hour representation.
Args:
query (str): A string representing the time, formatted as "HH:MM" or "HH:MM:SS".
settings_dict (Dict[str, Any]): Dictionary containing plugin settings
Returns:
ResultResponse: The converted time wrapped in a ResultResponse object.
Raises:
ValueError: If the input string does not match the expected format.
"""
parts: list[int] = list(map(int, query.strip().split(":")))
if len(parts) == 2:
m, s = parts
h = 0
elif len(parts) == 3:
h, m, s = parts
else:
raise ValueError
total_hours: float = h + m / 60 + s / 3600
# Get decimal places from settings, convert to int, and clamp between 0 and 6
decimal_places: int = min(
6, max(0, int(settings_dict.get("decimal_places", DEFAULT_DECIMAL_PLACES)))
)
result: str = f"{round(total_hours, decimal_places)}"
# Create the formatted display string showing input → result
display_text: str = f"{query.strip()} → {result} hrs"
# Create result with action
time_result = Result(
Title=display_text, SubTitle="Press Enter to copy", IcoPath=str(AppIcon)
)
time_result.add_action(copy, [result])
return send_results([time_result])
@plugin.on_method
def copy(text: str) -> None:
"""Copy text to clipboard
Args:
text (str): The text to copy
"""
pyperclip.copy(text)
@plugin.on_method
def query(query: str) -> ResultResponse:
try:
# Check for empty input
if not query.strip():
empty_result = Result(
Title="Enter time in MM:SS or HH:MM:SS format",
SubTitle="Example: 20:30 (20min 30sec) or 1:20:15 (1hr 20min 15sec)",
IcoPath=str(AppIcon),
)
return send_results([empty_result])
# Get current settings
plugin_settings: Dict[str, Any] = settings()
return convert_time(query, plugin_settings)
except ValueError:
error_result = Result(
Title="Invalid time format",
SubTitle="Please enter time in HH:MM or HH:MM:SS format",
IcoPath=str(AppIcon),
)
return send_results([error_result])
if __name__ == "__main__":
plugin.run()