@[has_globals] module main import term // color_flag is --color: 'auto' styles a terminal only, 'always' and // 'never' decide for every stream, as pacman's --color does __global color_flag = 'auto' // color_given is whether the command line spelled a --color: the config's // (options #:color) is the default only when it did not __global color_given = false // color_on is whether a stream gets styled: stdout, or stderr with err fn color_on(err bool) bool { return match color_flag { 'always' { true } 'never' { false } else { if err { term.can_show_color_on_stderr() } else { term.can_show_color_on_stdout() } } } } // style_names are the styles a theme may name, (color #:role #(...)): the // terminal's attributes and its sixteen colors const style_names = ['none', 'bold', 'dim', 'italic', 'underline', 'reverse', 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'gray', 'bright_black', 'bright_red', 'bright_green', 'bright_yellow', 'bright_blue', 'bright_magenta', 'bright_cyan', 'bright_white'] // apply_style wraps text in one named style; a name outside style_names is // refused by the config before it gets here fn apply_style(name string, s string) string { return match name { 'bold' { term.bold(s) } 'dim' { term.dim(s) } 'italic' { term.italic(s) } 'underline' { term.underline(s) } 'reverse' { term.inverse(s) } 'black' { term.black(s) } 'red' { term.red(s) } 'green' { term.green(s) } 'yellow' { term.yellow(s) } 'blue' { term.blue(s) } 'magenta' { term.magenta(s) } 'cyan' { term.cyan(s) } 'white' { term.white(s) } 'gray' { term.gray(s) } 'bright_black' { term.bright_black(s) } 'bright_red' { term.bright_red(s) } 'bright_green' { term.bright_green(s) } 'bright_yellow' { term.bright_yellow(s) } 'bright_blue' { term.bright_blue(s) } 'bright_magenta' { term.bright_magenta(s) } 'bright_cyan' { term.bright_cyan(s) } 'bright_white' { term.bright_white(s) } else { s } } } // default_theme is what every role wears unless the config's (color ...) // form dresses it otherwise: the roles are the keys that form takes fn default_theme() map[string][]string { return { 'heading': ['bold'] 'title': ['bright_blue'] 'source': ['bright_blue'] 'name': ['bold'] 'version': ['bright_green'] 'branch': ['bright_blue'] 'old': ['red'] 'success': ['bold'] 'warning': ['bold', 'bright_yellow'] 'error': ['bold', 'red'] 'prompt': ['bright_white'] 'default': ['bold'] } } __global theme = default_theme() // themed wraps text in a role's styles, whether or not the stream is styled fn themed(role string, s string) string { mut out := s for name in theme[role] or { []string{} } { out = apply_style(name, out) } return out } // role styles text bound for stdout as a role of the theme, erole text // bound for stderr fn role(name string, s string) string { return if color_on(false) { themed(name, s) } else { s } } fn erole(name string, s string) string { return if color_on(true) { themed(name, s) } else { s } } fn heading(s string) string { return role('heading', s) } fn bold_heading(s string) string { return role('title', s) } fn source_style(s string) string { return role('source', s) } fn name_style(s string) string { return role('name', s) } fn version_style(s string) string { return role('version', s) } fn branch_style(s string) string { return role('branch', s) } fn old_style(s string) string { return role('old', s) } fn success(s string) string { return role('success', s) } // warning goes to either stream, so it is styled only when both would be fn warning(s string) string { prefix := 'warning: ' return if color_on(false) && color_on(true) { themed('warning', prefix) + s } else { prefix + s } }