aboutsummaryrefslogblamecommitdiff
blob: 573800e55a84ebf0bf23e9e1de8c6ea47d9b24fc (plain) (tree)

























































































































                                                                                                         
#!/usr/bin/env bash
#
# Use rofi/zenity to display world clock.
#
# Note: this currently relies on associative array support in the shell.
#
# Inspired from i3pystatus wiki:
# https://github.com/enkore/i3pystatus/wiki/Shutdown-Menu
#
# Copyright 2022 Jinwei Zhao <i at jinwei dot me>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# modified to work with latest rofi update by joekamprad <[email protected]>

#######################################################################
#                            BEGIN CONFIG                             #
#######################################################################

# Colors: FG (foreground), BG (background), HL (highlighted)
FG_COLOR="#bbbbbb"
BG_COLOR="#111111"
HLFG_COLOR="#111111"
HLBG_COLOR="#bbbbbb"
BORDER_COLOR="#222222"

# Options not related to colors (most rofi options do not work anymore)
ROFI_OPTIONS=(-theme ~/.config/rofi/worldclock.rasi)

#######################################################################
#                             END CONFIG                              #
#######################################################################

# Preferred launcher if both are available
preferred_launcher="rofi"

# Check whether the user-defined launcher is valid
launcher_list=(rofi)
function check_launcher() {
  if [[ ! "${launcher_list[@]}" =~ (^|[[:space:]])"$1"($|[[:space:]]) ]]; then
    echo "Supported launchers: ${launcher_list[*]}"
    exit 1
  else
    # Get array with unique elements and preferred launcher first
    # Note: uniq expects a sorted list, so we cannot use it
    i=1
    launcher_list=($(for l in "$1" "${launcher_list[@]}"; do printf "%i %s\n" "$i" "$l"; let i+=1; done \
      | sort -uk2 | sort -nk1 | cut -d' ' -f2- | tr '\n' ' '))
  fi
}

# Parse CLI arguments
while getopts "hcp:" option; do
  case "${option}" in
    p) preferred_launcher="${OPTARG}"
       check_launcher "${preferred_launcher}"
       ;;
    *) exit 1
       ;;
  esac
done

# Check whether a command exists
function command_exists() {
  command -v "$1" &> /dev/null 2>&1
}

# menu defined as an associative array
typeset -A menu

menu=(
  [🇨🇦: `date '+%b %d %H:%M'`]=""
  [🇬🇧: `TZ=Europe/London date '+%b %d %H:%M'`]=""
  [🇨🇳: `TZ=Asia/Shanghai date '+%b %d %H:%M'`]=""
)

menu_nrows=${#menu[@]}

launcher_exe=""
launcher_options=""
rofi_colors=""

function prepare_launcher() {
  if [[ "$1" == "rofi" ]]; then
    rofi_colors=(-bc "${BORDER_COLOR}" -bg "${BG_COLOR}" -fg "${FG_COLOR}" \
        -hlfg "${HLFG_COLOR}" -hlbg "${HLBG_COLOR}")
    launcher_exe="rofi"
    launcher_options=(-dmenu -i -lines "${menu_nrows}" -p "${ROFI_TEXT}" \
        "${rofi_colors}" "${ROFI_OPTIONS[@]}")
  elif [[ "$1" == "zenity" ]]; then
    launcher_exe="zenity"
    launcher_options=(--list --title="${ZENITY_TITLE}" --text="${ZENITY_TEXT}" \
        "${ZENITY_OPTIONS[@]}")
  fi
}

for l in "${launcher_list[@]}"; do
  if command_exists "${l}" ; then
    prepare_launcher "${l}"
    break
  fi
done

# No launcher available
if [[ -z "${launcher_exe}" ]]; then
  exit 1
fi

launcher=(${launcher_exe} "${launcher_options[@]}")
printf '%s\n' "${!menu[@]}" | sort | "${launcher[@]}"
Powered by cgit v1.2.3 (git 2.41.0)