]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/tput-initc
ncurses 6.2 - patch 20200404
[ncurses.git] / test / tput-initc
1 #!/bin/sh
2 ##############################################################################
3 # Copyright 2020 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.6 2020/02/02 23:34:34 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 "?? $*\n" >&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 cat $file |\
108 awk     -v opt_r=$opt_r \
109         -v opt_s=$opt_s \
110         -v colors=$colors \
111         -v myterm=$myterm '
112 BEGIN {
113         limit = 1000;
114         range = -1;
115         cramp = -1;
116         if ( colors == 88 ) {
117                 cramp = 80;
118         } else if ( colors = 256 ) {
119                 cramp = 232;
120         }
121 }
122 function scaled(n) {
123         return (n * 1000)/limit;
124 }
125
126 /^scale:[0-9]+/{
127         sub("^scale:","",$0);
128         limit = $0;
129 }
130
131 /^[0-9]+:/{
132         sub(":","",$1);
133         item = $1 + 0;
134         if (range < item) {
135                 range = item;
136         }
137         params[$1] = sprintf ("%d %d %d", scaled($2),scaled($3),scaled($4));
138 }
139 END {
140         for (n = 0; n <= range; ++n) {
141                 m = n;
142                 if ( opt_r == "yes" ) {
143                         if ( colors <= 16 ) {
144                                 m = range - n;
145                         } else if ( ( opt_s == "yes" ) && ( n < 16 ) ) {
146                                 m = 15 - n;
147                         } else if ( n >= cramp ) {
148                                 m = cramp + colors - 1 - n;
149                         } else {
150                                 m = 16 + cramp - 1 - n;
151                         }
152                 }
153                 printf "tput -T%s initc %d %s\n", myterm, m, params[n];
154         }
155 }
156 ' |sh -