diff options
Diffstat (limited to 'dot_config/i3/scripts/executable_worldclock')
-rw-r--r-- | dot_config/i3/scripts/executable_worldclock | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/dot_config/i3/scripts/executable_worldclock b/dot_config/i3/scripts/executable_worldclock new file mode 100644 index 0000000..573800e --- /dev/null +++ b/dot_config/i3/scripts/executable_worldclock | |||
@@ -0,0 +1,122 @@ | |||
1 | #!/usr/bin/env bash | ||
2 | # | ||
3 | # Use rofi/zenity to display world clock. | ||
4 | # | ||
5 | # Note: this currently relies on associative array support in the shell. | ||
6 | # | ||
7 | # Inspired from i3pystatus wiki: | ||
8 | # https://github.com/enkore/i3pystatus/wiki/Shutdown-Menu | ||
9 | # | ||
10 | # Copyright 2022 Jinwei Zhao <i at jinwei dot me> | ||
11 | # | ||
12 | # This program is free software: you can redistribute it and/or modify | ||
13 | # it under the terms of the GNU General Public License as published by | ||
14 | # the Free Software Foundation, either version 3 of the License, or | ||
15 | # (at your option) any later version. | ||
16 | |||
17 | # This program is distributed in the hope that it will be useful, | ||
18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
20 | # GNU General Public License for more details. | ||
21 | |||
22 | # You should have received a copy of the GNU General Public License | ||
23 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
24 | |||
25 | # modified to work with latest rofi update by joekamprad <[email protected]> | ||
26 | |||
27 | ####################################################################### | ||
28 | # BEGIN CONFIG # | ||
29 | ####################################################################### | ||
30 | |||
31 | # Colors: FG (foreground), BG (background), HL (highlighted) | ||
32 | FG_COLOR="#bbbbbb" | ||
33 | BG_COLOR="#111111" | ||
34 | HLFG_COLOR="#111111" | ||
35 | HLBG_COLOR="#bbbbbb" | ||
36 | BORDER_COLOR="#222222" | ||
37 | |||
38 | # Options not related to colors (most rofi options do not work anymore) | ||
39 | ROFI_OPTIONS=(-theme ~/.config/rofi/worldclock.rasi) | ||
40 | |||
41 | ####################################################################### | ||
42 | # END CONFIG # | ||
43 | ####################################################################### | ||
44 | |||
45 | # Preferred launcher if both are available | ||
46 | preferred_launcher="rofi" | ||
47 | |||
48 | # Check whether the user-defined launcher is valid | ||
49 | launcher_list=(rofi) | ||
50 | function check_launcher() { | ||
51 | if [[ ! "${launcher_list[@]}" =~ (^|[[:space:]])"$1"($|[[:space:]]) ]]; then | ||
52 | echo "Supported launchers: ${launcher_list[*]}" | ||
53 | exit 1 | ||
54 | else | ||
55 | # Get array with unique elements and preferred launcher first | ||
56 | # Note: uniq expects a sorted list, so we cannot use it | ||
57 | i=1 | ||
58 | launcher_list=($(for l in "$1" "${launcher_list[@]}"; do printf "%i %s\n" "$i" "$l"; let i+=1; done \ | ||
59 | | sort -uk2 | sort -nk1 | cut -d' ' -f2- | tr '\n' ' ')) | ||
60 | fi | ||
61 | } | ||
62 | |||
63 | # Parse CLI arguments | ||
64 | while getopts "hcp:" option; do | ||
65 | case "${option}" in | ||
66 | p) preferred_launcher="${OPTARG}" | ||
67 | check_launcher "${preferred_launcher}" | ||
68 | ;; | ||
69 | *) exit 1 | ||
70 | ;; | ||
71 | esac | ||
72 | done | ||
73 | |||
74 | # Check whether a command exists | ||
75 | function command_exists() { | ||
76 | command -v "$1" &> /dev/null 2>&1 | ||
77 | } | ||
78 | |||
79 | # menu defined as an associative array | ||
80 | typeset -A menu | ||
81 | |||
82 | menu=( | ||
83 | [🇨🇦: `date '+%b %d %H:%M'`]="" | ||
84 | [🇬🇧: `TZ=Europe/London date '+%b %d %H:%M'`]="" | ||
85 | [🇨🇳: `TZ=Asia/Shanghai date '+%b %d %H:%M'`]="" | ||
86 | ) | ||
87 | |||
88 | menu_nrows=${#menu[@]} | ||
89 | |||
90 | launcher_exe="" | ||
91 | launcher_options="" | ||
92 | rofi_colors="" | ||
93 | |||
94 | function prepare_launcher() { | ||
95 | if [[ "$1" == "rofi" ]]; then | ||
96 | rofi_colors=(-bc "${BORDER_COLOR}" -bg "${BG_COLOR}" -fg "${FG_COLOR}" \ | ||
97 | -hlfg "${HLFG_COLOR}" -hlbg "${HLBG_COLOR}") | ||
98 | launcher_exe="rofi" | ||
99 | launcher_options=(-dmenu -i -lines "${menu_nrows}" -p "${ROFI_TEXT}" \ | ||
100 | "${rofi_colors}" "${ROFI_OPTIONS[@]}") | ||
101 | elif [[ "$1" == "zenity" ]]; then | ||
102 | launcher_exe="zenity" | ||
103 | launcher_options=(--list --title="${ZENITY_TITLE}" --text="${ZENITY_TEXT}" \ | ||
104 | "${ZENITY_OPTIONS[@]}") | ||
105 | fi | ||
106 | } | ||
107 | |||
108 | for l in "${launcher_list[@]}"; do | ||
109 | if command_exists "${l}" ; then | ||
110 | prepare_launcher "${l}" | ||
111 | break | ||
112 | fi | ||
113 | done | ||
114 | |||
115 | # No launcher available | ||
116 | if [[ -z "${launcher_exe}" ]]; then | ||
117 | exit 1 | ||
118 | fi | ||
119 | |||
120 | launcher=(${launcher_exe} "${launcher_options[@]}") | ||
121 | printf '%s\n' "${!menu[@]}" | sort | "${launcher[@]}" | ||
122 | |||