diff --git a/utils/cheatsheet_viewer.sh b/utils/cheatsheet_viewer.sh index 2a83ecc5ce44e1e4f72c327846843617da09123e..8343b3e4aa94a6586d79369eb6019b27b99d0786 100755 --- a/utils/cheatsheet_viewer.sh +++ b/utils/cheatsheet_viewer.sh @@ -31,6 +31,7 @@ $PROGRAM $VERSION, simple utility for viewing image cheatsheets Usage: $PROGRAM [OPTIONS] list $PROGRAM [OPTIONS] info PROFILE + $PROGRAM [OPTIONS] display [PROFILE] EOF } @@ -49,6 +50,8 @@ $(usage) $PROGRAM is a simple utility for viewing image cheatsheets with support for multiple profiles. +Cheatsheets are shown using the pqiv image viewer. + All configuration is stored under user's own home directory at: - ~/.config/cheatsheet_viewer/ @@ -82,6 +85,15 @@ info PROFILE Shows detailed information about user-specified profile, such as list of defined cheatsheets etc. +display [PROFILE] + + Arguments: + + PROFILE (profile name) + + Displays cheatsheets from the specified profile (default profile + name is 'default'). + $PROGRAM accepts the following options: -q @@ -208,6 +220,55 @@ function command_info() { } +# +# Displays configured cheatsheets from a profile. +# +# Arguments: +# +# $1 (config_dir) +# Base directory under which all configuration files are stored. +# +# $2 (profile) +# Profile name. +# +# Returns: +# 0 on success, 1 otherwise. +# +function command_display() { + local config_dir="$1" + local profile="$2" + + local profile_dir="$config_dir/profiles/$profile" + local cheatsheets_file="$profile_dir/cheatsheets" + + local cheatsheets=() + + if [[ ! -d $profile_dir ]]; then + error "No such profile found under $profile_dir" + return 1 + fi + + if [[ -e $cheatsheets_file ]]; then + readarray -t cheatsheets < <(grep --invert-match --extended-regexp "(^[[:blank:]]*#|^[[:blank:]]*$)" "$cheatsheets_file") + fi + + if (( ${#cheatsheets[@]} == 0 )); then + warning "No cheatsheets are defined under $cheatsheets_file" + return 0 + fi + + local pqiv_options=( + "--scale-mode-screen-fraction=1.0" + "--zoom-level=1.0" + "--transparent-background" + "--hide-info-box" + "--window-title=cheatsheet-viewer" + ) + + pqiv "${pqiv_options[@]}" "${cheatsheets[@]}" & +} + + # Set-up colours for message printing if we're not piping and terminal is # capable of outputting the colors. _COLOR_TERMINAL=$(tput colors 2>&1) @@ -314,6 +375,13 @@ elif [[ $COMMAND == info ]]; then command_info "$CONFIG_DIR" "$PROFILE" +elif [[ $COMMAND == display ]]; then + + PROFILE="${1-default}" + shift + + command_display "$CONFIG_DIR" "$PROFILE" + else error "Unsupported command: $COMMAND"