vpac / src / style.v
140 lines · 121 sloc · 3.7 KB · 841315c100c81d9e6b14c4b55a25d4e3d12db762
Raw
1@[has_globals]
2module main
3
4import term
5
6// color_flag is --color: 'auto' styles a terminal only, 'always' and
7// 'never' decide for every stream, as pacman's --color does
8__global color_flag = 'auto'
9// color_given is whether the command line spelled a --color: the config's
10// (options #:color) is the default only when it did not
11__global color_given = false
12
13// color_on is whether a stream gets styled: stdout, or stderr with err
14fn color_on(err bool) bool {
15 return match color_flag {
16 'always' { true }
17 'never' { false }
18 else {
19 if err { term.can_show_color_on_stderr() } else { term.can_show_color_on_stdout() }
20 }
21 }
22}
23
24// style_names are the styles a theme may name, (color #:role #(...)): the
25// terminal's attributes and its sixteen colors
26const style_names = ['none', 'bold', 'dim', 'italic', 'underline', 'reverse', 'black', 'red', 'green',
27 'yellow', 'blue', 'magenta', 'cyan', 'white', 'gray', 'bright_black', 'bright_red', 'bright_green',
28 'bright_yellow', 'bright_blue', 'bright_magenta', 'bright_cyan', 'bright_white']
29
30// apply_style wraps text in one named style; a name outside style_names is
31// refused by the config before it gets here
32fn apply_style(name string, s string) string {
33 return match name {
34 'bold' { term.bold(s) }
35 'dim' { term.dim(s) }
36 'italic' { term.italic(s) }
37 'underline' { term.underline(s) }
38 'reverse' { term.inverse(s) }
39 'black' { term.black(s) }
40 'red' { term.red(s) }
41 'green' { term.green(s) }
42 'yellow' { term.yellow(s) }
43 'blue' { term.blue(s) }
44 'magenta' { term.magenta(s) }
45 'cyan' { term.cyan(s) }
46 'white' { term.white(s) }
47 'gray' { term.gray(s) }
48 'bright_black' { term.bright_black(s) }
49 'bright_red' { term.bright_red(s) }
50 'bright_green' { term.bright_green(s) }
51 'bright_yellow' { term.bright_yellow(s) }
52 'bright_blue' { term.bright_blue(s) }
53 'bright_magenta' { term.bright_magenta(s) }
54 'bright_cyan' { term.bright_cyan(s) }
55 'bright_white' { term.bright_white(s) }
56 else { s }
57 }
58}
59
60// default_theme is what every role wears unless the config's (color ...)
61// form dresses it otherwise: the roles are the keys that form takes
62fn default_theme() map[string][]string {
63 return {
64 'heading': ['bold']
65 'title': ['bright_blue']
66 'source': ['bright_blue']
67 'name': ['bold']
68 'version': ['bright_green']
69 'branch': ['bright_blue']
70 'old': ['red']
71 'success': ['bold']
72 'warning': ['bold', 'bright_yellow']
73 'error': ['bold', 'red']
74 'prompt': ['bright_white']
75 'default': ['bold']
76 }
77}
78
79__global theme = default_theme()
80
81// themed wraps text in a role's styles, whether or not the stream is styled
82fn themed(role string, s string) string {
83 mut out := s
84 for name in theme[role] or { []string{} } {
85 out = apply_style(name, out)
86 }
87 return out
88}
89
90// role styles text bound for stdout as a role of the theme, erole text
91// bound for stderr
92fn role(name string, s string) string {
93 return if color_on(false) { themed(name, s) } else { s }
94}
95
96fn erole(name string, s string) string {
97 return if color_on(true) { themed(name, s) } else { s }
98}
99
100fn heading(s string) string {
101 return role('heading', s)
102}
103
104fn bold_heading(s string) string {
105 return role('title', s)
106}
107
108fn source_style(s string) string {
109 return role('source', s)
110}
111
112fn name_style(s string) string {
113 return role('name', s)
114}
115
116fn version_style(s string) string {
117 return role('version', s)
118}
119
120fn branch_style(s string) string {
121 return role('branch', s)
122}
123
124fn old_style(s string) string {
125 return role('old', s)
126}
127
128fn success(s string) string {
129 return role('success', s)
130}
131
132// warning goes to either stream, so it is styled only when both would be
133fn warning(s string) string {
134 prefix := 'warning: '
135 return if color_on(false) && color_on(true) {
136 themed('warning', prefix) + s
137 } else {
138 prefix + s
139 }
140}
141