2019-06-24 19:50:36 +03:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// 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
|
|
|
|
2019-07-04 18:39:35 +03:00
|
|
|
import os
|
|
|
|
|
|
|
|
// Calling this functions enables color terminal output on windows
|
|
|
|
// Maybe implement a way to auto run an init function when a module
|
|
|
|
// is imported on a certain os. for example to run this?
|
|
|
|
pub fn enable_term_color_win() {
|
|
|
|
$if windows {
|
2019-07-28 18:19:03 +03:00
|
|
|
mode_wanted := os.ENABLE_PROCESSED_OUTPUT | os.ENABLE_VIRTUAL_TERMINAL_PROCESSING
|
|
|
|
mut mode_current := 0
|
2019-07-04 18:39:35 +03:00
|
|
|
h_output := C.GetStdHandle(os.STD_OUTPUT_HANDLE)
|
2019-07-28 18:19:03 +03:00
|
|
|
if h_output == os.INVALID_HANDLE_VALUE {
|
|
|
|
panic('term.enable_term_color_win(): error getting output handle.')
|
|
|
|
}
|
|
|
|
if !C.GetConsoleMode(h_output, &mode_current) {
|
|
|
|
panic('term.enable_term_color_win(): error getting console mode.')
|
|
|
|
}
|
|
|
|
if mode_wanted == mode_current {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !C.SetConsoleMode(h_output, mode_wanted) {
|
|
|
|
panic('term.enable_term_color_win(): error setting console mode.')
|
2019-07-04 18:39:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$else {
|
2019-07-28 18:19:03 +03:00
|
|
|
println('term.enable_term_color_win() should only be called on windows.')
|
2019-07-04 18:39:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn format(msg, open, close string) string {
|
2019-07-17 03:44:08 +03:00
|
|
|
$if windows {
|
|
|
|
enable_term_color_win()
|
|
|
|
}
|
|
|
|
|
2019-06-24 19:50:36 +03:00
|
|
|
return '\x1b[' + open + 'm' + msg + '\x1b[' + close + 'm'
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn bg_black(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '40', '49')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn bg_blue(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '44', '49')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn bg_cyan(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '46', '49')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn bg_green(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '42', '49')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn bg_magenta(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '45', '49')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn bg_red(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '41', '49')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn bg_white(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '47', '49')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn bg_yellow(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '43', '49')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn black(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '30', '39')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn blue(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '34', '39')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn bold(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '1', '22')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn cyan(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '36', '39')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn dim(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '2', '22')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn green(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '32', '39')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn gray(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '90', '39')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn hidden(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '8', '28')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn italic(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '3', '23')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn inverse(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '7', '27')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn magenta(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '35', '39')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn reset(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '0', '0')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn red(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '31', '39')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn strikethrough(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '9', '29')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn underline(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '4', '24')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn white(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '37', '39')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn yellow(msg string) string {
|
2019-06-24 19:50:36 +03:00
|
|
|
return format(msg, '33', '39')
|
2019-07-16 18:59:07 +03:00
|
|
|
}
|