#! /bin/sh #---------------------------------------------------------------------------- # /var/install/bin/choose - choose from list # # Creation: 2007-01-11 max # Last Update: $Id$ # # Copyright (c) 2001-@@YEAR@@ the eisfair team, team(at)eisfair(dot)org # # 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 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------- #---------------------------------------------------------------------------- # # Syntax: # # /var/install/bin/choose ARRAY [POS] # #---------------------------------------------------------------------------- # include eislib . /var/install/include/eislib ### get core parameters _choose_array="$1" _choose_start="$2" eval _choose_title='$'$_choose_array'_TITLE' eval _choose_subtitle='$'$_choose_array'_SUBTITLE' eval _choose_caption='$'$_choose_array'_CAPTION_1' eval _choose_question='$'$_choose_array'_QUESTION' eval _choose_cols='$'$_choose_array'_COLS' _choose_cols_n="`echo "$_choose_cols" | wc -w`" _choose_cols_seq="`seq $_choose_cols_n`" eval _choose_arrayrows='$'$_choose_array'_ROWS' _choose_skip_pre=0 _choose_skip_post=0 _choose_indent=8 ### special flags eval _choose_flags='$'$_choose_array'_FLAGS' _choose_spread='no' _choose_showexit='no' ### is parameter [POS] a valid number? if !(echo "$_choose_start" | grep -qE '^[0-9]+$') then _choose_start=1 fi ### check flags set -- $_choose_flags while [ $# -gt 0 ] do case "$1" in --spread|-spread) _choose_spread='yes' shift ;; --showexit|-showexit) _choose_showexit='yes' shift ;; --indent|-indent) shift let _choose_indent=$1+4 [ "$_choose_indent" -lt "8" ] && _choose_indent=8 shift ;; --list) _choose_list='yes' shift ;; *) shift ;; esac done ### find inactive rows and build mapping _choose_rows=0 for i in `seq $_choose_arrayrows` do eval _choose_active='$'$_choose_array'_'${i}'_ACTIVE' if [ "$_choose_active" != "no" ] then let _choose_rows+=1 _choose_map[$_choose_rows]="$i" fi # also map [POS] if [ "$i" -eq "$_choose_start" ] then _choose_start="$_choose_rows" if [ "$_choose_active" = "no" ] then let _choose_start+=1 fi fi done if [ "$_choose_start" -gt "$_choose_rows" ] then _choose_start="$_choose_rows" fi ### calculate starting page ### here we never have -spread, since this is only ### valid if we only have 1 page refresh_screensize --tty let _choose_lines=$_EISLIB_SCREENSIZE_Y-5 if [ -n "${_choose_subtitle}" ] then let _choose_lines-=1 # check if '\n' newline im string if echo "${_choose_subtitle}" | grep -q '\\n' then let _choose_lines-=1 fi fi [ "$_choose_caption" != "" ] && let _choose_lines-=1 [ "$_choose_showexit" = "yes" ] && let _choose_lines-=1 _choose_scrollhint="" if [ "$SCROLL" = "yes" ] then ### if user wants to scroll, always show everything in 1 page if [ $_choose_rows -gt $_choose_lines ] then _choose_scrollhint="Use SHIFT + PAGE-UP to scroll up" _choose_lines=$_choose_rows fi _choose_from=1 else let _choose_from=($_choose_start-1)/$_choose_lines*$_choose_lines+1 fi ### parameter processing if [ "$_choose_spread" = "yes" ] then let _choose_temp=$_choose_lines/2 if [ $_choose_temp -lt $_choose_rows ] then ### more than 1 page => no spreading _choose_spread="no" else ### vcenter _choose_skip_pre=0 _choose_skip_post=0 fi fi ### print list while [ 1 ] do ### first refresh screen sizes refresh_screensize --tty let _choose_lines=$_EISLIB_SCREENSIZE_Y-5 if [ -n "${_choose_subtitle}" ] then let _choose_lines-=1 # check if '\n' newline im string if echo "${_choose_subtitle}" | grep -q '\\n' then let _choose_lines-=1 fi fi [ "$_choose_caption" != "" ] && let _choose_lines-=1 [ "$_choose_showexit" = "yes" ] && let _choose_lines-=1 let _choose_to=$_choose_from+$_choose_lines-1 if [ "$SCROLL" = "yes" -a $_choose_to -lt $_choose_rows ] then ### if user wants to scroll, always show everything in 1 page _choose_lines=$_choose_rows _choose_to=$_choose_rows fi if [ $_choose_to -gt $_choose_rows ] then let _choose_skip_post=$_choose_to-$_choose_rows _choose_to=$_choose_rows else _choose_skip_post=0 fi ### print title/subtitle clrhome -tty >/dev/tty mecho --tty --info "$_choose_title" >/dev/tty # caption uses 2 or 3 lines [ "$_choose_subtitle" != "" ] && mecho --tty --std "$_choose_subtitle" >/dev/tty mecho --tty >/dev/tty techo --tty --begin ${_choose_indent}r "${_choose_cols}" >/dev/tty ### vcenter if spread is set if [ "$_choose_spread" = "yes" ] then ### remember: spread only works if we have not more than 1 page echo todo >/dev/null let _choose_skip_post-=$_choose_rows+1 [ "$_choose_caption" != "" ] && let _choose_skip_post-=1 [ "$_choose_showexit" = "yes" ] && let _choose_skip_post-=1 ### let _choose_skip_post+=$_choose_to%2 mecho --tty >/dev/tty let _choose_skip_pre=$_choose_skip_post/2 let _choose_skip_post-=$_choose_skip_pre for j in `seq $_choose_skip_pre` do mecho --tty >/dev/tty done fi ### print caption if [ "$_choose_caption" != "" ] then for k in $_choose_cols_seq do eval _choose_data[$k]='$'$_choose_array'_CAPTION_'$k done eval techo --tty --row '""' "${_choose_data[*]}" >/dev/tty [ "$_choose_spread" = "yes" ] && mecho --tty >/dev/tty fi ### print rows for j in `seq $_choose_from $_choose_to` do for k in $_choose_cols_seq do eval _choose_data[$k]='$'$_choose_array'_'${_choose_map[$j]}'_'$k done if [ "${_choose_list}" = "yes" ] then eval techo --tty --row '""' "${_choose_data[*]}" >/dev/tty else eval techo --tty --row "$j." "${_choose_data[*]}" >/dev/tty fi [ "$_choose_spread" = "yes" ] && mecho --tty >/dev/tty done if [ "$_choose_showexit" = "yes" ] then if [ $_choose_to -lt $_choose_rows ] then eval techo --tty --row "0." "Return" >/dev/tty else eval techo --tty --row "0." "Exit" >/dev/tty fi [ "$_choose_spread" = "yes" ] && mecho --tty >/dev/tty fi ### fill page with empty lines for j in `seq $_choose_skip_post` do mecho --tty >/dev/tty done techo --tty --end >/dev/tty ### now print footer question mecho --tty >/dev/tty # question uses 4 lines mecho --tty --info $_choose_scrollhint >/dev/tty ### and ask question _choose_previous="p=previous" [ $_choose_from -eq 1 ] && _choose_previous="" eval _choose_list_to="1-'${_choose_to}'" eval _choose_list_rows="1-'${_choose_rows}'" _choose_return='^$=Return' if [ "${_choose_list}" = "yes" ] then _choose_list_to="" _choose_list_rows="" _choose_return='^$=Accept' fi if [ $_choose_to -lt $_choose_rows ] then ### we have more pages _ask_tmpfile=$(/bin/mktemp -t XXXXXXXXXXXXX) /var/install/bin/ask "$_choose_question" "" ${_choose_list_to} $_choose_previous "n=next page" "${_choose_return}" "0=Exit" > ${_ask_tmpfile} rc=$? read a < ${_ask_tmpfile} rm -f ${_ask_tmpfile} [ $rc = 255 ] && exit 255 [ "$a" = "" ] && a='return' [ "$a" = "0" ] && a="exit" else ### last page _ask_tmpfile=$(/bin/mktemp -t XXXXXXXXXXXXX) /var/install/bin/ask "$_choose_question" "" ${_choose_list_rows} $_choose_previous "${_choose_return}" "0=Exit" > ${_ask_tmpfile} rc=$? read a < ${_ask_tmpfile} rm -f ${_ask_tmpfile} [ $rc = 255 ] && exit 255 [ "$a" = "0" ] && a="exit" [ "$a" = "" ] && a="return" fi case "$a" in n) let _choose_from+=$_choose_lines ;; p) let _choose_from-=$_choose_lines [ "$_choose_from" -lt 1 ] && _choose_from=1 ;; exit) echo "0" exit 0 ;; return) echo "" exit 0 ;; *) a=`/usr/bin/expr $a + 0` echo "${_choose_map[$a]}" exit 0 ;; esac done exit 255 # not reached