Create your own text control codes: Difference between revisions

Content added Content deleted
(Added Wren)
(→‎{{header|Wren}}: Added more effects.)
Line 96: Line 96:
When doing RC tasks, I often use methods in my own ''Wren-fmt'' module which does most of what C's ''printf'' statement does and other things besides. Although I could add anything I like to that, it's already more than 800 lines long and so I don't think it would be appropriate to patch it for the purposes of this task.
When doing RC tasks, I often use methods in my own ''Wren-fmt'' module which does most of what C's ''printf'' statement does and other things besides. Although I could add anything I like to that, it's already more than 800 lines long and so I don't think it would be appropriate to patch it for the purposes of this task.


What I've done instead is to create a special class called ''Sgr'' (Select graphic rendition) which adds special effects when printing text to terminals which support ANSI escape sequences. For demonstration purposes, I've just added methods which color (''Sgr.c'') or underline (''Sgr.u'') a given piece of text (and then restore the default attributes) though several other effects could be added as well. ''System.print'' can then interpolate these method calls.
What I've done instead is to create a special class called ''Sgr'' (Select graphic rendition) which adds special effects when printing text to terminals which support ANSI escape sequences. The effects supported are: color, bold, faint, italic, underline, wink, strike and overline each of which is represented by a method consisting of its initial letter.

When these methods complete, they restore the terminal attributes to what they were before.
''System.print'' can now interpolate these method calls.


Although it would be possible to abbreviate the color arguments passed to ''Sgr.c'', I haven't done so because I didn't think it would be very user friendly.
Although it would be possible to abbreviate the color arguments passed to ''Sgr.c'', I haven't done so because I didn't think it would be very user friendly.
Line 102: Line 105:
// capitalize the initial letter for bright colors
// capitalize the initial letter for bright colors
static init_() {
static init_() {
__cm = { "black": 30, "red" : 31, "green": 32, "yellow": 33,
__cm = { "black": 30, "red" : 31, "green": 32, "yellow": 33,
"blue" : 34, "magenta": 35, "cyan" : 36, "white" : 37,
"blue" : 34, "magenta": 35, "cyan" : 36, "white" : 37,
"Black": 90, "Red" : 91, "Green": 92, "Yellow": 93,
"Black": 90, "Red" : 91, "Green": 92, "Yellow": 93,
Line 110: Line 113:
}
}


static c(fore, back, text) {
static c(fore, back, text) { // colorize
if (!__cm) init_()
if (!__cm) init_()
var fcn = __cm[fore]
var fcn = __cm[fore]
Line 121: Line 124:
}
}



static u(text) { "\e[4m%(text)\e[24m" }
static b(text) { "\e[1m%(text)\e[22m" } // bold

static f(text) { "\e[2m%(text)\e[22m" } // faint

static i(text) { "\e[3m%(text)\e[23m" } // italic

static u(text) { "\e[4m%(text)\e[24m" } // underline

static w(text) { "\e[5m%(text)\e[25m" } // wink (or blink)

static r(text) { "\e[7m%(text)\e[27m" } // reverse video

static s(text) { "\e[9m%(text)\e[29m" } // strike out

static o(text) { "\e[53m%(text)\e[55m" } // overline

}
}


System.print("%(Sgr.c("red", "green", "This")) is %(Sgr.u("just")) a %(Sgr.c("yellow", "blue", "test")).")</lang>
System.print("%(Sgr.c("red", "green", "This")) is a color %(Sgr.c("yellow", "blue", "test")).")
System.print("\nOther effects:")
var effects = [
Sgr.b("Bold"), Sgr.f("Faint"), Sgr.i("Italic"), Sgr.u("Underline"),
Sgr.w("Wink"), Sgr.r("Reverse"), Sgr.s("Strike"), Sgr.o("Overline")
]
System.print(effects.join(", "))</lang>