2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2019-06-24 19:50:36 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-07-01 18:09:22 +03:00
|
|
|
module term
|
2019-06-24 19:50:36 +03:00
|
|
|
|
2022-12-03 15:01:39 +03:00
|
|
|
// format_esc produces an ANSI escape code, for selecting the graphics rendition of the following
|
|
|
|
// text. Each of the attributes that can be passed in `code`, separated by `;`, will be in effect,
|
|
|
|
// until the terminal encounters another SGR ANSI escape code. For more details about the different
|
|
|
|
// codes, and their meaning, see: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
|
|
|
|
pub fn format_esc(code string) string {
|
|
|
|
return '\x1b[${code}m'
|
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// format returns ANSI escape coded `msg` formatted with a preceding
|
|
|
|
// `open` and a succeeding `close`.
|
|
|
|
// For instance, `format('hi', '9', '29')` returns `'\x1b[9mhi\x1b[29m'`,
|
|
|
|
// or 'hi' with strikethrough, where `'\x1b[9m'` represents
|
|
|
|
// crossed out/strikethrough text and `'\x1b[29m'` turns off strikethrough.
|
2020-10-15 13:32:28 +03:00
|
|
|
pub fn format(msg string, open string, close string) string {
|
2022-11-15 16:53:13 +03:00
|
|
|
return '\x1b[${open}m${msg}\x1b[${close}m'
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// format_rgb returns ANSI escape coded `msg` formatted with a preceding
|
|
|
|
// `open`, a succeeding `close` and the provided RGB colors `r`, `g`, and `b`.
|
2020-10-15 13:32:28 +03:00
|
|
|
pub fn format_rgb(r int, g int, b int, msg string, open string, close string) string {
|
2022-11-15 16:53:13 +03:00
|
|
|
return '\x1b[${open};2;${r};${g};${b}m${msg}\x1b[${close}m'
|
2019-07-31 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// rbg returns the `msg` with the foreground in the specified RGB color
|
|
|
|
// For example, `rgb(0, 255, 0, 'hi')` returns the `'hi'` string in
|
|
|
|
// lime color.
|
2020-10-15 13:32:28 +03:00
|
|
|
pub fn rgb(r int, g int, b int, msg string) string {
|
2019-07-31 20:22:38 +03:00
|
|
|
return format_rgb(r, g, b, msg, '38', '39')
|
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bg_rgb returns the `msg` with the background in the specified RGB color.
|
|
|
|
// For example, `bg_rgb(255, 0, 0, 'hi')` returns the text `'hi'` in
|
|
|
|
// red color.
|
2020-10-15 13:32:28 +03:00
|
|
|
pub fn bg_rgb(r int, g int, b int, msg string) string {
|
2019-07-31 20:22:38 +03:00
|
|
|
return format_rgb(r, g, b, msg, '48', '49')
|
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// hex returns the `msg` with the foreground in the specified `hex` color
|
|
|
|
// For example, `rgb(255, 'hi')` returns the `'hi'` string in
|
|
|
|
// blue color, which is `(0, 0, 255)` in RGB.
|
2019-07-31 20:22:38 +03:00
|
|
|
pub fn hex(hex int, msg string) string {
|
2020-10-15 13:32:28 +03:00
|
|
|
return format_rgb(hex >> 16, hex >> 8 & 0xFF, hex & 0xFF, msg, '38', '39')
|
2019-07-31 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// hex returns the `msg` with the background in the specified `hex` color
|
|
|
|
// For example, `bg_rgb(255, 'hi')` returns the `'hi'` string in
|
|
|
|
// a background of blue color, which is `(0, 0, 255)` in RGB.
|
2019-07-31 20:22:38 +03:00
|
|
|
pub fn bg_hex(hex int, msg string) string {
|
2020-10-15 13:32:28 +03:00
|
|
|
return format_rgb(hex >> 16, hex >> 8 & 0xFF, hex & 0xFF, msg, '48', '49')
|
2019-07-31 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// reset resets all formatting for `msg`.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn reset(msg string) string {
|
|
|
|
return format(msg, '0', '0')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bold returns the given `msg` in bold.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bold(msg string) string {
|
|
|
|
return format(msg, '1', '22')
|
2019-07-31 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// dim returns the dimmed `msg`.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn dim(msg string) string {
|
|
|
|
return format(msg, '2', '22')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// italic returns the given `msg` in italic.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn italic(msg string) string {
|
|
|
|
return format(msg, '3', '23')
|
2019-07-31 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// underline returns the underlined `msg`.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn underline(msg string) string {
|
|
|
|
return format(msg, '4', '24')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2022-11-18 20:03:14 +03:00
|
|
|
// slow_blink will surround the `msg` with ANSI escape codes for blinking (less than 150 times per minute).
|
|
|
|
pub fn slow_blink(msg string) string {
|
|
|
|
return format(msg, '5', '25')
|
|
|
|
}
|
|
|
|
|
|
|
|
// rapid_blink will surround the `msg` with ANSI escape codes for blinking (over 150 times per minute).
|
|
|
|
// Note that unlike slow_blink, this is not very widely supported.
|
|
|
|
pub fn rapid_blink(msg string) string {
|
|
|
|
return format(msg, '6', '26')
|
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// inverse inverses the given `msg`.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn inverse(msg string) string {
|
|
|
|
return format(msg, '7', '27')
|
2019-07-31 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// hidden hides the given `msg`.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn hidden(msg string) string {
|
|
|
|
return format(msg, '8', '28')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// strikethrough returns the given `msg` in strikethrough.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn strikethrough(msg string) string {
|
|
|
|
return format(msg, '9', '29')
|
2019-07-31 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// black formats the `msg` in black.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn black(msg string) string {
|
|
|
|
return format(msg, '30', '39')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// red formats the `msg` in red.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn red(msg string) string {
|
|
|
|
return format(msg, '31', '39')
|
2019-07-31 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// green formats the `msg` in green.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn green(msg string) string {
|
|
|
|
return format(msg, '32', '39')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// yellow formats the `msg` in yellow.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn yellow(msg string) string {
|
|
|
|
return format(msg, '33', '39')
|
2019-07-31 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// blue formats the `msg` in blue.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn blue(msg string) string {
|
|
|
|
return format(msg, '34', '39')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// magenta formats the `msg` in magenta.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn magenta(msg string) string {
|
|
|
|
return format(msg, '35', '39')
|
2019-07-31 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// cyan formats the `msg` in cyan.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn cyan(msg string) string {
|
|
|
|
return format(msg, '36', '39')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// white formats the `msg` in white.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn white(msg string) string {
|
|
|
|
return format(msg, '37', '39')
|
2019-07-31 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bg_black formats the `msg` in black background.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bg_black(msg string) string {
|
|
|
|
return format(msg, '40', '49')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bg_red formats the `msg` in red background.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bg_red(msg string) string {
|
|
|
|
return format(msg, '41', '49')
|
2019-07-31 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bg_green formats the `msg` in green background.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bg_green(msg string) string {
|
|
|
|
return format(msg, '42', '49')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bg_yellow formats the `msg` in yellow background.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bg_yellow(msg string) string {
|
|
|
|
return format(msg, '43', '49')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bg_blue formats the `msg` in blue background.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bg_blue(msg string) string {
|
|
|
|
return format(msg, '44', '49')
|
2019-07-31 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bg_magenta formats the `msg` in magenta background.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bg_magenta(msg string) string {
|
|
|
|
return format(msg, '45', '49')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bg_cyan formats the `msg` in cyan background.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bg_cyan(msg string) string {
|
|
|
|
return format(msg, '46', '49')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bg_white formats the `msg` in white background.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bg_white(msg string) string {
|
|
|
|
return format(msg, '47', '49')
|
2019-07-31 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// gray formats the `msg` in gray (equivalent to `bright_black`).
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn gray(msg string) string {
|
2019-07-31 20:22:38 +03:00
|
|
|
return bright_black(msg)
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bright_black formats the `msg` in bright black.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bright_black(msg string) string {
|
|
|
|
return format(msg, '90', '39')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bright_red formats the `msg` in bright red.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bright_red(msg string) string {
|
|
|
|
return format(msg, '91', '39')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bright_green formats the `msg` in bright green.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bright_green(msg string) string {
|
|
|
|
return format(msg, '92', '39')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bright_yellow formats the `msg` in bright yellow.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bright_yellow(msg string) string {
|
|
|
|
return format(msg, '93', '39')
|
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bright_blue formats the `msg` in bright blue.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bright_blue(msg string) string {
|
|
|
|
return format(msg, '94', '39')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bright_magenta formats the `msg` in bright magenta.
|
2019-07-31 20:22:38 +03:00
|
|
|
pub fn bright_magenta(msg string) string {
|
|
|
|
return format(msg, '95', '39')
|
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bright_cyan formats the `msg` in bright cyan.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bright_cyan(msg string) string {
|
|
|
|
return format(msg, '96', '39')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bright_white formats the `msg` in bright white.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bright_white(msg string) string {
|
|
|
|
return format(msg, '97', '39')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bright_bg_black formats the `msg` in bright black background.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bright_bg_black(msg string) string {
|
|
|
|
return format(msg, '100', '49')
|
2019-07-31 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bright_bg_red formats the `msg` in bright red background.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bright_bg_red(msg string) string {
|
|
|
|
return format(msg, '101', '49')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bright_bg_green formats the `msg` in bright green background.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bright_bg_green(msg string) string {
|
|
|
|
return format(msg, '102', '49')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bright_bg_yellow formats the `msg` in bright yellow background.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bright_bg_yellow(msg string) string {
|
|
|
|
return format(msg, '103', '49')
|
2019-06-24 19:50:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bright_bg_blue formats the `msg` in bright blue background.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bright_bg_blue(msg string) string {
|
|
|
|
return format(msg, '104', '49')
|
2019-07-31 20:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bright_bg_magenta formats the `msg` in bright magenta background.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bright_bg_magenta(msg string) string {
|
|
|
|
return format(msg, '105', '49')
|
2019-07-16 18:59:07 +03:00
|
|
|
}
|
2019-07-31 20:22:38 +03:00
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bright_bg_cyan formats the `msg` in bright cyan background.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bright_bg_cyan(msg string) string {
|
|
|
|
return format(msg, '106', '49')
|
|
|
|
}
|
|
|
|
|
2023-01-19 10:09:09 +03:00
|
|
|
// bright_bg_white formats the `msg` in bright white background.
|
2021-09-04 19:49:47 +03:00
|
|
|
pub fn bright_bg_white(msg string) string {
|
|
|
|
return format(msg, '107', '49')
|
2019-09-02 15:02:25 +03:00
|
|
|
}
|
2021-07-27 12:35:54 +03:00
|
|
|
|
|
|
|
// highlight_command highlights the command with an on-brand background
|
|
|
|
// to make CLI commands immediately recognizable.
|
|
|
|
pub fn highlight_command(command string) string {
|
2022-11-15 16:53:13 +03:00
|
|
|
return bright_white(bg_cyan(' ${command} '))
|
2021-07-27 12:35:54 +03:00
|
|
|
}
|