From time to time, I use Google to tell me the time of another place on Earth, almost too frequent that I had wanted to make my current dzen-status give me a world clock. Although I didn’t end up having one, but I figured out a way, so it’s possible for dzen to communicate with the shell script which is feeding it the data.
The next GIF was taken from actual dzen window:
I used mouse button and wheel to switch timezones. The final code is shown as the following:
#!/bin/bash
# Copyright (C) 2014 by Yu-Jie Lin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
TZs=(
'US/Eastern'
'US/Pacific'
'Europe/London'
'Europe/Paris'
'Asia/Tokyo'
'Asia/Shanghai'
)
TZ_IDX=0
export TZ=${TZs[TZ_IDX]}
PIPE="/tmp/pipe.$$"
trap "rm -f $PIPE" EXIT INT
mkfifo "$PIPE"
exec 7<>"$PIPE"
while :; do
if read -t 1 -u 7; then
case "$REPLY" in
next)
((TZ_IDX = ++TZ_IDX % ${#TZs[@]}))
;;
prev)
((TZ_IDX = (TZ_IDX + ${#TZs[@]} - 1) % ${#TZs[@]}))
;;
esac
TZ=${TZs[TZ_IDX]}
fi
printf '%s %(%c)T\n' $TZ -1
done |
dzen2 \
-x 100 \
-y 100 \
-tw 640 \
-fn 'Inconsolata-16' \
-e "button1=exec:echo next >$PIPE;button3=exit;button4=exec:echo prev >$PIPE;button5=exec:echo next >$PIPE"
Basically, the communication between dzen and the script is via a FIFO, it is opened as file descriptor 7 and read receives data from the file descriptor with a one second timeout, that also serves as interval for displaying the clock.
If the data is next, then the script moves to next timezone; if it’s prev, then go backward.
I think it’s even possible to make simple game, like a puzzle game, with dzen2. It will need to use ^ca() (click area) for specific area in the dzen window. This clock can even have a list of place names, and user can click on one of the names and get the time of the place. Sort of like a very primitive UI, it’s a bit of crazy to go through so many troubles, but it’s still doable with dzen.
But I will stop here, maybe I will make a game next time.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.