Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jjramsey authored Jun 30, 2024
1 parent 10cf59f commit 21adf3d
Showing 1 changed file with 155 additions and 63 deletions.
218 changes: 155 additions & 63 deletions contrib/waybar-yambar-poss-other-applets/workrave-break-info.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,139 @@

import pydbus

def wrap_timer_waybar(str_val, colors, timer_type, timer_state):
return f"<span color='{colors[0]}' bgcolor='{colors[1]}'>{str_val}</span>"
#########################################################################
#
# For those who want to edit this script's behavior, especially to add
# a new format ...
#
# The "fmt_timer_{$FORMAT}" function, where $FORMAT is a format type
# (plain, waybar, etc.), formats the string for an individual timer,
# i.e. microbreak, rest break, or daily limit. This function takes
# the following inputs to describe the timer:
#
# * enabled: a Boolean indicating if the timer is enabled
#
# * time_left: in seconds, the time left until the next break, or for
# the case of the daily limit, the time left for computer activity
# for the day
#
# * limit: in seconds, the time between breaks, or for the case of
# the daily limit, the daily limit itself
#
# * timer_type: a string with one of the following values:
# "microbreak", "restbreak", or "dailylimit".
#
# * timer_state: a string with one of the following values:
# "default", "close to break", and "overdue".
#
# * colors: a tuple of two strings, the first with a string
# indicating the foreground (or text) color, and the second
# indicating the background color. The colors indicate the value of
# timer_state.
#
# The "fmt_all_timers_{$FORMAT}" function takes a list of strings,
# each of which has information on an individual timer, and produces a
# string that contains informations about all of the timers
# described in that list.
#
# Here's how these functions are used. For example, suppose that:
#
# * The interval between microbreaks is 5 minutes and the time to
# the next microbreak is 3 minutes.
#
# * The interval between rest breaks is 50 minutes and the time to
# the next rest break is 15 minutes.
#
# * The daily limit is 4 hours, and time left for computer activity
# is 2.5 hours.
#
# Then fmt_timer_plain will generate the string "M 3:00/5:00" for the
# microbreak, "R 15:00/50:00" for the rest break, and
# "D 4:00:00/2:30:00" for the daily limit. fmt_all_timers_plain will
# receive the list of strings ["M 3:00/5:00", "R 15:00/50:00",
# "D 4:00:00/2:30:00"] and output the following string:
#
# "M 3:00/5:00 R 15:00/50:00 D 4:00:00/2:30:00"
#
# The dictionary fmt_info_dict associates a string that labels the
# format with a tuple containing the "fmt_timer_{$FORMAT}"
# function and the "fmt_all_timers_{$FORMAT}" function.
# The first key in fmt_info_dict is the default format (i.e.,
# "plain").
#
# To add, say, format "foo", define the functions fmt_timer_foo and
# fmt_all_timers_foo, and then add the following entry to fmt_info_dict:
#
# foo: (fmt_timer_foo, fmt_all_timers_foo)
#
#########################################################################

def fmt_timer_plain(enabled, time_left, limit, timer_type, timer_state, colors):

if not enabled: return ""

def wrap_timer_plain(str_val, colors, timer_type, timer_state):
return str_val
time_left_str = seconds_to_time_str(time_left)
limit_str = seconds_to_time_str(limit)

def wrap_timer_yambar(str_val, colors, timer_type, timer_state):
return "\n".join((f"{timer_type}|string|{str_val.replace('|', '/')}",
f'{timer_type}_state|string|{timer_state}'))
prefix = {"microbreak": "M",
"restbreak": "R",
"dailylimit": "D"}[timer_type]

return f"{prefix}: {time_left_str}/{limit_str}"

wrap_timer_func_dict = {
"plain": wrap_timer_plain,
"waybar": wrap_timer_waybar,
"yambar": wrap_timer_yambar
}
def fmt_all_timers_plain(timer_strs):
return " ".join(timer_str for timer_str in timer_strs if timer_str)

