#!/bin/bash -u #console_codes(4) man page... vt100/2 et && EMCA-48 standard # (c) la walsh (2013) -- free to use and modify for personal use. # -- optionally licenced under Gnu v3 license. # v0.0.3 - try to reduce tabcols to minimal set to reproduce. # v0.0.2 - set tabs for full terminal width (try to get term width) shopt -s expand_aliases extglob alias int='declare -i' sub='function' array='declare -a' alias intArray='declare -ia' P=printf string=declare P -v clrallts "\x1b[3g" P -v sts "\033H" P -v cpr "\x1b[6n" sub getcols() { local sttyout="$(stty size /dev/tty) & 2>/dev/null ) read -sd R -r -t $[2 + attempt] ans /dev/null || ((x==-1||y==-1)); then ((attempt+=1 < max_attempt)) && continue fi break; done string out="" [[ $wanted =~ x ]] && out="$x" [[ $wanted =~ y ]] && out="${out:+$x }$y" [[ $out ]] && echo -n "$out" } declare -iga tabs sub get_tabs () { P "\r" tabs=() int pos=0 oldpos=0-1 while ((oldpos!=pos));do ((pos)) && tabs+=($pos) oldpos=pos P "\t" pos=$(getpos x) done P "\r" return 0 } # Note: this sub uses ability to _read_ tabstops as _proxy_ for setting them # (i.e. it makes no sense to be able to read them if you can't set them) sub test_tabset_ability () { string prompt="tty_tab:" int newcol=${#prompt}+1 P "\r$prompt" int mycol=$(getpos x) ((mycol && mycol==newcol)) && return 0 ## return OK { P " Term tabset ability not detected mycol=${mycol:-''}," P " promptlen=$newcol)\n"; } >&2 exit -1 } sub do_help_n_display_curtabs () { P " - set tab stop to N\r" intArray diffs; int last=1 cur i string eol="" get_tabs && { for ((i=0; i<${#tabs[@]}; ++i)); do cur=${tabs[i]} diffs[i]=cur-last last=cur done intArray reverse_tabs_set=() int prevtab=0-1 for ((i=${#diffs[@]}-2; i>0; --i)); do int thistab=${diffs[i]} if ((thistab!= prevtab)) ;then reverse_tabs_set+=($thistab) prevtab=thistab fi done P "current value: tty_tab " for ((i=${#reverse_tabs_set[@]}-1; i>=0; --i)); do P "%d " "${reverse_tabs_set[i]}"; done P "\r"; } get_tabs && { P "(from 1, tabs skip to column: " P "%s " "${tabs[@]}" P "\r\n" } } sub set_tabs () { int max_col=${1:-80} tabstop=${2:-"need a param for tabstop"} int tab=$tabstop pos=0 string str="" P $clrallts ## reset old tabs while ((++pos