Reorder Tabs -> bash script

Home Forums General Programming Reorder Tabs -> bash script

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #83689
    higgy
    Participant
    • Topics: 9
    • Replies: 11
    • Total: 20

    hi,
    a little bash script for reordering tabs (moving a tab from one position to another)

    <youscriptname> -f <panelfile> -t <TabName> -o <oldTabIndex> -n <newTabIndex>
    where panelfile is the .panel xml file

    
    #! c:/bin/bash.exe</blockquote>
    
    function movetab() {
      TAB=$1
      opos=$2
      npos=$3
     
      _obuff="$buff"
      
      [ $npos -eq $opos ]  && { echo "cannot copy onto itself" ; exit }
      [ $npos -gt $opos ]  && SIGN=1 
      [ $npos -lt $opos ]  && SIGN=-1 
    
      echo "tab from -> to"
    
      s=0  ; _opos=$opos  ; _npos=-1 ;
      printf "     %2d  ->  %2s\n" $_opos $_npos
    
      _nbuff=$( echo "$_obuff" | sed \
                "s/componentTabId=\"${_opos}\" componentTabName=\"$TAB\"/componentTabId=\"${_npos}\" componentTabName=\"$TAB\"/g; 
                s/componentTabName=\"$TAB\" componentTabId=\"${_opos}\"/componentTabName=\"$TAB\" componentTabId=\"${_npos}\"/g;"
              ) 
      _obuff="$_nbuff"
    
      s=1; while [ $s -le $(( SIGN*(npos-opos)  )) ]  ; do  
        _opos=$(( opos + SIGN*s )) ; _npos=$(( opos + SIGN*(s-1) )) ;
        printf "     %2d  ->  %2s\n" $_opos $_npos
    
        _nbuff=$( echo "$_obuff" | sed \
                  "s/componentTabId=\"${_opos}\" componentTabName=\"$TAB\"/componentTabId=\"${_npos}\" componentTabName=\"$TAB\"/g; 
                   s/componentTabName=\"$TAB\" componentTabId=\"${_opos}\"/componentTabName=\"$TAB\" componentTabId=\"${_npos}\"/g;"
                ) 
        _obuff="$_nbuff"
        s=$((s+1))
      done
    
      _opos=-1 ;   _npos=$npos 
      printf "     %2d  ->  %2s\n" $_opos $_npos
      
      _nbuff=$( echo "$_obuff" | sed \
                "s/componentTabId=\"${_opos}\" componentTabName=\"$TAB\"/componentTabId=\"${_npos}\" componentTabName=\"$TAB\"/g; 
                s/componentTabName=\"$TAB\" componentTabId=\"${_opos}\"/componentTabName=\"$TAB\" componentTabId=\"${_npos}\"/g;"
              ) 
    
      echo "$_nbuff" > $outfile 
    }
    
    function freeordertab() {
    ## modify to demands:
      TAB=$1
      echo "$buff" \
        | sed \
        "s/componentTabId=\"0\"[ ][ ]*componentTabName=\"$TAB\"/componentTabId=\"-1\" componentTabName=\"$TAB\"/g; 
         s/componentTabName=\"$TAB\"[ ][ ]*componentTabId=\"0\"/componentTabName=\"$TAB\" componentTabId=\"-1\"/g;"\
        | sed \
        "s/componentTabId=\"1\"[ ][ ]*componentTabName=\"$TAB\"/componentTabId=\"0\" componentTabName=\"$TAB\"/g; 
         s/componentTabName=\"$TAB\"[ ][ ]*componentTabId=\"1\"/componentTabName=\"$TAB\" componentTabId=\"0\"/g;"\
        | sed \ 
        "s/componentTabId=\"2\"[ ][ ]*componentTabName=\"$TAB\"/componentTabId=\"1\" componentTabName=\"$TAB\"/g; 
         s/componentTabName=\"$TAB\"[ ][ ]*componentTabId=\"2\"/componentTabName=\"$TAB\" componentTabId=\"1\"/g;"\
        | sed \ 
        "s/componentTabId=\"-1\"[ ][ ]*componentTabName=\"$TAB\"/componentTabId=\"1\" componentTabName=\"$TAB\"/g; 
         s/componentTabName=\"$TAB\"[ ][ ]*componentTabId=\"-1\"/componentTabName=\"$TAB\" componentTabId=\"1\"/g;"\
      >  $outfile
    }
    
    # MAIN
    while getopts f:t:o:n: option ; do
      case "$option" in
        f) infile="$OPTARG"   ;;
        t) TabName="$OPTARG"  ;;
        o) OldPos="$OPTARG"   ;;
        n) NewPos="$OPTARG"   ;;
        *) echo "usage: $0  -f <panelfile> -t <TabName> -o <oldTabIndex> -n <newTabIndex>" 
           echo
           echo "NOTE: You get the TabName and TabIndexes from fields 'name' and 'uiTabsTabIndex' from the Tab-modulator." 
           echo "      Normally, TabIndexes start at 0 (=1st tab)"
           echo "      Only usable with uiTabsTabIndex-ex in continuous order." 
           echo "      For not continuous order hack function freeordertab() in this script ;)" 
           exit
           ;;
      esac
    done
    
    [ -z "$infile" ] || [ -z "$TabName" ]  || [ -z "$OldPos" ] || [ -z "$NewPos" ]  && { 
       echo "usage: $0  -f <panelfile> -t <TabName> -o <oldTabIndex> -n <newTabIndex>" 
       exit 
    }
    
    [ ! -f "$infile" ] && { 
      echo "file $0 not existing" ; exit
    } 
    
    echo
    echo "File: $infile"
    echo "Tabulator $TabName"
    echo "Moving tab from $OldPos to $NewPos ..."
    echo
    
    # this is against crashing of old GnuBash4Win
    touch __xyxyxy ; rm -f __xyxyxy 
    
    outfile=${infile}_new.panel
    rm -f $outfile
    
    # make 'one straight line of text"
    buff=$( cat $infile | awk '{printf ("%s", $0) }' )
    
    movetab  $TabName $OldPos $NewPos
    # freeordertab $TabName
    
    echo
    echo "outfile: $outfile"  
    echo
    echo "Now load the panel, check all modulators and rename the TABS"
    echo
    • This topic was modified 4 years ago by higgy.
Viewing 1 post (of 1 total)
  • The forum ‘Programming’ is closed to new topics and replies.
There is currently 0 users and 72 guests online
No users are currently active
Forum Statistics
Threads: 2,495, Posts: 17,374, Members: 77,605
Most users ever online was 12 on January 22, 2019 3:47 pm
Ctrlr