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

builtin: use C.fwrite (buffered) for _write_buf_to_fd (#14558)

This commit is contained in:
Delyan Angelov
2022-05-30 19:15:05 +03:00
committed by GitHub
parent 58ebc0680e
commit 0f3b2c2ae7
11 changed files with 69 additions and 14 deletions

View File

@@ -16,6 +16,7 @@ module term
// y is the y coordinate
pub fn set_cursor_position(c Coord) {
print('\x1b[$c.y;$c.x' + 'H')
flush_stdout()
}
// n is number of cells
@@ -25,6 +26,7 @@ pub fn set_cursor_position(c Coord) {
// direction: D is backward / West
pub fn move(n int, direction string) {
print('\x1b[$n$direction')
flush_stdout()
}
pub fn cursor_up(n int) {
@@ -50,6 +52,7 @@ pub fn cursor_back(n int) {
pub fn erase_display(t string) {
print('\x1b[' + t + 'J')
flush_stdout()
}
pub fn erase_toend() {
@@ -63,6 +66,7 @@ pub fn erase_tobeg() {
// clears entire screen and returns cursor to top left-corner
pub fn erase_clear() {
print('\033[H\033[J')
flush_stdout()
}
pub fn erase_del_clear() {
@@ -75,6 +79,7 @@ pub fn erase_del_clear() {
// Note: Cursor position does not change
pub fn erase_line(t string) {
print('\x1b[' + t + 'K')
flush_stdout()
}
pub fn erase_line_toend() {
@@ -92,11 +97,13 @@ pub fn erase_line_clear() {
// Will make cursor appear if not visible
pub fn show_cursor() {
print('\x1b[?25h')
flush_stdout()
}
// Will make cursor invisible
pub fn hide_cursor() {
print('\x1b[?25l')
flush_stdout()
}
// clear_previous_line - useful for progressbars.
@@ -105,4 +112,5 @@ pub fn hide_cursor() {
// the previous content.
pub fn clear_previous_line() {
print('\r\x1b[1A\x1b[2K')
flush_stdout()
}

View File

@@ -52,6 +52,7 @@ pub fn get_cursor_position() ?Coord {
unsafe { C.tcsetattr(0, C.TCSANOW, &state) }
print('\e[6n')
flush_stdout()
mut x := 0
mut y := 0
@@ -87,6 +88,7 @@ pub fn set_terminal_title(title string) bool {
print('\033]0')
print(title)
print('\007')
flush_stdout()
return true
}
@@ -94,4 +96,5 @@ pub fn set_terminal_title(title string) bool {
pub fn clear() {
print('\x1b[2J')
print('\x1b[H')
flush_stdout()
}

View File

@@ -32,12 +32,14 @@ pub fn init(cfg Config) &Context {
fn save_title() {
// restore the previously saved terminal title
print('\x1b[22;0t')
flush_stdout()
}
[inline]
fn load_title() {
// restore the previously saved terminal title
print('\x1b[23;0t')
flush_stdout()
}
pub fn (mut ctx Context) run() ? {

View File

@@ -26,6 +26,7 @@ fn restore_terminal_state() {
// clear the terminal and set the cursor to the origin
print('\x1b[2J\x1b[3J')
print('\x1b[?1049l')
flush_stdout()
}
C.SetConsoleMode(ui.ctx_ptr.stdin_handle, ui.stdin_at_startup)
}
@@ -65,6 +66,7 @@ pub fn init(cfg Config) &Context {
print('\x1b[?1049h')
// clear the terminal and set the cursor to the origin
print('\x1b[2J\x1b[3J\x1b[1;1H')
flush_stdout()
}
if ctx.cfg.hide_cursor {
@@ -74,6 +76,7 @@ pub fn init(cfg Config) &Context {
if ctx.cfg.window_title != '' {
print('\x1b]0;$ctx.cfg.window_title\x07')
flush_stdout()
}
unsafe {
@@ -317,10 +320,12 @@ fn (mut ctx Context) parse_events() {
fn save_title() {
// restore the previously saved terminal title
print('\x1b[22;0t')
flush_stdout()
}
[inline]
fn load_title() {
// restore the previously saved terminal title
print('\x1b[23;0t')
flush_stdout()
}

View File

@@ -79,6 +79,7 @@ fn (mut ctx Context) termios_setup() ? {
if ctx.cfg.window_title != '' {
print('\x1b]0;$ctx.cfg.window_title\x07')
flush_stdout()
}
if !ctx.cfg.skip_init_checks {
@@ -90,6 +91,7 @@ fn (mut ctx Context) termios_setup() ? {
// feature-test the SU spec
sx, sy := get_cursor_position()
print('$bsu$esu')
flush_stdout()
ex, ey := get_cursor_position()
if sx == ex && sy == ey {
// the terminal either ignored or handled the sequence properly, enable SU
@@ -108,11 +110,14 @@ fn (mut ctx Context) termios_setup() ? {
C.tcsetattr(C.STDIN_FILENO, C.TCSAFLUSH, &termios)
// enable mouse input
print('\x1b[?1003h\x1b[?1006h')
flush_stdout()
if ctx.cfg.use_alternate_buffer {
// switch to the alternate buffer
print('\x1b[?1049h')
flush_stdout()
// clear the terminal and set the cursor to the origin
print('\x1b[2J\x1b[3J\x1b[1;1H')
flush_stdout()
}
ctx.window_height, ctx.window_width = get_terminal_size()
@@ -162,6 +167,7 @@ fn (mut ctx Context) termios_setup() ? {
fn get_cursor_position() (int, int) {
print('\033[6n')
flush_stdout()
mut s := ''
unsafe {
buf := malloc_noscan(25)
@@ -183,8 +189,10 @@ fn supports_truecolor() bool {
}
// set the bg color to some arbirtrary value (#010203), assumed not to be the default
print('\x1b[48:2:1:2:3m')
flush_stdout()
// andquery the current color
print('\x1bP\$qm\x1b\\')
flush_stdout()
mut s := ''
unsafe {
buf := malloc_noscan(25)
@@ -199,6 +207,7 @@ fn termios_reset() {
// C.TCSANOW ??
C.tcsetattr(C.STDIN_FILENO, C.TCSAFLUSH, &ui.termios_at_startup)
print('\x1b[?1003l\x1b[?1006l\x1b[?25h')
flush_stdout()
c := ctx_ptr
if unsafe { c != 0 } && c.cfg.use_alternate_buffer {
print('\x1b[?1049l')

View File

@@ -116,6 +116,7 @@ pub fn (mut ctx Context) clear() {
[inline]
pub fn (mut ctx Context) set_window_title(s string) {
print('\x1b]0;$s\x07')
flush_stdout()
}
// draw_point draws a point at position `x`,`y`.