it2setcolor (2813B)
1 #!/bin/bash 2 open=0 3 4 # tmux requires unrecognized OSC sequences to be wrapped with DCS tmux; 5 # <sequence> ST, and for all ESCs in <sequence> to be replaced with ESC ESC. It 6 # only accepts ESC backslash for ST. 7 function print_osc() { 8 if [[ $TERM == screen* ]] ; then 9 printf "\033Ptmux;\033\033]" 10 else 11 printf "\033]" 12 fi 13 } 14 15 # More of the tmux workaround described above. 16 function print_st() { 17 if [[ $TERM == screen* ]] ; then 18 printf "\a\033\\" 19 else 20 printf "\a" 21 fi 22 } 23 24 # set_color key rgb 25 function set_color() { 26 print_osc 27 printf '1337;SetColors=%s=%s' "$1" "$2" 28 print_st 29 } 30 31 function error() { 32 echo "ERROR: $*" 1>&2 33 } 34 35 function show_help() { 36 if [ $open = 1 ]; then 37 print_st 38 fi 39 echo "Usage" 40 echo "" 41 echo "1) To set a specific color to an RGB value:" 42 echo " it2setcolor name color [name color...]" 1>& 2 43 echo "For example:" 44 echo " it2setcolor fg fff" 45 echo "" 46 echo "name is one of:" 47 echo " fg bg bold link selbg selfg curbg curfg underline tab" 48 echo " black red green yellow blue magenta cyan white" 49 echo " br_black br_red br_green br_yellow br_blue br_magenta br_cyan br_white" 50 echo "" 51 echo "color is of the format:" 52 echo " RGB (three hex digits, like fff)" 53 echo " RRGGBB (six hex digits, like f0f0f0)" 54 echo " cs:RGB (cs is a color space name)" 55 echo " cs:RRGGBB (cs is a color space name)" 56 echo "" 57 echo " The color space names accepted in the color are:" 58 echo " srgb (sRGB, the default if none is specified)" 59 echo " rgb (Apple's old device-independent colorspace)" 60 echo " p3 (Apple's fancy large-gamut colorspace)" 61 echo "" 62 echo "2) To switch to a named color preset:" 63 echo " it2setcolor preset name" 64 echo "For example:" 65 echo " it2setcolor preset 'Light Background'" 66 echo "" 67 echo "3) To reset the current tab's color to the system default:" 68 echo " it2setcolor tab default" 69 } 70 71 # Show help if no arguments and no stdin. 72 if [ $# -eq 0 ]; then 73 show_help 74 exit 75 fi 76 77 # Look for command line flags. 78 while [ $# -gt 0 ]; do 79 case "$1" in 80 -h|--h|--help) 81 show_help 82 exit 83 ;; 84 -*) 85 error "Unknown option flag: $1" 86 show_help 87 exit 1 88 ;; 89 *) 90 if [ $# -lt 2 ]; then 91 show_help 92 exit 93 fi 94 if [ $open = 0 ]; then 95 open=1 96 print_osc 97 printf '1337;SetColors=' 98 else 99 printf "," 100 fi 101 # name is not checked for validity because we'd like this to work with future colors, too. 102 printf "%s=%s" "$1" "$2" 103 shift 104 ;; 105 esac 106 shift 107 done 108 109 if [ $open = 1 ]; then 110 print_st 111 else 112 show_help 113 fi 114 115 exit 0 116