Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

jvscode [[at]] fastmail [[dot]] fm

Automatically resize Terminal window

For more information see Terminal window commands, Discover tput and Apple Shell Scripting Primer -> ANSI Escape Sequence Tables.

tput lines
tput cols

echo $LINES
echo $COLUMNS

stty size
stty size | awk '{print $1}'    # lines
stty size | awk '{print $NF}'   # columns

stty size | cut -d" " -f1   # lines
stty size | cut -d" " -f2   # columns 

stty -a | awk '/rows/ {print $4}'      # lines
stty -a | awk '/columns/ {print $6}'   # columns

stty -a | sed -E -n -e 's/^.*[^[:digit:]]([[:digit:]]+)[[:space:]]+rows;.*$/\1/p;q;'
stty -a | sed -E -n -e 's/^.*[^[:digit:]]([[:digit:]]+)[[:space:]]+columns;.*$/\1/p;q;'



# automatically resize the Terminal window if it gets smaller than the default size

# positive integer test (including zero)
function positive_int() { return $(test "$@" -eq "$@" > /dev/null 2>&1 && test "$@" -ge 0 > /dev/null 2>&1); }


# resize the Terminal window
function sizetw() { 
   if [[ $# -eq 2 ]] && $(positive_int "$1") && $(positive_int "$2"); then 
      printf "\e[8;${1};${2};t"
      return 0
   fi
   return 1
}


# the default Terminal window size: 26 lines and 107 columns
sizetw 26 107


# automatically adjust Terminal window size
function defaultwindow() {

   DEFAULTLINES=26
   DEFAULTCOLUMNS=107

   if [[ $(/usr/bin/tput lines) -lt $DEFAULTLINES ]] && [[ $(/usr/bin/tput cols) -lt $DEFAULTCOLUMNS ]]; then
      sizetw $DEFAULTLINES $DEFAULTCOLUMNS
   elif [[ $(/usr/bin/tput lines) -lt $DEFAULTLINES ]]; then
      sizetw $DEFAULTLINES $(/usr/bin/tput cols)
   elif [[ $(/usr/bin/tput cols) -lt $DEFAULTCOLUMNS ]]; then
      sizetw $(/usr/bin/tput lines) $DEFAULTCOLUMNS
   fi

   return 0
}


# SIGWINCH is the window change signal
trap defaultwindow SIGWINCH    


sizetw 26 70
sizetw 10 107
sizetw 4 15

Testing Terminal RGB color combinations on Mac OS X

# change the background color of the current Terminal window
# for the osascript code see the comment by John Prevost at http://norman.walsh.name/2007/08/27/macProgress
# cf. also http://nslog.com/2006/11/02/terminal_color_changing_via_applescript and
# http://www.red-sweater.com/blog/220/random-color-terminal

open -a 'DigitalColor Meter'

function bgcol() { 
   declare BGCOLOR="$1"

/usr/bin/osascript <<__END__
   tell application "Terminal"
      activate
      with timeout of 10 seconds
         do script
         tell (first window whose name contains " ")
            set background color to "${BGCOLOR}"
        end tell
      end timeout
   end tell
__END__

   return 0 
}

bgcol grey
bgcol blue
bgcol white
bgcol red
bgcol orange
bgcol green
bgcol black
bgcol brown
bgcol cyan
bgcol purple
bgcol magenta


function bgcol2() { 
   declare BGCOLOR="$1"

/usr/bin/osascript <<__END__
  set RGBbg to ${BGCOLOR} as RGB color

  tell application "Terminal"
      activate
      with timeout of 10 seconds
         do script
         tell (first window whose name contains " ")
            set background color to RGBbg
        end tell
      end timeout
   end tell
__END__

  return 0 
}

bgcol2 '{57212, 56949, 40762}'
bgcol2 '{60000, 50000, 40000}'
bgcol2 '{60000, 40000, 40000}'
bgcol2 '{30810, 26728, 12934}'
bgcol2 '{24829, 27580, 33169}'
bgcol2 '{2570, 33924, 46774}'
bgcol2 '{50719, 61018, 52220}'
bgcol2 '{65123, 45636, 24237}'


function bgcol3() { 

#declare r=$[$RANDOM + 30000]     # $RANDOM generates a random integer between 0 and 32767 (cf. man bash)
#declare g=$[$RANDOM + 25000]
#declare b=$[$RANDOM + 15000]

declare nums=$(ruby -e 'ar=[]; 3.times { ar << Kernel.rand(65535) }; puts ar.join(" ")')
#declare nums=$(ruby -e 'ar=[]; n = 15_000; 3.times { ar << Kernel.rand(n) + n; n+=8000 }; puts ar.join(" ")')

declare r=$(printf "%s" "$nums" | awk '{print $1}')
declare g=$(printf "%s" "$nums" | awk '{print $2}')
declare b=$(printf "%s" "$nums" | awk '{print $3}')

printf "bgcol: ${r}, ${g}, ${b}\n"

/usr/bin/osascript <<__END__
set RGBbg to {${r}, ${g}, ${b}} as RGB color

  tell application "Terminal"
      activate
      with timeout of 10 seconds
         do script
         tell (first window whose name contains " ")
            set background color to RGBbg
        end tell
      end timeout
   end tell
__END__

  return 0 
}

for (( i=0; i<=20; i++ )); do bgcol3; sleep 3; done



# change the text color of the current Terminal window

function fgcol() { 
   declare FGCOLOR="$1"

/usr/bin/osascript <<__END__
   tell application "Terminal"
      activate
      with timeout of 10 seconds
         do script
         tell (first window whose name contains " ")
            set normal text color to "${FGCOLOR}"
        end tell
      end timeout
   end tell
__END__

   return 0 
}

fgcol grey
fgcol blue
fgcol white
fgcol red
fgcol orange
fgcol green
fgcol black
fgcol brown
fgcol cyan
fgcol purple
fgcol magenta


function defaultcolors() { 
   fgcol black
   bgcol white
   return 0 
}

defaultcolors


function fgcol2() { 

#declare r=$[$RANDOM + 30000]
#declare g=$[$RANDOM + 10000]
#declare b=$[$RANDOM]

declare nums=$(ruby -e 'ar=[]; 3.times { ar << Kernel.rand(65535) }; puts ar.join(" ")')
declare r=$(printf "%s" "$nums" | awk '{print $1}')
declare g=$(printf "%s" "$nums" | awk '{print $2}')
declare b=$(printf "%s" "$nums" | awk '{print $3}')

printf "fgcol: ${r}, ${g}, ${b}\n"

/usr/bin/osascript <<__END__
set RGBfg to {${r}, ${g}, ${b}} as RGB color

  tell application "Terminal"
      activate
      with timeout of 10 seconds
         do script
         tell (first window whose name contains " ")
            set normal text color to RGBfg
            --set bold text color to RGBfg
        end tell
      end timeout
   end tell
__END__

  return 0 
}


for (( i=0; i<=20; i++ )); do fgcol2; sleep 3; done

for (( i=0; i<=20; i++ )); do fgcol2; sleep 3; bgcol3; sleep 5; printf "\n"; done