blob: 13976a255431cfbb0fed5da820ae20f2761dafc8 [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001SYNOPSIS
Zesstra715ec202025-07-09 22:18:31 +02002 varargs string terminal_colour(string str, null|mapping|closure map,
Zesstrad59c3892019-11-28 20:53:39 +01003 int wrap, int indent)
MG Mud User88f12472016-06-24 23:31:02 +02004
Zesstra715ec202025-07-09 22:18:31 +02005DESCRIPTION
6 If <map> is given as a non-0 value, this efun expands all
7 colour-defines of the form "%^KEY%^" (see below for details) from the
8 input-string and replaces them by the apropriate values found
9 for the color-key specified by <map>.
MG Mud User88f12472016-06-24 23:31:02 +020010
Zesstra715ec202025-07-09 22:18:31 +020011 If <map> is a mapping, the entries queries have the
12 format "KEY" : "value", non-string contents are ignored with one
13 exception: if the mapping contains an entry 0:value, it is used
14 for all otherwise unrecognized keys. The value in this case can be
15 a string, or a closure. If it is a closure, it takes the key as
16 argument and has to return the replacement string.
MG Mud User88f12472016-06-24 23:31:02 +020017
Zesstra715ec202025-07-09 22:18:31 +020018 If <map> is given as a closure, it is called with the KEYs to
19 replace, and has to return the replacement string.
MG Mud User88f12472016-06-24 23:31:02 +020020
Zesstra715ec202025-07-09 22:18:31 +020021 The special keys "%^%^" and "%%^^" are always replaced with the
22 literal "%^".
MG Mud User88f12472016-06-24 23:31:02 +020023
Zesstra715ec202025-07-09 22:18:31 +020024 The parameters wrap and indent are both optional, if only wrap is
25 given then the str will be linewrapped at the column given with
26 wrap. If indent is given too, then all wrapped lines will be
27 indented with the number of blanks specified with indent.
MG Mud User88f12472016-06-24 23:31:02 +020028
Zesstra715ec202025-07-09 22:18:31 +020029 The wrapper itself ignores the length of the color macros and that
30 what they contain, it wraps the string based on the length of the
31 other chars inside. Therefore it is color-aware.
32
33 If <map> is given as 0, the efun does no colour-define detection
34 and replacement at all, but still does linewrapping and indentation
35 if requested. This way terminal_colour() doubles as a simple
36 line wrapping function, duplicating the functionality also
37 provided by sprintf("%-=s").
MG Mud User88f12472016-06-24 23:31:02 +020038
39
Zesstra715ec202025-07-09 22:18:31 +020040 KEY RECOGNITION STRATEGY
MG Mud User88f12472016-06-24 23:31:02 +020041
Zesstra715ec202025-07-09 22:18:31 +020042 As mentioned above, the special keys "%^%^" and "%%^^" are always
43 replaced with the literal "%^" and play no role in the following
44 considerations.
MG Mud User88f12472016-06-24 23:31:02 +020045
Zesstra715ec202025-07-09 22:18:31 +020046 The input string is supposed to follow this syntax:
MG Mud User88f12472016-06-24 23:31:02 +020047
Zesstra715ec202025-07-09 22:18:31 +020048 text { '%^' colorkey '%^' text } [ '%^' colorkey ]
MG Mud User88f12472016-06-24 23:31:02 +020049
Zesstra715ec202025-07-09 22:18:31 +020050 or in words: the efun splits up the string at every '%^' it finds
51 and then treats every second substring as color key.
MG Mud User88f12472016-06-24 23:31:02 +020052
MG Mud User88f12472016-06-24 23:31:02 +020053
Zesstra715ec202025-07-09 22:18:31 +020054 Note that this is different from the way MudOS treats the
55 input string. MudOS uses this syntax:
MG Mud User88f12472016-06-24 23:31:02 +020056
Zesstra715ec202025-07-09 22:18:31 +020057 key_or_text { '%^' key_or_text }
58
59 or in words: the MudOS efun splits the string at every '%^' and
60 then tries to treat every substring as color key. One can achieve
61 the MudOS behaviour with this LPC function:
MG Mud User88f12472016-06-24 23:31:02 +020062
Zesstrad59c3892019-11-28 20:53:39 +010063 string mudos_terminal_colour(string str, mapping ext, int w, int i) {
MG Mud User88f12472016-06-24 23:31:02 +020064 return terminal_colour("%^"+implode(explode(str, "%^")-({""})
65 ,"%^%^")
66 , ext, w, i);
67 }
68
Zesstra715ec202025-07-09 22:18:31 +020069EXAMPLES
MG Mud User88f12472016-06-24 23:31:02 +020070 mapping trans;
71 string str;
72
73 trans = ([ "GREEN" : "ansi-green", "RED" : "", "BLUE" : 1 ]);
74
75 str = terminal_colour( "%^GREEN%^ and %^RED%^ and %^BLUE%^", trans );
76
Zesstra715ec202025-07-09 22:18:31 +020077 This will result in str == "ansi-green and and BLUE"
MG Mud User88f12472016-06-24 23:31:02 +020078
Zesstra715ec202025-07-09 22:18:31 +020079 %^GREEN%^ is expanded to ansi-green because trans defines that,
80 %^RED%^ is stripped because trans defines that as "" and
81 %^BLUE%^ gets the %^'s removed because the contents of trans are
82 not valid (i.e. no string). The same would happen to %^DEFINES%^
83 where the key is not found inside the trans mapping.
MG Mud User88f12472016-06-24 23:31:02 +020084
Zesstra715ec202025-07-09 22:18:31 +020085 Caveat: to replace adjacent keys, use the efun like this:
MG Mud User88f12472016-06-24 23:31:02 +020086
87 str = terminal_colour( "%^GREEN%^%^RED%^", trans );
88
Zesstra715ec202025-07-09 22:18:31 +020089 A command like
MG Mud User88f12472016-06-24 23:31:02 +020090
91 str = terminal_colour( "%^GREEN%^RED%^", trans );
92
Zesstra715ec202025-07-09 22:18:31 +020093 will return the logical but sometimes unexpected "ansi-greenRED".
MG Mud User88f12472016-06-24 23:31:02 +020094
95
Zesstra715ec202025-07-09 22:18:31 +020096 Some words about wrapping:
MG Mud User88f12472016-06-24 23:31:02 +020097
Zesstra715ec202025-07-09 22:18:31 +020098 a string wrapped without indent would look like this:
MG Mud User88f12472016-06-24 23:31:02 +020099
Zesstra715ec202025-07-09 22:18:31 +0200100 "this is the first line\nand this is the second line"
MG Mud User88f12472016-06-24 23:31:02 +0200101
Zesstra715ec202025-07-09 22:18:31 +0200102 a string wrapped with indent 3 would look like:
MG Mud User88f12472016-06-24 23:31:02 +0200103
Zesstra715ec202025-07-09 22:18:31 +0200104 "this is the first line\n and this is the indented second one"
MG Mud User88f12472016-06-24 23:31:02 +0200105
Zesstra715ec202025-07-09 22:18:31 +0200106HISTORY
107 Efun idea and initial implementation taken from MudOS; the key
108 recognition strategy (including pure wrapping mode) was straightened
109 out in LDMud 3.2.8.
110 LDMud 3.2.9/3.3.58 added the use of closures to specify the colour
111 mappings.
112 LDMud 3.2.9/3.3.102 officialized the "%%^^" replacement pattern for
113 better MudOS compatibility.
MG Mud User88f12472016-06-24 23:31:02 +0200114
Zesstra715ec202025-07-09 22:18:31 +0200115SEE ALSO
MG Mud User88f12472016-06-24 23:31:02 +0200116 sprintf(E)