#!/bin/bash
# u2 - User Interface helper

set -e

source '/home/zwelch/src/mcf/mcsh-release/install/share/mcsh/mcsh.sh'

lib_load 'mcui'


######
# Check

u2_config_init() {
	:
}

u2_config_check() {
	:
}

######
# CLI

u2_error() {
	case "$#" in
	(1) mcui_error_box "$@" ;;
	(2) mcui_error_details "$@";;
	(*) error_usage "wrong number of arguments" ;;
	esac
}
u2_warn() {
	case "$#" in
	(1) mcui_warn_box "$@" ;;
	(2) mcui_warn_details "$@";;
	(*) error_usage "wrong number of arguments" ;;
	esac
}

u2_info() { mcui_info_box "$@"; }
u2_text() { mcui_text_box "$@"; }

u2_passive() { mcui_passive "$@"; }
u2_progress() { mcui_progress "$@"; }


######
# Prompt UI functions

u2_yesno() { mcui_input_run mcui_yesno "$@"; }
u2_yesno_warn() { mcui_input_run mcui_yesno_warn "$@"; }
u2_yesnocancel() { mcui_input_run mcui_yesnocancel "$@"; }
u2_yesnocancel_warn() { mcui_input_run mcui_yesnocancel_warn "$@"; }
u2_continue_cancel() { mcui_input_run mcui_continue_cancel "$@"; }


######
# Input UI functions

u2_input() { cmd_dispatch "$@"; }
u2_input_line() { mcui_input_run mcui_input "$@"; }
u2_input_text() { mcui_input_run mcui_input_text "$@"; }
u2_input_file() { mcui_input_run mcui_input_file "$@"; }
u2_input_askpass() { mcui_input_run mcui_askpass "$@"; }


######
# Check

u2_check() { [ "$*" ] && cmd_dispatch "$@" || u2_check_default; }
u2_check_usage() {
	cat <<USAGE
...
UI Testing Commands:
	all				Runs all tests for each backend
	compare				Runs each test for each backend

Test Suites:
	output				Runs output tests
	prompt				Runs prompt tests
	input				Runs input tests
	progress			Runs progress tests
USAGE
}

u2_check_default() {
	warn "check: non-interactive testing of UI is not implemented"
	cmd_usage
}

for_each_backend() {
	# test built-in backends
	local -a mcui_backends=( cli dialog kdialog )

	local mcui_backend
	for mcui_backend in "${mcui_backends[@]}"; do
		mcui_backend_load
		"$@"
	done
}

u2_check_all() { for_each_backend _u2_check_all; }
_u2_check_all() {
	_u2_check_output
	_u2_check_prompt
	_u2_check_input
	_u2_check_progress
}

u2_check_compare() {
	u2_check_output
	u2_check_prompt
	u2_check_input
	u2_check_progress
}

u2_check_output() { for_each_backend _u2_check_output; }
_u2_check_output() {
	local -a funcs
	local func

	funcs=( error warn info passive )
	for func in "${funcs[@]}"; do
		app_echo "output: ${mcui_backend}_${func} with 1 arg..."
		u2_$func "$func"
	done

	funcs=( warn error )
	for func in "${funcs[@]}"; do
		app_echo "output: ${mcui_backend}_${func} with 2 args..."
		u2_$func "$mcui_backend" "$func"
	done
}

u2_check_prompt() { for_each_backend _u2_check_prompt; }
_u2_check_prompt() {
	local -a funcs
	funcs=( yesno yesno_warn yesnocancel yesnocancel_warn continue_cancel )
	local func
	for func in "${funcs[@]}"; do
		app_echo "prompt: ${mcui_backend}_${func} with 1 arg..."
		u2_$func "$mcui_backend $func"
	done
}

u2_check_input() { for_each_backend _u2_check_input; }
_u2_check_input() {
	local -a funcs=( input_line input_text input_askpass )
	for func in "${funcs[@]}"; do
		app_echo "input: ${mcui_backend}_${func} with 2 args..."
		u2_$func "$mcui_backend" "$func"
	done
}

u2_check_progress() { for_each_backend _u2_check_progress; }
_u2_check_progress() {
	local state
	for state in $(seq 0 25 100) 'done'; do
		u2_progress "$mcui_backend progress" "$state"
		sleep 1
	done
}


######
# Main

u2_desc() { echo "user interface tool"; }

u2_usage() {
	cat <<USAGE
<cmd> ...
UI Output Commands:
	error <text> [<details>]	Displays error
	warn <text> [<details>]		Displays warning
	info <text>			Displays information
	text <file>			Displays file contents
	passive <text>			Displays message using passive UI
	progress <text>			Displays progress bar

UI Prompt Commands:
	yesno <text> [<warn>]		Shows Yes/No prompt
	yesnocancel <text> [<warn>]	Shows Yes/No/Cancel prompt
	continue_cancel <text>		Shows Continue/Cancel prompt

UI Input Commands:
	input line <text> [<init>]	Displays string input UI
	input text <text> [<init>]	Displays text input UI
	input askpass <text> [<init>]	Displays password input UI
USAGE
}

u2_help() {
	cat <<HELP
u2 is user interface helper tool.  It is part of the mcsh package.
This tool creates user interface elements using the best mechanism
available: X (GUI), ncurses (TUI), or readline (CLI).
HELP
}

app_run "$@"

View the Developer Guide Index

View the Reference Manual Index


Generated on Sat Jul 8 19:27:57 PDT 2017 by mcsh d14 v0.22.0.