#!/bin/bash
#  cli - simple readline-based backend

set -e

lib_load 'mcui/input'


######
# Internal functions

cli_run_prompt() {
	has_args 3 "$@"
	local func=$1
	local -n var=$2
	local prompt=$3

	local value=''
	run "prompt_$func" "$prompt" value

	case "$value" in
	(true)		var='yes' ;;
	(false)		var='no' ;;
	('')		var='cancel' ;;
	(*)		error "$value: unexpected prompt return value" ;;
	esac
}

cli_run_input() {
	has_args 4 "$@"
	local func=$1
	local var=$2
	local prompt=$3
	local init=$4
	run $func "$prompt " $var
}

cli_title() {
	local warning=${1:-false}
	local func="warn"
	$warning || func="app_echo"
	[ -z "$mcui_title" ] || $func "=== $mcui_title ==="
}


######
# Output UI functions

cli_error_box() { cli_title true; prompt_error "$1"; }
cli_error_details() { cli_title true; error "$1" || prompt_error "$2"; }

cli_warn_box() { cli_title true; prompt_warn "$1"; }
cli_warn_details() { cli_title true; warn "$1"; prompt_warn "$2"; }

cli_info_box() { cli_title; prompt_info "$1"; }
cli_text_box() { cli_title; run_pager "$1"; }

cli_passive() { cli_title; app_echo "INFO: $1"; }
cli_progress() {
	local prompt=$1
	local value=$2
	if [ "$value" = done ]; then
		echo
		return
	fi

	local left=$((value / 2))
	local right=$((50 - $left))

	local lbar=$(printf "%${left}s" '')
	local rbar=$(printf "%${right}s" '')

	printf "%16s [%s>%s] %3d %%\r" "$1" "${lbar// /=}" "$rbar" "$value"
}


######
# Prompt UI functions

cli_yesno() { cli_run_prompt yesno $1 "$2 [y/n] "; }
cli_yesno_warn() { cli_run_prompt yesno $1 "CAUTION: $2 [y/n] "; }

cli_yesnocancel() { cli_run_prompt yesnocancel $1 "$2 [y/n/c] "; }
cli_yesnocancel_warn() { cli_run_prompt yesnocancel $1 "CAUTION: $2 [y/n/c] "; }

cli_continue_cancel() { cli_run_prompt yesno $1 "CAUTION: $2 [continue/cancel] "; }


######
# Input UI functions

cli_input() { cli_run_input read_string "$@"; }
cli_input_text() {
	local -n var=$1
	prompt_info "$2"
	run_editor "$3"
	var=$(<"$3")
}
cli_askpass() { cli_run_input read_silent "$@"; }

cli_menu() { error "unimplemented"; }
cli_checklist() { error "unimplemented"; }
cli_radiolist() { error "unimplemented"; }
cli_combobox() { error "unimplemented"; }

cli_dir_choose() { error "unimplemented"; }
cli_file_open() { error "unimplemented"; }
cli_file_save() { error "unimplemented"; }

cli_calendar() { error "unimplemented"; }
cli_slider() { error "unimplemented"; }
cli_color() { error "unimplemented"; }

View the Script Reference Index


Generated on Tue Apr 25 21:21:02 PDT 2017 by mcsh i7 v0.18.0.