Jump to content

Create your own text control codes: Difference between revisions

→‎{{header|Wren}}: Added more effects.
(Added Wren)
(→‎{{header|Wren}}: Added more effects.)
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.
 
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. ForThe demonstrationeffects purposes,supported I'veare: justcolor, addedbold, methodsfaint, which color (''Sgr.c'') oritalic, underline, (''Sgr.u'')wink, astrike given piece of text (and thenoverline restoreeach theof defaultwhich attributes)is thoughrepresented severalby othera effectsmethod couldconsisting beof addedits as well. ''System.print'' can then interpolate these methodinitial callsletter.
 
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.
Line 102 ⟶ 105:
// capitalize the initial letter for bright colors
static init_() {
__cm = { "black": 30, "red" : 31, "green": 32, "yellow": 33,
"blue" : 34, "magenta": 35, "cyan" : 36, "white" : 37,
"Black": 90, "Red" : 91, "Green": 92, "Yellow": 93,
Line 110 ⟶ 113:
}
 
static c(fore, back, text) { // colorize
if (!__cm) init_()
var fcn = __cm[fore]
Line 121 ⟶ 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 acolor %(Sgr.c("yellow", "blue", "test")).")</lang>
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>
9,483

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.