#!/bin/bash

#  This script demonstrates a bug in vte. Invoking it without the
#  "-e" parameter causes the status line not to be updated. "-e"
#  inserts a "el" terminal capability string (clear to eol) which
#  makes the symptom go away.
#  
#  Running the script under xterm always succeeds, regardless of any
#  "-e" parameter.
#
# tput capabilities used:
#    init
#    clear
#    csr        change_scroll_region  line1 line2
#    cup        cursor_address        row column
#    el         clr_eol
#    smso       enter_standout_mode
#    cr         carriage_return
#    cuu1       cursor_up
#    rmso       exit_standout_mode
#    ind        scroll_forward


to_status_line() {
    tput cup $STATUS_LINE 0      # goto bottom line
    $DO_EL && tput el            # clear to eol if invoked with -e option
    tput smso                    # enter standout mode
    printf "%-${COLUMNS}s" "$1"  # print text on status line
#   $DO_SLOW && sleep 1          # sleep if invoked with -s option
    tput cr                      # cr (start of line)
    tput cuu1                    # cursor up
    tput rmso                    # end standout mode
    # Note: leave cursor at start of last line of the scroll region
}

to_scroll_region() {
    # Assumes we're already at start of last line of the scroll region.
    printf '%b' "$1"          # print message
}

####################

tput init                     # figure out the screen size
LINES=`tput lines`
COLUMNS=`tput cols`
DO_EL=false
DO_SLOW=false
while getopts "se" optname ; do
  case "$optname" in
    "e")
      DO_EL=true
      ;;
    "s")
      DO_SLOW=true
      ;;
  esac
done
tput clear
STATUS_LINE=$((LINES-1))
SCROLL_BOT=$((STATUS_LINE-1))
tput csr 0 $SCROLL_BOT         # create scroll region
tput cup $SCROLL_BOT 0         # goto bottom line of scroll region.
to_scroll_region "\nUnder vte Without the '-e' option, the status line often fails to update."
to_scroll_region "\nThe '-s' option sleeps 1 second after the status line update, which causes"
to_scroll_region "\nit to succeed. (So the bug is timing dependant.)"
to_scroll_region "\nUnder xterm, the status line stays consistently with or w/o '-e'.\n\n"
sleep 1
for word in one two three four five six seven eight nine ten
do
   save_up=`to_status_line   "STATUS LINE: $word"; to_scroll_region "\nSCROLL REGION: status line should show '$word'."`
   printf '%s' "$save_up"
   tput cr
   tput ind
   sleep 2

done
# clean up
tput csr 0 $((LINES-1))      # set scroll region to whole window
tput cup $STATUS_LINE 0      # goto bottom line
tput ind                     # scroll forward


