-
-
Save fridim/9ccd597de24441e663cc to your computer and use it in GitHub Desktop.
Script to toggle timezones in i3blocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/ruby | |
# | |
# Toggle timezones in i3blocks | |
# Author: Vivien Didelot <vivien.didelot@gmail.com> | |
# | |
# Fill the ZONES array and define a block like this: | |
# | |
# [timezone] | |
# command=THIS_SCRIPT | |
# interval=5 | |
ZONES = [ | |
"Europe/Paris", | |
"America/Montreal", | |
"Indian/Reunion", | |
] | |
CACHE = "/tmp/tz" | |
def datetime zone, format | |
%x(TZ=#{zone} date '+#{format}') | |
end | |
if File.exist? CACHE | |
index = File.read(CACHE).to_i | |
else | |
index = 0 | |
end | |
# toggle on click | |
if ENV["BLOCK_BUTTON"].to_i > 0 | |
# cache the index | |
index = (index + 1) % ZONES.size | |
File.open(CACHE, 'w') { |f| f.write(index) } | |
end | |
zone = ZONES[index] | |
city = zone.split('/').last | |
puts "#{city}: #{datetime(zone, "%Y-%m-%d %T")}" | |
puts "#{city.chars.first}: #{datetime(zone, "%T")}" | |
puts "#77FF77" | |
# vim: ts=2 sw=2 et |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment