From 89332994eb38982a8bd04ccc008bc5aba0c58908 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Wed, 28 Oct 2015 23:31:49 -0400 Subject: tui_show_prompt(): Implement (line editing missing) --- (limited to 'src/ui') diff --git a/src/ui/tui.sh b/src/ui/tui.sh index 6e8b4e1..40a2857 100644 --- a/src/ui/tui.sh +++ b/src/ui/tui.sh @@ -174,8 +174,91 @@ tui_show_menu() tui_show_prompt() { local title="${1}" + local len=${2} + shift 2 + local w= + local y= + local x= + local i= + local line='' local input='' - shift 1 + local curpos=0 + local focus=0 + local key= + + # Calculate width and draw the box. + w=$(expr ${#title} + 4) + if [ $(expr ${len} + 7) -gt ${w} ]; then + w=$(expr ${len} + 7) + fi + read -r y x <<-EOF + $(_tui_box ${w} 7) + EOF + + # Write the title. + term_cursor_down 1 + term_cursor_forward 2 + term_write "${title}" + + # Build blank input line. + i=0 + while [ ${i} -le ${len} ]; do + line="${line} " + i=$(($i + 1)) + done + + # Event loop. + while :; do + # Draw a button. + term_cursor_position $(($y + 5)) $(($x + $w - 6)) + if [ ${focus} -eq 1 ]; then + term_attr_on reverse + term_write '[OK]' + term_attr_off reverse + else + term_write '[OK]' + fi + + # Draw a textbox. + term_cursor_position $(($y + 3)) $(($x + 2)) + term_write '[' + term_attr_on underline + term_write "${line}" + term_attr_off underline + term_write ']' + term_cursor_position $(($y + 3)) $(($x + 3)) + term_attr_on underline + term_write "${input}" + term_attr_off underline + + # Put the cursor in the text box. + if [ ${focus} -eq 0 ]; then + term_show_cursor + term_cursor_position $(($y + 3)) $(($x + 3 + $curpos)) + else + term_hide_cursor + fi + + # Input. + key="$(term_getch)" + if [ "x${key}" = xKEY_TAB ]; then + focus=$(expr \( ${focus} + 1 \) % 2) + elif [ ${focus} -eq 0 ]; then + case "${key}" in + KEY_ENTER) break;; + # TODO: Line editing + *) + input="${input}${key}" + curpos=$(($curpos + 1)) + ;; + esac + elif [ ${focus} -eq 1 ]; then + case "${key}" in + KEY_ENTER | KEY_SPACE) break;; + esac + fi + done + printf '%s' "${input}" return 0 } -- cgit v0.9.1