aboutsummaryrefslogtreecommitdiff
path: root/core/text.nom
blob: e1aaa9d82973d287f6d0ae6b24f16cf144e0f5bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#
    This file contains some definitions of text escape sequences, including ANSI console
    color codes.

use "core/metaprogramming.nom"

# Text functions
action [%texts joined with %glue]
    lua> ".."
        local text_bits = {}
        for i,bit in ipairs(\%texts) do text_bits[i] = stringify(bit) end
        return table.concat(text_bits, \%glue)
parse [joined %texts, %texts joined] as: %texts joined with ""

compile [capitalized %text, %text capitalized] to
    Lua value "((\(%text as lua expr)):gsub('%l', string.upper, 1))"

compile [%text with %sub instead of %patt, %text s/%patt/%sub] to
    Lua value "((\(%text as lua expr)):gsub(\(%patt as lua expr), \(%sub as lua expr)))"

# Text literals
lua> ".."
    do
        local escapes = {
            nl="\\\\n", newline="\\\\n", tab="\\\\t", bell="\\\\a", cr="\\\\r", ["carriage return"]="\\\\r",
            backspace="\\\\b", ["form feed"]="\\\\f", formfeed="\\\\f", ["vertical tab"]="\\\\v",
        };
        for name, e in pairs(escapes) do
            local lua = "'"..e.."'";
            nomsu:define_compile_action(name, function(tree) return Lua.Value(tree.source, lua); end);
        end
        local colors = {
            ["reset color"]="\\\\27[0m", bright="\\\\27[1m", dim="\\\\27[2m", underscore="\\\\27[4m",
            blink="\\\\27[5m", inverse="\\\\27[7m", hidden="\\\\27[8m",

            black="\\\\27[30m", red="\\\\27[31m", green="\\\\27[32m", yellow="\\\\27[33m", blue="\\\\27[34m",
            magenta="\\\\27[35m", cyan="\\\\27[36m", white="\\\\27[37m",

            ["on black"]="\\\\27[40m", ["on red"]="\\\\27[41m", ["on green"]="\\\\27[42m", ["on yellow"]="\\\\27[43m",
            ["on blue"]="\\\\27[44m", ["on magenta"]="\\\\27[45m", ["on cyan"]="\\\\27[46m", ["on white"]="\\\\27[47m",
        };
        for name, c in pairs(colors) do
            local color = "'"..c.."'";
            local reset = "'"..colors["reset color"].."'";
            nomsu:define_compile_action(name, function(tree) return Lua.Value(tree.source, color); end);
            nomsu:define_compile_action(name.." %", function(\%)
                return Lua.Value(tree.source, color, "..", \%:as_lua(nomsu), "..", reset);
            end);
        end
    end