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