Debian Codegraph @

test/tput-initc

raw symbols SCIP
1 #!/bin/sh
2 ##############################################################################
3 # Copyright 2020,2025 Thomas E. Dickey #
4 # Copyright 2016 Free Software Foundation, Inc. #
5 # #
6 # Permission is hereby granted, free of charge, to any person obtaining a #
7 # copy of this software and associated documentation files (the "Software"), #
8 # to deal in the Software without restriction, including without limitation #
9 # the rights to use, copy, modify, merge, publish, distribute, distribute #
10 # with modifications, sublicense, and/or sell copies of the Software, and to #
11 # permit persons to whom the Software is furnished to do so, subject to the #
12 # following conditions: #
13 # #
14 # The above copyright notice and this permission notice shall be included in #
15 # all copies or substantial portions of the Software. #
16 # #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL #
20 # THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER #
23 # DEALINGS IN THE SOFTWARE. #
24 # #
25 # Except as contained in this notice, the name(s) of the above copyright #
26 # holders shall not be used in advertising or otherwise to promote the sale, #
27 # use or other dealings in this Software without prior written #
28 # authorization. #
29 ##############################################################################
30 # $Id: tput-initc,v 1.7 2025/06/14 14:06:55 tom Exp $
31 # Some of the ".dat" files in ncurses' test-directory give r/g/b numbers for
32 # default palettes of xterm and Linux console. This script reads the numbers
33 # and (assuming the same or compatible terminal) uses tput to (re)initialize
34 # the palette using those numbers.
35
36 failed() {
37 printf "?? %s\n" "$1" >&2
38 exit 1
39 }
40
41 usage() {
42 cat >&2 <<-EOF
43 usage: $0 [-r] [-s] [palette-data]
44
45 Use this script with a palette data-file to (re)initialize colors with
46 tput. This script assumes arrangements for 16-, 88- and 256-colors
47 like the xterm 88colors2.pl and 256colors2.pl scripts.
48
49 Options:
50 -r reverse palette
51 -s reverse system colors (first 16 if more than 16 colors)
52 EOF
53 exit 1
54 }
55
56 opt_r=no
57 opt_s=no
58
59 while getopts "rs" option "$@"
60 do
61 case $option in
62 (r)
63 opt_r=yes
64 ;;
65 (s)
66 opt_s=yes
67 ;;
68 (*)
69 usage
70 ;;
71 esac
72 done
73 shift "$(expr "$OPTIND" - 1)"
74
75 if [ $# = 1 ]
76 then
77 file=$1
78 elif [ $# = 0 ]
79 then
80 file=$TERM.dat
81 else
82 failed "expected one parameter or none"
83 fi
84
85 if [ ! -f "$file" ]
86 then
87 if [ -f "$file.dat" ]
88 then
89 file="$file.dat"
90 else
91 failed "no such file: $file"
92 fi
93 fi
94
95 myterm=${file%%.dat}
96 colors=$(tput -T "$myterm" colors 2>/dev/null)
97 if [ "${colors:-0}" -le 0 ]
98 then
99 myterm=${myterm%%-color}
100 colors=$(tput -T "$myterm" colors 2>/dev/null)
101 fi
102 if [ "${colors:-0}" -le 0 ]
103 then
104 failed "terminal $myterm does not support color"
105 fi
106
107 awk -v opt_r=$opt_r \
108 -v opt_s=$opt_s \
109 -v colors="$colors" \
110 -v myterm="$myterm" < "$file" '
111 BEGIN {
112 limit = 1000;
113 range = -1;
114 cramp = -1;
115 if ( colors == 88 ) {
116 cramp = 80;
117 } else if ( colors = 256 ) {
118 cramp = 232;
119 }
120 }
121 function scaled(n) {
122 return (n * 1000)/limit;
123 }
124
125 /^scale:[0-9]+/{
126 sub("^scale:","",$0);
127 limit = $0;
128 }
129
130 /^[0-9]+:/{
131 sub(":","",$1);
132 item = $1 + 0;
133 if (range < item) {
134 range = item;
135 }
136 params[$1] = sprintf ("%d %d %d", scaled($2),scaled($3),scaled($4));
137 }
138 END {
139 for (n = 0; n <= range; ++n) {
140 m = n;
141 if ( opt_r == "yes" ) {
142 if ( colors <= 16 ) {
143 m = range - n;
144 } else if ( ( opt_s == "yes" ) && ( n < 16 ) ) {
145 m = 15 - n;
146 } else if ( n >= cramp ) {
147 m = cramp + colors - 1 - n;
148 } else {
149 m = 16 + cramp - 1 - n;
150 }
151 }
152 printf "tput -T%s initc %d %s\n", myterm, m, params[n];
153 }
154 }
155 ' |sh -