aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/color/terminal.lux
blob: 2c5b6a89fea3cef78c8c23cd9419dc440a7487b4 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

... https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
(.require
 [library
  [lux (.except with)
   [abstract
    [equivalence (.only Equivalence)]]
   [data
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["[0]" char]
     ["%" \\format]]]
   [math
    [number
     ["n" nat]]]
   [meta
    [macro
     ["[0]" template]]
    [type
     ["[0]" nominal]]]]]
 [//
  ["[0]" rgb (.only RGB)]])

(nominal.def .public Command
  [Text Text]

  (def .public equivalence
    (Equivalence Command)
    (implementation
     (def (= reference it)
       (let [[beforeR afterR] (nominal.representation Command reference)
             [beforeI afterI] (nominal.representation Command it)]
         (and (text#= beforeR beforeI)
              (text#= afterR afterI))))))

  (def command
    Text
    (%.format (text.of_char char.escape) "["))

  (def .public (with command text)
    (-> Command Text
        Text)
    (let [[before after] (nominal.representation Command command)]
      (%.format before
                text
                after)))

  (with_template [<code> <name>]
    [(def <name>
       Text
       (%.format ..command (%.nat <code>) "m"))]

    [39 default_foreground_color]
    [49 default_background_color]
    )

  (with_template [<fg> <bg> <color>]
    [(`` (def .public (,, (template.symbol [<color> "_foreground"]))
           Command
           (|> [(%.format ..command (%.nat <fg>) "m")
                ..default_foreground_color]
               (nominal.abstraction Command))))

     (`` (def .public (,, (template.symbol [<color> "_background"]))
           Command
           (|> [(%.format ..command (%.nat <bg>) "m")
                ..default_background_color]
               (nominal.abstraction Command))))]

    [030 040 black]
    [031 041 red]
    [032 042 green]
    [033 043 yellow]
    [034 044 blue]
    [035 045 magenta]
    [036 046 cyan]
    [037 047 white]
    
    [090 100 bright_black]
    [091 101 bright_red]
    [092 102 bright_green]
    [093 103 bright_yellow]
    [094 104 bright_blue]
    [095 105 bright_magenta]
    [096 106 bright_cyan]
    [097 107 bright_white]
    )

  (with_template [<command> <name> <reset>]
    [(def .public (<name> it)
       (-> RGB
           Command)
       (|> [(%.format ..command
                      <command>
                      ";" (%.nat (the rgb.#red it))
                      ";" (%.nat (the rgb.#green it))
                      ";" (%.nat (the rgb.#blue it))
                      "m")
            <reset>]
           (nominal.abstraction Command)))]

    ["38;2" foreground ..default_foreground_color]
    ["48;2" background ..default_background_color]
    )
  )