Skip to content

Instantly share code, notes, and snippets.

@madprops
Last active July 19, 2023 09:28
Show Gist options
  • Save madprops/5eedd6bcb2bffe1e897f18a937f483c2 to your computer and use it in GitHub Desktop.
Save madprops/5eedd6bcb2bffe1e897f18a937f483c2 to your computer and use it in GitHub Desktop.
Timer manager ruby script
#!/usr/bin/env ruby
require "open3"
# Get input information using rofi
def get_input(prompt, data)
cmd = "rofi -dmenu -p '#{prompt}' -i"
stdin, stdout, stderr, wait_thr = Open3.popen3(cmd)
stdin.puts(data)
stdin.close
return stdout.read.strip
end
# Main function
def main()
data_file_name = File.expand_path(File.dirname(__FILE__) + "/timer.data")
data = File.read(data_file_name).strip
if ARGV.length >= 2
title = ARGV[0]
time = ARGV[1..-1].join(" ")
else
title = get_input("Enter Title", data)
if title == ""
exit
end
times = "5m\n10m\n15m\n20m\n30m\n1h\n2h"
time = get_input("Enter Time", times)
if time == ""
exit
end
end
n = time.gsub(/[^0-9]/, "").strip.to_i
u = time.gsub(/[0-9]/, "").strip
if u == "m" or u.include?("min")
t = n
elsif u == "s" or u.include?("sec")
t = n / 60.0
elsif u == "h" or u.include?("hour")
t = n * 60.0
else
t = n
end
lines = data.split("\n")
lines.delete_if {|x| x == title}
lines.unshift(title)
File.write(data_file_name, lines.join("\n"))
`awesome-client 'Utils.timer("#{title}", #{t})'`
end
# Start here
if __FILE__ == $0
main()
end
# timer.data is just lines of timer names to show in rofi
# Like:
#Tacos
#Tea
#Hue
#Grasshopper
#Release
# The file gets updated on each timer run based on input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment