1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

term: move enable_term_color_win() to colors_win.v

This commit is contained in:
joe-conigliaro 2019-07-29 12:44:21 +10:00 committed by Alexander Medvednikov
parent 1a22482c09
commit 45029f8c86
2 changed files with 36 additions and 29 deletions

View File

@ -6,38 +6,12 @@ module term
import os import os
// Calling this functions enables color terminal output on windows fn _format(msg, open, close string) string {
// Maybe implement a way to auto run an init function when a module return '\x1b[' + open + 'm' + msg + '\x1b[' + close + 'm'
// is imported on a certain os. for example to run this?
pub fn enable_term_color_win() {
$if windows {
mode_wanted := os.ENABLE_PROCESSED_OUTPUT | os.ENABLE_VIRTUAL_TERMINAL_PROCESSING
mut mode_current := 0
h_output := C.GetStdHandle(os.STD_OUTPUT_HANDLE)
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.')
}
}
$else {
println('term.enable_term_color_win() should only be called on windows.')
}
} }
pub fn format(msg, open, close string) string { pub fn format(msg, open, close string) string {
$if windows { return _format(msg, open, close)
enable_term_color_win()
}
return '\x1b[' + open + 'm' + msg + '\x1b[' + close + 'm'
} }
pub fn bg_black(msg string) string { pub fn bg_black(msg string) string {

33
vlib/term/colors_win.v Normal file
View File

@ -0,0 +1,33 @@
// 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.
module term
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() {
mode_wanted := os.ENABLE_PROCESSED_OUTPUT | os.ENABLE_VIRTUAL_TERMINAL_PROCESSING
mut mode_current := 0
h_output := C.GetStdHandle(os.STD_OUTPUT_HANDLE)
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.')
}
}
pub fn format(msg, open, close string) string {
enable_term_color_win()
return _format(msg, open, close)
}