diff options
author | P. J. McDermott <pj@pehjota.net> | 2015-10-28 22:12:25 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2015-10-28 22:12:25 (EDT) |
commit | c760fe41c838535197f3da239a1f8966df83f8e3 (patch) | |
tree | d9f0a6b1f2408becc372dd7b9e736f41b17d2cdf | |
parent | ec91d477e2b03f3dd80b96808e7abfedffa6a368 (diff) | |
download | firman.sh-c760fe41c838535197f3da239a1f8966df83f8e3.zip firman.sh-c760fe41c838535197f3da239a1f8966df83f8e3.tar.gz firman.sh-c760fe41c838535197f3da239a1f8966df83f8e3.tar.bz2 |
src/ui/tui.sh: New file
-rw-r--r-- | src/ui/local.mk | 3 | ||||
-rw-r--r-- | src/ui/tui.sh | 177 |
2 files changed, 179 insertions, 1 deletions
diff --git a/src/ui/local.mk b/src/ui/local.mk index 636d0a5..6343a2f 100644 --- a/src/ui/local.mk +++ b/src/ui/local.mk @@ -1,2 +1,3 @@ firman_SOURCES += \ - src/ui/cli.sh + src/ui/cli.sh \ + src/ui/tui.sh diff --git a/src/ui/tui.sh b/src/ui/tui.sh new file mode 100644 index 0000000..acac483 --- /dev/null +++ b/src/ui/tui.sh @@ -0,0 +1,177 @@ +# TUI user interface +# +# Copyright (C) 2015 Patrick "P. J." McDermott +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +tui_init_ui() +{ + term_init + term_raw + term_noecho + term_hide_cursor + term_clear +} + +tui_exit_ui() +{ + term_attr_reset + term_clear + term_show_cursor + term_echo + term_cooked +} + +_tui_box() +{ + local w=${1} + local h=${2} + shift 2 + local i= + local line='' + + # Clear the screen and set the cursor position to center the box. + term_clear + term_cursor_position $(expr \( $(term_lines) - ${h} \) / 2) \ + $(expr \( $(term_columns) - ${w} \) / 2) + + # Build the line. + i=0 + while [ ${i} -lt ${w} ]; do + line="${line} " + i=$(($i + 1)) + done + + # Draw white box background lines. + term_fg_color_set black + term_bg_color_set white + i=0 + while [ ${i} -lt ${h} ]; do + printf '%s' "${line}" + term_cursor_back ${w} + term_cursor_down 1 + i=$(($i + 1)) + done + + # Reset the cursor to the top-left corner of the box. + term_cursor_up ${h} + + return 0 +} + +_tui_dialog() +{ + local lvl="${1}" + local fmt="${2}" + shift 2 + local msg='' + + # Format the message. + msg="$(printf "${fmt}" "${@}")" + + # Draw the box. + _tui_box $(expr ${#msg} + 8) 5 + + # Draw an icon. + term_cursor_forward 2 + case "${lvl}" in + warn) + term_fg_color_set yellow + term_write ' .' + term_cursor_back 2 + term_cursor_down 1 + term_attr_on underline + term_write '/!\' + term_attr_off underline + term_fg_color_set black + ;; + err) + term_cursor_forward 1 + term_fg_color_set red + term_attr_on underline + term_write ' ' + term_attr_off underline + term_cursor_back 2 + term_cursor_down 1 + term_write '(' + term_attr_on underline + term_write 'x' + term_attr_off underline + term_write ')' + term_fg_color_set black + ;; + esac + + # Write the message. + term_cursor_forward 1 + term_write "${msg}" + + # Draw a (selected) button. + term_cursor_back 4 + term_cursor_down 2 + term_attr_on reverse + term_write '[OK]' + term_attr_off reverse + + while :; do + case "$(term_getch)" in + KEY_ENTER | KEY_SPACE) break;; + esac + done + + return 0 +} + +tui_dbg() +{ + : No output. +} + +tui_info() +{ + : No output. +} + +tui_warn() +{ + local fmt="${1}" + shift 1 + + _tui_dialog warn "${fmt}" "${@}" +} + +tui_err() +{ + local fmt="${1}" + shift 1 + + _tui_dialog err "${fmt}" "${@}" +} + +tui_show_menu() +{ + local title="${1}" + shift 1 + + return 0 +} + +tui_show_prompt() +{ + local title="${1}" + local input='' + shift 1 + + return 0 +} |