Skip to content

Commit d31750c

Browse files
red-001nerzhul
authored andcommittedMar 17, 2017
Give CSM access to use core.colorize() (#5113)
1 parent 7b74f04 commit d31750c

File tree

4 files changed

+74
-33
lines changed

4 files changed

+74
-33
lines changed
 

‎builtin/common/misc_helpers.lua

+32
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,35 @@ if INIT == "client" or INIT == "mainmenu" then
638638
return core.formspec_escape(fgettext_ne(text, ...))
639639
end
640640
end
641+
642+
-- Client-sided mods don't have access to getbool
643+
if core.setting_getbool and core.setting_getbool("disable_escape_sequences") then
644+
645+
function core.get_color_escape_sequence(color)
646+
return ""
647+
end
648+
649+
function core.get_background_escape_sequence(color)
650+
return ""
651+
end
652+
653+
function core.colorize(color, message)
654+
return message
655+
end
656+
657+
else
658+
659+
local ESCAPE_CHAR = string.char(0x1b)
660+
function core.get_color_escape_sequence(color)
661+
return ESCAPE_CHAR .. "(c@" .. color .. ")"
662+
end
663+
664+
function core.get_background_escape_sequence(color)
665+
return ESCAPE_CHAR .. "(b@" .. color .. ")"
666+
end
667+
668+
function core.colorize(color, message)
669+
return core.get_color_escape_sequence(color) .. message .. core.get_color_escape_sequence("#ffffff")
670+
end
671+
672+
end

‎builtin/game/misc.lua

-31
Original file line numberDiff line numberDiff line change
@@ -170,37 +170,6 @@ function core.http_add_fetch(httpenv)
170170
return httpenv
171171
end
172172

173-
if minetest.setting_getbool("disable_escape_sequences") then
174-
175-
function core.get_color_escape_sequence(color)
176-
return ""
177-
end
178-
179-
function core.get_background_escape_sequence(color)
180-
return ""
181-
end
182-
183-
function core.colorize(color, message)
184-
return message
185-
end
186-
187-
else
188-
189-
local ESCAPE_CHAR = string.char(0x1b)
190-
function core.get_color_escape_sequence(color)
191-
return ESCAPE_CHAR .. "(c@" .. color .. ")"
192-
end
193-
194-
function core.get_background_escape_sequence(color)
195-
return ESCAPE_CHAR .. "(b@" .. color .. ")"
196-
end
197-
198-
function core.colorize(color, message)
199-
return core.get_color_escape_sequence(color) .. message .. core.get_color_escape_sequence("#ffffff")
200-
end
201-
202-
end
203-
204173
function core.close_formspec(player_name, formname)
205174
return minetest.show_formspec(player_name, formname, "")
206175
end

‎clientmods/preview/init.lua

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ core.register_chatcommand("dump", {
4040
end,
4141
})
4242

43+
core.register_chatcommand("colorize_test", {
44+
func = function(param)
45+
return true, core.colorize("red", param)
46+
end,
47+
})
48+
4349
core.register_chatcommand("test_node", {
4450
func = function(param)
4551
core.display_chat_message(dump(core.get_node({x=0, y=0, z=0})))

‎doc/client_lua_api.md

+36-2
Original file line numberDiff line numberDiff line change
@@ -787,12 +787,12 @@ Call these functions only at load time!
787787
extra arguments and return the result
788788
* `fgettext(string, ...)` : returns string
789789
* same as fgettext_ne(), but calls core.formspec_escape before returning result
790-
* `show_formspec(formname, formspec)` : returns true on success
791-
* Shows a formspec to the player
792790

793791
### UI
794792
* `minetest.ui.minimap`
795793
* Reference to the minimap object. See `Minimap` class reference for methods.
794+
* `show_formspec(formname, formspec)` : returns true on success
795+
* Shows a formspec to the player
796796

797797
Class reference
798798
---------------
@@ -837,3 +837,37 @@ Definition tables
837837
func = function(name, param), -- Called when command is run.
838838
-- Returns boolean success and text output.
839839
}
840+
841+
Escape sequences
842+
----------------
843+
Most text can contain escape sequences, that can for example color the text.
844+
There are a few exceptions: tab headers, dropdowns and vertical labels can't.
845+
The following functions provide escape sequences:
846+
* `core.get_color_escape_sequence(color)`:
847+
* `color` is a ColorString
848+
* The escape sequence sets the text color to `color`
849+
* `core.colorize(color, message)`:
850+
* Equivalent to:
851+
`core.get_color_escape_sequence(color) ..
852+
message ..
853+
core.get_color_escape_sequence("#ffffff")`
854+
* `color.get_background_escape_sequence(color)`
855+
* `color` is a ColorString
856+
* The escape sequence sets the background of the whole text element to
857+
`color`. Only defined for item descriptions and tooltips.
858+
859+
`ColorString`
860+
-------------
861+
`#RGB` defines a color in hexadecimal format.
862+
863+
`#RGBA` defines a color in hexadecimal format and alpha channel.
864+
865+
`#RRGGBB` defines a color in hexadecimal format.
866+
867+
`#RRGGBBAA` defines a color in hexadecimal format and alpha channel.
868+
869+
Named colors are also supported and are equivalent to
870+
[CSS Color Module Level 4](http://dev.w3.org/csswg/css-color/#named-colors).
871+
To specify the value of the alpha channel, append `#AA` to the end of the color name
872+
(e.g. `colorname#08`). For named colors the hexadecimal string representing the alpha
873+
value must (always) be two hexadecima

0 commit comments

Comments
 (0)
Please sign in to comment.