bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bash doesn't display tabs for tabs -- ignores tabstops.


From: Linda Walsh
Subject: Re: bash doesn't display tabs for tabs -- ignores tabstops.
Date: Wed, 15 May 2013 12:40:32 -0700
User-agent: Thunderbird


Eduardo Bustamante wrote:
> The cool thing about free software is that you're free to submit
> patches. Please consider that option, instead of ranting on what Chet
> should do.
> 

---
Ranting?  Chet seemed to indicate he wouldn't accept a patch.  If, OTOH,
he left that open... then, you are right.  I can decide.

FWIW, attached is latest revision of tty_tab that will set
tabstops to a different tabstop and w/o args will tell you current tab
stops.

Currently it will display variable tabs, but doesn't have the ablity
to set variable tabs.

Also, FWIW, some people commented on someone's bash programming style
in an earlier post.  I would say this typifies what I would consider
to be good style for bash -- it uses the features of bash to make
some things easier;  I can't say my coding styles are 'fixed'...
I often change them to see if things look  or work better.

Main changes --
        setting tabs for terms not sized at 80 columns and showing
a minimal line to reproduce the current settings.

#!/bin/bash  -u
#console_codes(4) man page... vt100/2 et && EMCA-48 standard
shopt -s expand_aliases extglob
alias int='declare -i'
alias sub='function'
alias array='declare -a'
alias intArray='declare -ia'
alias P=printf

P -v clrallts  "\x1b[3g"
P -v sts       "\033H"
P -v cr        "\013"
P -v cpr       "\x1b[6n"

sub getcols() {
        local sttyout="$(stty size </dev/tty)"
        int default_cols=80
        if [[ -n ${COLUMNS:-""} && $COLUMNS =~ ^[0-9]+$ ]]; then 
                default_cols=$COLUMNS; fi
        if [[ -z ${sttyout:-""} ]]; then 
                echo $default_cols; return 0;fi
        int cols="${sttyout#*\ }"
        echo $[cols<2?default_cols:cols]
        return 0
}

sub getpos () {
  local wanted=${1:-xy}
  local ans x y
 ( (sleep .01 && echo -en "\x1b[6n" >/dev/tty) & 2>/dev/null )  
  read  -sd R -r -t 2 ans </dev/tty; 
  declare ans=${ans:2}; x=${ans#*;}; y=${ans%;*} ; 
  declare -g out=""
  [[ $wanted =~ x ]] && out="$x"
  [[ $wanted =~ y ]] && out="${out:+$x }$y"
  [[ $out ]] && echo "$out"
}

declare -iga tabs

sub get_tabs () {
  P $cr
        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
}


sub test_tabset_ability () {
        prompt="tty_tab:"
        int newcol=${#prompt}+1
        echo -en "\r$prompt"
        mycol=$(getpos x)

        if [[ $mycol != $newcol ]]; then
                (
                        P " Term tabset ability not detected (out=$out 
mycol=$mycol,"
                        P " promptlen=$newcol)\n"
                ) >&2
                exit -1 
        fi
}

sub do_help_n_display_curtabs () {      
  echo -en " <n>   - set tab stop to N"
        echo -en "\r"
        intArray diffs;
        int last=1
        int cur i
        local eol=""
        get_tabs && {
                for ((i=0; i<address@hidden; ++i)); do
                        cur=${tabs[i]}
                        diffs[i]=cur-last
                        last=cur
                done    
                intArray reverse_tabs_set=()
                int prevtab=-1
                for ((address@hidden; 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 ((address@hidden; i>=0; --i)); do
                        P "%d " "${reverse_tabs_set[i]}"
                done
                P "\r";
        }
  get_tabs  && {
    P "(from 1, tabs skip to column: "
    P "%s " "address@hidden"
                P "\n"
  }
}

sub set_tabs () {
        int max_col=${1:-80}                    ## #columns on screen
        int tabstop=${2:-"need a param for tabstop"}
        int tab=$tabstop
        str=""
        int pos=0
        P $clrallts                                     ## reset old tabs
        while ((++pos<cols)) ;do        ## move across screen setting tabs
                str+=" "
                ((pos%tab)) || str+="$sts"
        done
        #echo -e "\033c"
        P "\r$str$cr"
        P ""
}


int cols=$(getcols)

test_tabset_ability                             ## exits if no ability


if (($#==0)) ; then
        do_help_n_display_curtabs
  exit 1
else
        set_tabs "$cols" "$@"
fi

# vim: ts=2


reply via email to

[Prev in Thread] Current Thread [Next in Thread]