#!/bin/bash
# mcui - Mandolin Creek User Interface (MCUI) support

set -e


######
# Library initialization

# mcui_lib_init - detects best MCUI backend and loads it
mcui_lib_init() {
	mcui_unimplemented_init

	mcui_backend_detect
	debug "\$mcui_backend set to '$mcui_backend'"
	mcui_backend_load
}

# mcui_backend_load - selects the best available backend
mcui_backend_detect() {
	if [ "$mcui_backend" ]; then
		debug "$FUNCNAME: skipping backend detection..."
		return
	fi

	local backend
	if [ "$DISPLAY" ]; then
		backend=kdialog
	elif file_is_terminal stdin; then
		backend=dialog
	else
		backend=cli
	fi
	mcui_backend=$backend
}

# mcui_backend_load - loads the selected backend
mcui_backend_load() {
	case "$mcui_backend" in
	(cli)
		lib_load 'mcui/cli'
		;;
	(dialog)
		lib_load 'mcui/dialog'
		;;
	(kdialog)
		lib_load 'mcui/kdialog'
		;;
	(*)
		warn "$mcui_backend: loading unsupported MCUI backend..."
		lib_load "$mcui_backend"
		;;
	esac
}


######
# Configuration

mcui_config_init() {
	lib_setting_vars -ro mcui_backend

	lib_setting_vars --null mcui_title mcui_default

	# mcui_remember - name of flag to remember to ignore a dialog
	lib_setting_vars --null mcui_remember

	lib_setting_vars --null \
		mcui_label_yes mcui_label_no \
		mcui_label_continue mcui_label_cancel

	# mcui_multiple - allow multiple selections
	lib_setting_vars mcui_multiple
	mcui_multiple=false

	# mcui_separate_output - output one selection per line
	lib_setting_vars mcui_separate_output
	mcui_separate_output=false
}


######
# Internal functions

mcui_run() {
	min_args 2 "$@"
	local func=$1
	local nargs=$2
	shift 2
	has_args "$nargs" "$@"

	"${mcui_backend}_$func" "$@"
}

# mcui_run_prompt() - Runs a MCUI prompt function
# $1 - function
# $2 - output variable
# $3 - prompt
mcui_run_prompt() {
	has_args 3 "$@"
	mcui_run "$1" 2 "$2" "$3"
}

# mcui_prompt_result() - Interprets result of a MCUI prompt function
# $1 - output variable
# $2 - return code
mcui_prompt_result() {
	local -n var=$1

	case "$2" in
	(0) var='yes' ;;
	(1) var='no' ;;
	(2) var='cancel' ;;
	(*) error "$mcui_backend: unexpected result: $2" ;;
	esac
}


######
# Output UI functions

# mcui_error - display error string and wait for acknowledgement
# $1 - message
mcui_error_box() { mcui_run error_box 1 "$@"; }

# mcui_error_details - display error w/ details and wait for acknowledgement
# $1 - message
# $2 - details
mcui_error_details() { mcui_run error_details 2 "$@"; }


# mcui_warn - display warning string and wait for acknowledgement
# $1 - message
mcui_warn_box() { mcui_run warn_box 1 "$@"; }

# mcui_warn_details - display warning w/ details and wait for acknowledgement
# $1 - message
# $2 - details
mcui_warn_details() { mcui_run warn_details 2 "$@"; }


# mcui_info - display info string and wait for acknowledgement
# $1 - message
mcui_info_box() { mcui_run info_box 1 "$@"; }

# mcui_textbox - display text file and wait for acknowledgement
# $1 - file
mcui_text_box() { mcui_run text_box 1 "$@"; }


# mcui_passive - display passive information
# $1 - message
mcui_passive() { mcui_run passive 1 "$@"; }

# mcui_progress - display progress bar
# $1 - message
# $2 - percent (0--100)
mcui_progress() { mcui_run progress 2 "$@"; }


######
# Prompt UI functions

# mcui_yesno - prompt user with a benign yes/no question
# $1 - output variable
# $2 - prompt
mcui_yesno() { mcui_run_prompt yesno "$@"; }

# mcui_yesno_warn - prompt user with a risky yes/no question
# $1 - output variable
# $2 - prompt
mcui_yesno_warn() { mcui_run_prompt yesno_warn "$@"; }


# mcui_yesnocancel - prompt user with a benign yes/no/cancel question
# $1 - output variable
# $2 - prompt
mcui_yesnocancel() { mcui_run_prompt yesnocancel "$@"; }

# mcui_yesnocancel_warn - prompt user with a risk yes/no/cancel question
# $1 - output variable
# $2 - prompt
mcui_yesnocancel_warn() { mcui_run_prompt yesnocancel_warn "$@"; }


# mcui_continue_cancel - prompt user with a risk continue/cancel question
# $1 - output variable
# $2 - prompt
mcui_continue_cancel() { mcui_run_prompt continue_cancel "$@"; }


######
# Input UI functions

# mcui_input_run - wraps input functions to echo result to stdout
mcui_input_run() {
	local func=$1
	shift
	local out
	$func out "$@"
	echo "$out" >&2
}


# mcui_input - prompt user with a input box
# $1 - output variable
# $2 - prompt
# $3 - initial text
mcui_input() { mcui_run input 3 "$1" "$2" "$3"; }

# mcui_input_file - prompt user with a file in an input box
# $1 - output variable
# $2 - prompt
# $3 - file
mcui_input_file() { mcui_run input_text 3 "$@"; }

# mcui_input_text - prompt user with a large text input box
# $1 - output variable
# $2 - prompt
# $3 - initial text
mcui_input_text() {
	local textfile
	textfile=$(cmd_tempfile)
	echo "$3" >"$textfile"
	mcui_input_file "$1" "$2" "$textfile"
	run rm -f "$textfile"
}

# mcui_askpass - prompt user for a password
# $1 - output variable
# $2 - prompt
# $3 - initial text
mcui_askpass() { mcui_run askpass 3 "$1" "$2" "$3"; }


######
# High-level UI support

mcui_prompt_func() {
	min_args 3 "$@"
	local pfunc=$1
	local prompt=$2
	local func=$3
	local msg=$4

	local confirm
	mcui_$pfunc confirm "$prompt"

	if [ "$confirm" = yes ]; then
		$func
	elif [ "$msg" ]; then
		mcui_warn_box "$msg"
	fi
}

mcui_host_restart() {
	min_args 2 "$@"
	mcui_prompt_func yesno_warn "$1" host_restart "$2" "$3"
}


######
# Unimplemented functions

# mcui_unimplemented - Implementes ``unimplemented_*`` aliases
mcui_unimplemented() {
	min_args 2 "$@"
	local level=$2
	local name=$1
	shift
	$level "${FUNCNAME[1]}: not implemented${*:+ }$*"
}

# mcui_unimplemented_init - Defines function aliases to use when leaving.
# unimplemented functionality. These trigger different levels of interaction:
#
#   - ``unimplemented_error()`` - Missing implementation causes runtime errors
#   - ``unimplemented_warn()`` - Missing implementation may cause output errors
#   - ``unimplemented_info()`` - New implementation would fix bugs
#   - ``unimplemented_debug()`` - New implementation adds features
mcui_unimplemented_init() {
	local name
	for name in error warn info debug; do
		alias unimplemented_$name="mcui_unimplemented $name"
	done
	alias unimplemented='mcui_unimplemented error'
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Fri Jul 28 14:35:45 PDT 2017 by mcsh d14 v0.23.0.