sgrPushPop.pl revision f2e35a3a
1#!/usr/bin/env perl
2# $XTermId: sgrPushPop.pl,v 1.10 2018/08/02 21:09:46 tom Exp $
3# -----------------------------------------------------------------------------
4# this file is part of xterm, contributed by Dan Thompson
5#
6# Copyright 2018 by Thomas E. Dickey
7#
8#                         All Rights Reserved
9#
10# Permission is hereby granted, free of charge, to any person obtaining a
11# copy of this software and associated documentation files (the
12# "Software"), to deal in the Software without restriction, including
13# without limitation the rights to use, copy, modify, merge, publish,
14# distribute, sublicense, and/or sell copies of the Software, and to
15# permit persons to whom the Software is furnished to do so, subject to
16# the following conditions:
17#
18# The above copyright notice and this permission notice shall be included
19# in all copies or substantial portions of the Software.
20#
21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
25# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28#
29# Except as contained in this notice, the name(s) of the above copyright
30# holders shall not be used in advertising or otherwise to promote the
31# sale, use or other dealings in this Software without prior written
32# authorization.
33# -----------------------------------------------------------------------------
34
35use strict;
36use warnings;
37
38# This script demonstrates the utility of the [non-standard] SGR push/pop
39# control sequences. The ability to save (on a stack) the current SGR
40# attributes (fg/bg color, bold, etc.) and then later restore them allows
41# [SGR-containing] text from independent sources to be easily composed
42# together, without requiring any sort of global coordination.
43
44our (
45    $pushSgr,     $popSgr,     $lib1Fmt,
46    $lib2Fmt,     $redOnBlack, $blackOnYellow,
47    $blueOnGreen, $bg,         $fg
48);
49
50$pushSgr = "\x1b[#{";
51$popSgr  = "\x1b[#}";
52
53# lib1Fmt represents a "top-level" program, and controls the current "ambient"
54# fg/bg colors.
55$lib1Fmt = "\x1b[48;5;%sm\x1b[38;5;%sm%03.3d/%03.3d ";
56
57# lib2Fmt represents some intermediate library. Note that it contains no SGR
58# control sequences at all.
59$lib2Fmt = "Test stack: %s, %s, %s";
60
61# The following represent individual bits of colorized data that come from
62# other, "leaf-level" libraries.
63$redOnBlack    = $pushSgr . "\x1b[1;31m\x1b[40m" . "redOnBlack" . $popSgr;
64$blackOnYellow = $pushSgr . "\x1b[30m\x1b[4;43m" . "blackOnYellow" . $popSgr;
65$blueOnGreen   = $pushSgr . "\x1b[34m\x1b[42m" . "blueOnGreen" . $popSgr;
66
67printf $pushSgr;
68printf "\x1b[40;37mSetting ambient colors to white-on-black\n";
69printf $pushSgr;
70
71for ( $bg = 0 ; $bg < 16 ; $bg++ ) {
72    for ( $fg = 0 ; $fg < 16 ; $fg++ ) {
73        printf $pushSgr;
74        printf $lib1Fmt, $fg, $bg, $fg, $bg;
75        printf $lib2Fmt, $redOnBlack, $blackOnYellow, $blueOnGreen;
76        print " something else";
77        printf $popSgr;    # keep the newline from bleeding color
78        print "\n";
79    }
80    print "\n";
81}
82printf $popSgr;
83printf "The ambient colors should still be white-on-black.\n";
84printf $popSgr;
85printf "Now we should be back to whatever it was before we got here.\n";
86
871;
88