wrap_str_func_dict = {
"plain": lambda x: x,
"waybar": lambda x: f"""{{"text": "{x}", "tooltip": "Workrave"}}""",
"yambar": lambda x: f"{x}\n"
}
def fmt_timer_waybar(enabled, time_left, limit, timer_type, timer_state, colors):

if not enabled: return ""

timer_str = fmt_timer_plain(enabled, time_left, limit, timer_type, timer_state, colors)

return f"<span color='{colors[0]}' bgcolor='{colors[1]}'>{timer_str}</span>"

def fmt_all_timers_waybar(timer_strs):
all_timers_str = " ".join(timer_str for timer_str in timer_strs if timer_str)

return f"""{{"text": "{all_timers_str}", "tooltip": "Workrave"}}"""

def fmt_timer_yambar(enabled, time_left, limit, timer_type, timer_state, colors):

timer_str = fmt_timer_plain(True, time_left, limit, timer_type, timer_state, colors)

return "\n".join((f"{timer_type}|string|{timer_str.replace('|', '/')}",
f"{timer_type}_enabled|bool|{str(enabled).lower()}",
f'{timer_type}_state|string|{timer_state}'))

separator_dict = {
"plain": " ",
"waybar": " ",
"yambar": "\n"
def fmt_all_timers_yambar(timer_strs):
all_timers_str = "\n".join(timer_strs)
return f"{all_timers_str}\n"

def fmt_timer_json(enabled, time_left, limit, timer_type, timer_state, colors):
limit_str_key_prefix = (f"daily" if timer_type == "dailylimit"
else f"{timer_type}_")

return ", ".join([f'"{timer_type}_enabled": {str(enabled).lower()}',
f'"{timer_type}_left_in_seconds": {time_left}',
f'"{limit_str_key_prefix}limit_in_seconds": {limit}',
f'"{timer_type}_left_str": "{seconds_to_time_str(time_left)}"',
f'"{limit_str_key_prefix}limit_str": "{seconds_to_time_str(limit)}"',
f'"{timer_type}_state": "{timer_state}"',
f'"{timer_type}_fgcol": "{colors[0]}"',
f'"{timer_type}_bgcol": "{colors[1]}"'])

def fmt_all_timers_json(timer_strs):
all_timers_str = ", ".join(timer_strs)
return f"{{{all_timers_str}}}"

fmt_info_dict = {
"plain": (fmt_timer_plain, fmt_all_timers_plain),
"waybar": (fmt_timer_waybar, fmt_all_timers_waybar),
"yambar": (fmt_timer_yambar, fmt_all_timers_yambar),
"json": (fmt_timer_json, fmt_all_timers_json)
}

output_fmts = tuple(wrap_timer_func_dict.keys())
#### Shouldn't need to edit anything below here. ####

def seconds_to_time_str(tot_num_sec):
abs_tot_num_sec = abs(tot_num_sec)
Expand All @@ -49,46 +153,46 @@ def seconds_to_time_str(tot_num_sec):
num_min, num_sec = divmod(abs_tot_num_sec, 60)
return f"{sign}{num_min}:{num_sec:02d}"

def choose_colors(time_left, limit):
def get_timer_state(time_left, limit):
if time_left < 0:
return "overdue"
elif time_left/limit <= 0.1:
return "close to break"
else:
return "default"

def timer_str(timer_type, wr_core, wr_cfg, wrap_timer_func, color_dict):
def timer_str(timer_type, wr_core, wr_cfg, fmt_timer_func, color_dict):

timer_enabled_path = {
"microbreak": "/breaks/micro-pause/enabled",
"restbreak": "/breaks/rest-break/enabled",
"dailylimit": "/breaks/daily-limit/enabled"}[timer_type]

timer_obj_path = {
timer_limit_path = {
"microbreak": "/timers/micro_pause/limit",
"restbreak": "/timers/rest_break/limit",
"dailylimit": "/timers/daily_limit/limit"}[timer_type]

limit = wr_cfg.GetInt(timer_obj_path)[0]
time_left = limit - wr_core.GetTimerElapsed(timer_type)

timer_state = choose_colors(time_left, limit)
colors = color_dict[timer_state]
enabled = wr_cfg.GetBool(timer_enabled_path)[0]

time_left_str = seconds_to_time_str(time_left)
limit_str = seconds_to_time_str(limit)
limit = wr_cfg.GetInt(timer_limit_path)[0]
time_left = limit - wr_core.GetTimerElapsed(timer_type)

prefix = {"microbreak": "M",
"restbreak": "R",
"dailylimit": "D"}[timer_type]
timer_state = get_timer_state(time_left, limit)
colors = color_dict[timer_state]

basic_str = f"{prefix}: {time_left_str}/{limit_str}"
return fmt_timer_func(enabled, time_left, limit, timer_type, timer_state, colors)

return wrap_timer_func(basic_str, colors, timer_type, timer_state)
# def microbreak_str(wr_core, wr_cfg, fmt_timer_func, color_dict):
# return timer_str("microbreak", wr_core, wr_cfg, fmt_timer_func, color_dict)

def microbreak_str(wr_core, wr_cfg, wrap_timer_func, color_dict):
return timer_str("microbreak", wr_core, wr_cfg, wrap_timer_func, color_dict)
# def restbreak_str(wr_core, wr_cfg, fmt_timer_func, color_dict):
# return timer_str("restbreak", wr_core, wr_cfg, fmt_timer_func, color_dict)

def restbreak_str(wr_core, wr_cfg, wrap_timer_func, color_dict):
return timer_str("restbreak", wr_core, wr_cfg, wrap_timer_func, color_dict)
# def dailylimit_str(wr_core, wr_cfg, fmt_timer_func, color_dict):
# return timer_str("dailylimit", wr_core, wr_cfg, fmt_timer_func, color_dict)

def dailylimit_str(wr_core, wr_cfg, wrap_timer_func, color_dict):
return timer_str("dailylimit", wr_core, wr_cfg, wrap_timer_func, color_dict)
output_fmts = tuple(fmt_info_dict.keys())

parser = argparse.ArgumentParser()

Expand Down Expand Up @@ -117,9 +221,7 @@ def dailylimit_str(wr_core, wr_cfg, wrap_timer_func, color_dict):

args = parser.parse_args()

wrap_timer_func = wrap_timer_func_dict[args.format]
wrap_str_func = wrap_str_func_dict[args.format]
separator = separator_dict[args.format]
fmt_timer_func, fmt_all_timers_func = fmt_info_dict[args.format]

color_dict = {
"default": args.colors_default,
Expand All @@ -134,23 +236,13 @@ def dailylimit_str(wr_core, wr_cfg, wrap_timer_func, color_dict):

wr_cfg = wr_core["org.workrave.ConfigInterface"]

show_micro_break = wr_cfg.GetBool("/breaks/micro-pause/enabled")[0]
show_rest_break = wr_cfg.GetBool("/breaks/rest-break/enabled")[0]
show_daily_limit = wr_cfg.GetBool("/breaks/daily-limit/enabled")[0]

funcs_to_repeat = []
if show_micro_break:
funcs_to_repeat.append(microbreak_str)

if show_rest_break:
funcs_to_repeat.append(restbreak_str)

if show_daily_limit:
funcs_to_repeat.append(dailylimit_str)

while True:
print(wrap_str_func(
separator.join(func(wr_core, wr_cfg, wrap_timer_func, color_dict)
for func in funcs_to_repeat)), flush = True)

print(
fmt_all_timers_func(
[timer_str(timer_type, wr_core, wr_cfg, fmt_timer_func, color_dict)
for timer_type in ("microbreak", "restbreak", "dailylimit")]
), flush = True
)

time.sleep(args.polling_interval)

0 comments on commit 21adf3d

Please sign in to comment.