term: reorganize the module
							parent
							
								
									a14a5fbf95
								
							
						
					
					
						commit
						5c598a3085
					
				|  | @ -1,28 +0,0 @@ | |||
| module term | ||||
| 
 | ||||
| import os | ||||
| 
 | ||||
| pub fn can_show_color_on_stdout() bool { | ||||
| 	return supports_escape_sequences(1) | ||||
| } | ||||
| 
 | ||||
| pub fn can_show_color_on_stderr() bool { | ||||
| 	return supports_escape_sequences(2) | ||||
| } | ||||
| 
 | ||||
| fn supports_escape_sequences(fd int) bool { | ||||
| 	$if windows { | ||||
| 		return (is_atty(fd) & 0x0004) > 0 && os.getenv('TERM') != 'dumb' // ENABLE_VIRTUAL_TERMINAL_PROCESSING
 | ||||
| 	} $else { | ||||
| 		return is_atty(fd) > 0 && os.getenv('TERM') != 'dumb' | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| // ////////////////////////////////////////////
 | ||||
| pub fn ok_message(s string) string { | ||||
| 	return if can_show_color_on_stdout() { green(s) } else { s } | ||||
| } | ||||
| 
 | ||||
| pub fn fail_message(s string) string { | ||||
| 	return if can_show_color_on_stdout() { red(s) } else { s } | ||||
| } | ||||
|  | @ -1,5 +0,0 @@ | |||
| // 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 | ||||
| 
 | ||||
|  | @ -1,6 +0,0 @@ | |||
| // 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 | ||||
| 
 | ||||
|  | @ -1,12 +0,0 @@ | |||
| module term | ||||
| // h_divider will return a horizontal divider line with a dynamic width,
 | ||||
| // that depends on the current terminal settings
 | ||||
| pub fn h_divider(divider string) string { | ||||
| 	mut cols := 76 | ||||
| 	term_cols,_ := get_terminal_size() | ||||
| 	if term_cols > 0 { | ||||
| 		cols = term_cols | ||||
| 	} | ||||
| 	result := divider.repeat(1 + (cols / divider.len)) | ||||
| 	return result[0..cols] | ||||
| } | ||||
|  | @ -1,6 +0,0 @@ | |||
| module term | ||||
| 
 | ||||
| pub fn get_terminal_size() (int, int) {  | ||||
|    // TODO: find a way to get proper width&height of the terminal on a Javascript environment
 | ||||
|    return 80, 25   | ||||
| } | ||||
|  | @ -0,0 +1,48 @@ | |||
| module term | ||||
| 
 | ||||
| import os | ||||
| 
 | ||||
| const ( | ||||
| 	default_columns_size = 80 | ||||
| 	default_rows_size = 25 | ||||
| ) | ||||
| 
 | ||||
| // can_show_color_on_stdout returns true if colors are allowed in stdout;
 | ||||
| // returns false otherwise.
 | ||||
| pub fn can_show_color_on_stdout() bool { | ||||
| 	return supports_escape_sequences(1) | ||||
| } | ||||
| 
 | ||||
| // can_show_color_on_stderr returns true if colors are allowed in stderr;
 | ||||
| // returns false otherwise.
 | ||||
| pub fn can_show_color_on_stderr() bool { | ||||
| 	return supports_escape_sequences(2) | ||||
| } | ||||
| 
 | ||||
| // ok_message returns a colored string with green color.
 | ||||
| // If colors are not allowed, returns a given string.
 | ||||
| pub fn ok_message(s string) string { | ||||
| 	return if can_show_color_on_stdout() { green(s) } else { s } | ||||
| } | ||||
| 
 | ||||
| // fail_message returns a colored string with red color.
 | ||||
| // If colors are not allowed, returns a given string.
 | ||||
| pub fn fail_message(s string) string { | ||||
| 	return if can_show_color_on_stdout() { red(s) } else { s } | ||||
| } | ||||
| 
 | ||||
| // h_divider returns a horizontal divider line with a dynamic width,
 | ||||
| // that depends on the current terminal settings.
 | ||||
| pub fn h_divider(divider string) string { | ||||
| 	cols, _ := get_terminal_size() | ||||
| 	result := divider.repeat(1 + (cols / divider.len)) | ||||
| 	return result[0..cols] | ||||
| } | ||||
| 
 | ||||
| fn supports_escape_sequences(fd int) bool { | ||||
| 	$if windows { | ||||
| 		return (is_atty(fd) & 0x0004) > 0 && os.getenv('TERM') != 'dumb' // ENABLE_VIRTUAL_TERMINAL_PROCESSING
 | ||||
| 	} $else { | ||||
| 		return is_atty(fd) > 0 && os.getenv('TERM') != 'dumb' | ||||
| 	} | ||||
| } | ||||
|  | @ -0,0 +1,8 @@ | |||
| module term | ||||
| 
 | ||||
| // get_terminal_size returns a number of colums and rows of terminal window.
 | ||||
| pub fn get_terminal_size() (int, int) { | ||||
|    // TODO Find a way to get proper width & height of the terminal
 | ||||
|    // on a Javascript environment
 | ||||
|    return default_columns_size, default_rows_size | ||||
| } | ||||
|  | @ -13,12 +13,12 @@ pub: | |||
| 
 | ||||
| fn C.ioctl(fd int, request u64, arg voidptr) int | ||||
| 
 | ||||
| 
 | ||||
| // get_terminal_size returns a number of colums and rows of terminal window.
 | ||||
| pub fn get_terminal_size() (int,int) { | ||||
| 	if is_atty(1) <= 0 || os.getenv('TERM') == 'dumb' { | ||||
| 		return 80,25 | ||||
| 		return default_columns_size, default_rows_size | ||||
| 	} | ||||
| 	w := C.winsize{} | ||||
| 	C.ioctl(0, C.TIOCGWINSZ, &w) | ||||
| 	C.ioctl(1, C.TIOCGWINSZ, &w) | ||||
| 	return int(w.ws_col),int(w.ws_row) | ||||
| } | ||||
|  | @ -31,5 +31,5 @@ pub fn get_terminal_size() (int, int) { | |||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	return 80, 25 | ||||
| 	return default_columns_size, default_rows_size | ||||
| } | ||||
		Loading…
	
		Reference in New Issue