MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 1 | SYNOPSIS |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 2 | varargs string terminal_colour(string str, null|mapping|closure map, |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 3 | int wrap, int indent) |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 4 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 5 | DESCRIPTION |
| 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 10 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 11 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 17 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 18 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 20 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 21 | The special keys "%^%^" and "%%^^" are always replaced with the |
| 22 | literal "%^". |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 23 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 24 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 28 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 29 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 38 | |
| 39 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 40 | KEY RECOGNITION STRATEGY |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 41 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 42 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 45 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 46 | The input string is supposed to follow this syntax: |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 47 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 48 | text { '%^' colorkey '%^' text } [ '%^' colorkey ] |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 49 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 50 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 52 | |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 53 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 54 | Note that this is different from the way MudOS treats the |
| 55 | input string. MudOS uses this syntax: |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 56 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 57 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 62 | |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 63 | string mudos_terminal_colour(string str, mapping ext, int w, int i) { |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 64 | return terminal_colour("%^"+implode(explode(str, "%^")-({""}) |
| 65 | ,"%^%^") |
| 66 | , ext, w, i); |
| 67 | } |
| 68 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 69 | EXAMPLES |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 70 | 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 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 77 | This will result in str == "ansi-green and and BLUE" |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 78 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 79 | %^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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 84 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 85 | Caveat: to replace adjacent keys, use the efun like this: |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 86 | |
| 87 | str = terminal_colour( "%^GREEN%^%^RED%^", trans ); |
| 88 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 89 | A command like |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 90 | |
| 91 | str = terminal_colour( "%^GREEN%^RED%^", trans ); |
| 92 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 93 | will return the logical but sometimes unexpected "ansi-greenRED". |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 94 | |
| 95 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 96 | Some words about wrapping: |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 97 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 98 | a string wrapped without indent would look like this: |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 99 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 100 | "this is the first line\nand this is the second line" |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 101 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 102 | a string wrapped with indent 3 would look like: |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 103 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 104 | "this is the first line\n and this is the indented second one" |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 105 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 106 | HISTORY |
| 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 114 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 115 | SEE ALSO |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 116 | sprintf(E) |