From acd042453efecb299559b54b4cf5a0564a65fde8 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Thu, 29 Oct 2015 00:59:43 -0400 Subject: tui_show_prompt(): Implement line editing --- (limited to 'src/ui') diff --git a/src/ui/tui.sh b/src/ui/tui.sh index b121906..6cf0f9f 100644 --- a/src/ui/tui.sh +++ b/src/ui/tui.sh @@ -188,6 +188,8 @@ tui_show_prompt() local curpos=0 local focus=0 local key= + local mask= + local buffer= # Calculate width and draw the box. w=$(expr ${#title} + 4) @@ -266,24 +268,62 @@ tui_show_prompt() fi ;; KEY_DEL) - if [ ${curpos} -lt ${#input} ]; then - : TODO - fi + [ ${curpos} -lt ${#input} ] || continue + i=0 + mask='' + while [ ${i} -lt ${curpos} ]; do + mask="${mask}." + i=$(($i + 1)) + done + cmd="s/^\\(${mask}\\).\\(.*\\)\$/\1\2/" + input="$(printf '%s' "${input}" | sed \ + "${cmd}")" ;; KEY_SPACE) - if [ ${curpos} -lt ${len} ]; then - # TODO: Handle curpos not at end - input="${input} " - curpos=$(($curpos + 1)) - fi + [ ${curpos} -lt ${len} ] || continue + key=' ' + i=0 + mask='' + while [ ${i} -lt ${curpos} ]; do + mask="${mask}." + i=$(($i + 1)) + done + cmd="s/^\\(${mask}\\).*\$/\1/" + buffer="$(printf '%s' "${input}" | sed \ + "${cmd}")${key}" + i=${curpos} + mask='' + while [ ${i} -lt ${#input} ]; do + mask="${mask}." + i=$(($i + 1)) + done + cmd="s/^.*\\(${mask}\\)\$/\1/" + input="${buffer}$(printf '%s' \ + "${input}" | sed "${cmd}")" + curpos=$(($curpos + 1)) ;; KEY_*);; *) - if [ ${curpos} -lt ${len} ]; then - # TODO: Handle curpos not at end - input="${input}${key}" - curpos=$(($curpos + 1)) - fi + [ ${curpos} -lt ${len} ] || continue + i=0 + mask='' + while [ ${i} -lt ${curpos} ]; do + mask="${mask}." + i=$(($i + 1)) + done + cmd="s/^\\(${mask}\\).*\$/\1/" + buffer="$(printf '%s' "${input}" | sed \ + "${cmd}")${key}" + i=${curpos} + mask='' + while [ ${i} -lt ${#input} ]; do + mask="${mask}." + i=$(($i + 1)) + done + cmd="s/^.*\\(${mask}\\)\$/\1/" + input="${buffer}$(printf '%s' \ + "${input}" | sed "${cmd}")" + curpos=$(($curpos + 1)) ;; esac elif [ ${focus} -eq 1 ]; then -- cgit v0.9.1