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