Changeset - f3669338ab80
[Not reviewed]
0 1 0
Branko Majic (branko) - 9 months ago 2025-03-06 13:32:29
branko@majic.rs
[cheatsheet_viewer.sh] Implemented command for showing profile information.
1 file changed with 80 insertions and 0 deletions:
0 comments (0 inline, 0 general)
utils/cheatsheet_viewer.sh
Show inline comments
 
@@ -27,12 +27,13 @@ VERSION="0.0.1"
 
function usage() {
 
    cat <<EOF
 
$PROGRAM $VERSION, simple utility for viewing image cheatsheets
 

	
 
Usage:
 
  $PROGRAM [OPTIONS] list
 
  $PROGRAM [OPTIONS] info PROFILE
 
EOF
 
}
 

	
 
function short_help() {
 
    cat <<EOF
 
$(usage)
 
@@ -55,19 +56,35 @@ All configuration is stored under user's own home directory at:
 
Individual profiles are stored as sub-directories, with each
 
sub-directory representing an individual profile that can be
 
activated. Base directory for profiles is:
 

	
 
  - ~/.config/cheatsheet_viewer/profiles/
 

	
 
Each profile definition consists out of a number of configuration
 
files:
 

	
 
  - ~/.config/cheatsheet_viewer/profiles/PROFILE/cheatsheets (list of
 
    cheatsheet files, one per line, blank lines are ignored, lines
 
    starting with '#' are ignored)
 

	
 
Multiple commands are provided for working with the profiles and
 
displaying the cheatsheets.
 

	
 
list
 

	
 
  Lists the available profiles.
 

	
 
info PROFILE
 

	
 
  Arguments:
 

	
 
    PROFILE (profile name)
 

	
 
  Shows detailed information about user-specified profile, such as
 
  list of defined cheatsheets etc.
 

	
 
$PROGRAM accepts the following options:
 

	
 
    -q
 
        Quiet mode.
 
    -d
 
        Enable debug mode.
 
@@ -137,12 +154,63 @@ function command_list() {
 
    for profile in "${profiles[@]}"; do
 
        echo "  - $(basename "$profile")"
 
    done
 
}
 

	
 

	
 
#
 
# Displays profile information.
 
#
 
# 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_info() {
 
    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
 

	
 
    echo "Profile $profile"
 
    echo "========${profile//?/=}"
 
    echo
 
    echo "Base directory:   $profile_dir"
 
    echo "Cheatsheets list: $cheatsheets_file"
 
    echo
 

	
 
    if (( ${#cheatsheets[@]} == 0 )); then
 
        echo "No configured cheatsheets."
 
    else
 
        echo "Configured cheatsheets:"
 
        echo
 
        for cheatsheet in "${cheatsheets[@]}"; do
 
            echo "  - $cheatsheet"
 
        done
 
    fi
 
}
 

	
 

	
 
# 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)
 
if [[ -t 1 ]] && (( _COLOR_TERMINAL > 0 )); then
 
    _TEXT_BOLD=$(tput bold)
 
    _TEXT_WHITE=$(tput setaf 7)
 
@@ -231,12 +299,24 @@ COMMAND="${1-}"
 
shift
 

	
 
if [[ $COMMAND == list ]]; then
 

	
 
    command_list "$CONFIG_DIR"
 

	
 
elif [[ $COMMAND == info ]]; then
 

	
 
    PROFILE="${1-}"
 
    shift
 

	
 
    if [[ -z $PROFILE ]]; then
 
        error "Profile name must be specified."
 
        exit "$ERROR_ARGUMENTS"
 
    fi
 

	
 
    command_info "$CONFIG_DIR" "$PROFILE"
 

	
 
else
 

	
 
    error "Unsupported command: $COMMAND"
 
    exit "$ERROR_ARGUMENTS"
 

	
 
fi
0 comments (0 inline, 0 general)