builtin: use C.fwrite (buffered) for _write_buf_to_fd (#14558)
parent
58ebc0680e
commit
0f3b2c2ae7
|
@ -24,6 +24,7 @@ pub fn cprint(omessage string) {
|
||||||
message = term.cyan(message)
|
message = term.cyan(message)
|
||||||
}
|
}
|
||||||
print(message)
|
print(message)
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cprint_strong(omessage string) {
|
pub fn cprint_strong(omessage string) {
|
||||||
|
@ -32,16 +33,19 @@ pub fn cprint_strong(omessage string) {
|
||||||
message = term.bright_green(message)
|
message = term.bright_green(message)
|
||||||
}
|
}
|
||||||
print(message)
|
print(message)
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cprintln(omessage string) {
|
pub fn cprintln(omessage string) {
|
||||||
cprint(omessage)
|
cprint(omessage)
|
||||||
println('')
|
println('')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cprintln_strong(omessage string) {
|
pub fn cprintln_strong(omessage string) {
|
||||||
cprint_strong(omessage)
|
cprint_strong(omessage)
|
||||||
println('')
|
println('')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn verbose_trace(label string, message string) {
|
pub fn verbose_trace(label string, message string) {
|
||||||
|
|
|
@ -123,6 +123,7 @@ pub fn (mut ts TestSession) print_messages() {
|
||||||
// progress mode, the last line is rewritten many times:
|
// progress mode, the last line is rewritten many times:
|
||||||
if is_ok && !ts.silent_mode {
|
if is_ok && !ts.silent_mode {
|
||||||
print('\r$empty\r$msg')
|
print('\r$empty\r$msg')
|
||||||
|
flush_stdout()
|
||||||
} else {
|
} else {
|
||||||
// the last \n is needed, so SKIP/FAIL messages
|
// the last \n is needed, so SKIP/FAIL messages
|
||||||
// will not get overwritten by the OK ones
|
// will not get overwritten by the OK ones
|
||||||
|
|
|
@ -200,8 +200,13 @@ fn (mut context Context) parse_options() ? {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn flushed_print(s string) {
|
||||||
|
print(s)
|
||||||
|
flush_stdout()
|
||||||
|
}
|
||||||
|
|
||||||
fn (mut context Context) clear_line() {
|
fn (mut context Context) clear_line() {
|
||||||
print(context.cline)
|
flushed_print(context.cline)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut context Context) expand_all_commands(commands []string) []string {
|
fn (mut context Context) expand_all_commands(commands []string) []string {
|
||||||
|
@ -247,7 +252,7 @@ fn (mut context Context) run() {
|
||||||
println('Series: ${si:4}/${context.series:-4}, command: $cmd')
|
println('Series: ${si:4}/${context.series:-4}, command: $cmd')
|
||||||
if context.warmup > 0 && run_warmups < context.commands.len {
|
if context.warmup > 0 && run_warmups < context.commands.len {
|
||||||
for i in 1 .. context.warmup + 1 {
|
for i in 1 .. context.warmup + 1 {
|
||||||
print('${context.cgoback}warming up run: ${i:4}/${context.warmup:-4} for ${cmd:-50s} took ${duration:6} ms ...')
|
flushed_print('${context.cgoback}warming up run: ${i:4}/${context.warmup:-4} for ${cmd:-50s} took ${duration:6} ms ...')
|
||||||
mut sw := time.new_stopwatch()
|
mut sw := time.new_stopwatch()
|
||||||
res := os.execute(cmd)
|
res := os.execute(cmd)
|
||||||
if res.exit_code != 0 {
|
if res.exit_code != 0 {
|
||||||
|
@ -260,9 +265,9 @@ fn (mut context Context) run() {
|
||||||
context.clear_line()
|
context.clear_line()
|
||||||
for i in 1 .. (context.count + 1) {
|
for i in 1 .. (context.count + 1) {
|
||||||
avg := f64(sum) / f64(i)
|
avg := f64(sum) / f64(i)
|
||||||
print('${context.cgoback}Average: ${avg:9.3f}ms | run: ${i:4}/${context.count:-4} | took ${duration:6} ms')
|
flushed_print('${context.cgoback}Average: ${avg:9.3f}ms | run: ${i:4}/${context.count:-4} | took ${duration:6} ms')
|
||||||
if context.show_output {
|
if context.show_output {
|
||||||
print(' | result: ${oldres:s}')
|
flushed_print(' | result: ${oldres:s}')
|
||||||
}
|
}
|
||||||
mut sw := time.new_stopwatch()
|
mut sw := time.new_stopwatch()
|
||||||
res := scripting.exec(cmd) or { continue }
|
res := scripting.exec(cmd) or { continue }
|
||||||
|
@ -288,7 +293,7 @@ fn (mut context Context) run() {
|
||||||
context.results[icmd].atiming = new_aints(context.results[icmd].timings, context.nmins,
|
context.results[icmd].atiming = new_aints(context.results[icmd].timings, context.nmins,
|
||||||
context.nmaxs)
|
context.nmaxs)
|
||||||
context.clear_line()
|
context.clear_line()
|
||||||
print(context.cgoback)
|
flushed_print(context.cgoback)
|
||||||
mut m := map[string][]int{}
|
mut m := map[string][]int{}
|
||||||
ioutputs := context.results[icmd].outputs
|
ioutputs := context.results[icmd].outputs
|
||||||
for o in ioutputs {
|
for o in ioutputs {
|
||||||
|
@ -358,7 +363,7 @@ fn (mut context Context) show_diff_summary() {
|
||||||
println('context: $context')
|
println('context: $context')
|
||||||
}
|
}
|
||||||
if int(base) > context.fail_on_maxtime {
|
if int(base) > context.fail_on_maxtime {
|
||||||
print(performance_regression_label)
|
flushed_print(performance_regression_label)
|
||||||
println('average time: ${base:6.1f} ms > $context.fail_on_maxtime ms threshold.')
|
println('average time: ${base:6.1f} ms > $context.fail_on_maxtime ms threshold.')
|
||||||
exit(2)
|
exit(2)
|
||||||
}
|
}
|
||||||
|
@ -367,7 +372,7 @@ fn (mut context Context) show_diff_summary() {
|
||||||
}
|
}
|
||||||
fail_threshold_max := f64(context.fail_on_regress_percent)
|
fail_threshold_max := f64(context.fail_on_regress_percent)
|
||||||
if first_cmd_percentage > fail_threshold_max {
|
if first_cmd_percentage > fail_threshold_max {
|
||||||
print(performance_regression_label)
|
flushed_print(performance_regression_label)
|
||||||
println('${first_cmd_percentage:5.1f}% > ${fail_threshold_max:5.1f}% threshold.')
|
println('${first_cmd_percentage:5.1f}% > ${fail_threshold_max:5.1f}% threshold.')
|
||||||
exit(3)
|
exit(3)
|
||||||
}
|
}
|
||||||
|
|
|
@ -183,6 +183,7 @@ fn (foptions &FormatOptions) format_pipe() {
|
||||||
// checker.new_checker(table, prefs).check(file_ast)
|
// checker.new_checker(table, prefs).check(file_ast)
|
||||||
formatted_content := fmt.fmt(file_ast, table, prefs, foptions.is_debug)
|
formatted_content := fmt.fmt(file_ast, table, prefs, foptions.is_debug)
|
||||||
print(formatted_content)
|
print(formatted_content)
|
||||||
|
flush_stdout()
|
||||||
foptions.vlog('fmt.fmt worked and $formatted_content.len bytes were written to stdout.')
|
foptions.vlog('fmt.fmt worked and $formatted_content.len bytes were written to stdout.')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -279,6 +280,7 @@ fn (mut foptions FormatOptions) post_process_file(file string, formatted_file_pa
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
print(formatted_fc)
|
print(formatted_fc)
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (f FormatOptions) str() string {
|
fn (f FormatOptions) str() string {
|
||||||
|
|
|
@ -268,15 +268,30 @@ fn _write_buf_to_fd(fd int, buf &u8, buf_len int) {
|
||||||
if buf_len <= 0 {
|
if buf_len <= 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
mut ptr := unsafe { buf }
|
||||||
|
mut remaining_bytes := isize(buf_len)
|
||||||
|
mut x := isize(0)
|
||||||
|
$if freestanding || vinix {
|
||||||
unsafe {
|
unsafe {
|
||||||
mut ptr := buf
|
|
||||||
mut remaining_bytes := buf_len
|
|
||||||
for remaining_bytes > 0 {
|
for remaining_bytes > 0 {
|
||||||
x := C.write(fd, ptr, remaining_bytes)
|
x = C.write(fd, ptr, remaining_bytes)
|
||||||
ptr += x
|
ptr += x
|
||||||
remaining_bytes -= x
|
remaining_bytes -= x
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} $else {
|
||||||
|
mut stream := voidptr(C.stdout)
|
||||||
|
if fd == 2 {
|
||||||
|
stream = voidptr(C.stderr)
|
||||||
|
}
|
||||||
|
unsafe {
|
||||||
|
for remaining_bytes > 0 {
|
||||||
|
x = isize(C.fwrite(ptr, 1, remaining_bytes, stream))
|
||||||
|
ptr += x
|
||||||
|
remaining_bytes -= x
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
__global total_m = i64(0)
|
__global total_m = i64(0)
|
||||||
|
|
|
@ -16,6 +16,7 @@ module term
|
||||||
// y is the y coordinate
|
// y is the y coordinate
|
||||||
pub fn set_cursor_position(c Coord) {
|
pub fn set_cursor_position(c Coord) {
|
||||||
print('\x1b[$c.y;$c.x' + 'H')
|
print('\x1b[$c.y;$c.x' + 'H')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
// n is number of cells
|
// n is number of cells
|
||||||
|
@ -25,6 +26,7 @@ pub fn set_cursor_position(c Coord) {
|
||||||
// direction: D is backward / West
|
// direction: D is backward / West
|
||||||
pub fn move(n int, direction string) {
|
pub fn move(n int, direction string) {
|
||||||
print('\x1b[$n$direction')
|
print('\x1b[$n$direction')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cursor_up(n int) {
|
pub fn cursor_up(n int) {
|
||||||
|
@ -50,6 +52,7 @@ pub fn cursor_back(n int) {
|
||||||
|
|
||||||
pub fn erase_display(t string) {
|
pub fn erase_display(t string) {
|
||||||
print('\x1b[' + t + 'J')
|
print('\x1b[' + t + 'J')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn erase_toend() {
|
pub fn erase_toend() {
|
||||||
|
@ -63,6 +66,7 @@ pub fn erase_tobeg() {
|
||||||
// clears entire screen and returns cursor to top left-corner
|
// clears entire screen and returns cursor to top left-corner
|
||||||
pub fn erase_clear() {
|
pub fn erase_clear() {
|
||||||
print('\033[H\033[J')
|
print('\033[H\033[J')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn erase_del_clear() {
|
pub fn erase_del_clear() {
|
||||||
|
@ -75,6 +79,7 @@ pub fn erase_del_clear() {
|
||||||
// Note: Cursor position does not change
|
// Note: Cursor position does not change
|
||||||
pub fn erase_line(t string) {
|
pub fn erase_line(t string) {
|
||||||
print('\x1b[' + t + 'K')
|
print('\x1b[' + t + 'K')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn erase_line_toend() {
|
pub fn erase_line_toend() {
|
||||||
|
@ -92,11 +97,13 @@ pub fn erase_line_clear() {
|
||||||
// Will make cursor appear if not visible
|
// Will make cursor appear if not visible
|
||||||
pub fn show_cursor() {
|
pub fn show_cursor() {
|
||||||
print('\x1b[?25h')
|
print('\x1b[?25h')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Will make cursor invisible
|
// Will make cursor invisible
|
||||||
pub fn hide_cursor() {
|
pub fn hide_cursor() {
|
||||||
print('\x1b[?25l')
|
print('\x1b[?25l')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
// clear_previous_line - useful for progressbars.
|
// clear_previous_line - useful for progressbars.
|
||||||
|
@ -105,4 +112,5 @@ pub fn hide_cursor() {
|
||||||
// the previous content.
|
// the previous content.
|
||||||
pub fn clear_previous_line() {
|
pub fn clear_previous_line() {
|
||||||
print('\r\x1b[1A\x1b[2K')
|
print('\r\x1b[1A\x1b[2K')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,6 +52,7 @@ pub fn get_cursor_position() ?Coord {
|
||||||
unsafe { C.tcsetattr(0, C.TCSANOW, &state) }
|
unsafe { C.tcsetattr(0, C.TCSANOW, &state) }
|
||||||
|
|
||||||
print('\e[6n')
|
print('\e[6n')
|
||||||
|
flush_stdout()
|
||||||
|
|
||||||
mut x := 0
|
mut x := 0
|
||||||
mut y := 0
|
mut y := 0
|
||||||
|
@ -87,6 +88,7 @@ pub fn set_terminal_title(title string) bool {
|
||||||
print('\033]0')
|
print('\033]0')
|
||||||
print(title)
|
print(title)
|
||||||
print('\007')
|
print('\007')
|
||||||
|
flush_stdout()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,4 +96,5 @@ pub fn set_terminal_title(title string) bool {
|
||||||
pub fn clear() {
|
pub fn clear() {
|
||||||
print('\x1b[2J')
|
print('\x1b[2J')
|
||||||
print('\x1b[H')
|
print('\x1b[H')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,12 +32,14 @@ pub fn init(cfg Config) &Context {
|
||||||
fn save_title() {
|
fn save_title() {
|
||||||
// restore the previously saved terminal title
|
// restore the previously saved terminal title
|
||||||
print('\x1b[22;0t')
|
print('\x1b[22;0t')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
[inline]
|
||||||
fn load_title() {
|
fn load_title() {
|
||||||
// restore the previously saved terminal title
|
// restore the previously saved terminal title
|
||||||
print('\x1b[23;0t')
|
print('\x1b[23;0t')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut ctx Context) run() ? {
|
pub fn (mut ctx Context) run() ? {
|
||||||
|
|
|
@ -26,6 +26,7 @@ fn restore_terminal_state() {
|
||||||
// clear the terminal and set the cursor to the origin
|
// clear the terminal and set the cursor to the origin
|
||||||
print('\x1b[2J\x1b[3J')
|
print('\x1b[2J\x1b[3J')
|
||||||
print('\x1b[?1049l')
|
print('\x1b[?1049l')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
C.SetConsoleMode(ui.ctx_ptr.stdin_handle, ui.stdin_at_startup)
|
C.SetConsoleMode(ui.ctx_ptr.stdin_handle, ui.stdin_at_startup)
|
||||||
}
|
}
|
||||||
|
@ -65,6 +66,7 @@ pub fn init(cfg Config) &Context {
|
||||||
print('\x1b[?1049h')
|
print('\x1b[?1049h')
|
||||||
// clear the terminal and set the cursor to the origin
|
// clear the terminal and set the cursor to the origin
|
||||||
print('\x1b[2J\x1b[3J\x1b[1;1H')
|
print('\x1b[2J\x1b[3J\x1b[1;1H')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.cfg.hide_cursor {
|
if ctx.cfg.hide_cursor {
|
||||||
|
@ -74,6 +76,7 @@ pub fn init(cfg Config) &Context {
|
||||||
|
|
||||||
if ctx.cfg.window_title != '' {
|
if ctx.cfg.window_title != '' {
|
||||||
print('\x1b]0;$ctx.cfg.window_title\x07')
|
print('\x1b]0;$ctx.cfg.window_title\x07')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -317,10 +320,12 @@ fn (mut ctx Context) parse_events() {
|
||||||
fn save_title() {
|
fn save_title() {
|
||||||
// restore the previously saved terminal title
|
// restore the previously saved terminal title
|
||||||
print('\x1b[22;0t')
|
print('\x1b[22;0t')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
[inline]
|
||||||
fn load_title() {
|
fn load_title() {
|
||||||
// restore the previously saved terminal title
|
// restore the previously saved terminal title
|
||||||
print('\x1b[23;0t')
|
print('\x1b[23;0t')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,6 +79,7 @@ fn (mut ctx Context) termios_setup() ? {
|
||||||
|
|
||||||
if ctx.cfg.window_title != '' {
|
if ctx.cfg.window_title != '' {
|
||||||
print('\x1b]0;$ctx.cfg.window_title\x07')
|
print('\x1b]0;$ctx.cfg.window_title\x07')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ctx.cfg.skip_init_checks {
|
if !ctx.cfg.skip_init_checks {
|
||||||
|
@ -90,6 +91,7 @@ fn (mut ctx Context) termios_setup() ? {
|
||||||
// feature-test the SU spec
|
// feature-test the SU spec
|
||||||
sx, sy := get_cursor_position()
|
sx, sy := get_cursor_position()
|
||||||
print('$bsu$esu')
|
print('$bsu$esu')
|
||||||
|
flush_stdout()
|
||||||
ex, ey := get_cursor_position()
|
ex, ey := get_cursor_position()
|
||||||
if sx == ex && sy == ey {
|
if sx == ex && sy == ey {
|
||||||
// the terminal either ignored or handled the sequence properly, enable SU
|
// 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)
|
C.tcsetattr(C.STDIN_FILENO, C.TCSAFLUSH, &termios)
|
||||||
// enable mouse input
|
// enable mouse input
|
||||||
print('\x1b[?1003h\x1b[?1006h')
|
print('\x1b[?1003h\x1b[?1006h')
|
||||||
|
flush_stdout()
|
||||||
if ctx.cfg.use_alternate_buffer {
|
if ctx.cfg.use_alternate_buffer {
|
||||||
// switch to the alternate buffer
|
// switch to the alternate buffer
|
||||||
print('\x1b[?1049h')
|
print('\x1b[?1049h')
|
||||||
|
flush_stdout()
|
||||||
// clear the terminal and set the cursor to the origin
|
// clear the terminal and set the cursor to the origin
|
||||||
print('\x1b[2J\x1b[3J\x1b[1;1H')
|
print('\x1b[2J\x1b[3J\x1b[1;1H')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
ctx.window_height, ctx.window_width = get_terminal_size()
|
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) {
|
fn get_cursor_position() (int, int) {
|
||||||
print('\033[6n')
|
print('\033[6n')
|
||||||
|
flush_stdout()
|
||||||
mut s := ''
|
mut s := ''
|
||||||
unsafe {
|
unsafe {
|
||||||
buf := malloc_noscan(25)
|
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
|
// set the bg color to some arbirtrary value (#010203), assumed not to be the default
|
||||||
print('\x1b[48:2:1:2:3m')
|
print('\x1b[48:2:1:2:3m')
|
||||||
|
flush_stdout()
|
||||||
// andquery the current color
|
// andquery the current color
|
||||||
print('\x1bP\$qm\x1b\\')
|
print('\x1bP\$qm\x1b\\')
|
||||||
|
flush_stdout()
|
||||||
mut s := ''
|
mut s := ''
|
||||||
unsafe {
|
unsafe {
|
||||||
buf := malloc_noscan(25)
|
buf := malloc_noscan(25)
|
||||||
|
@ -199,6 +207,7 @@ fn termios_reset() {
|
||||||
// C.TCSANOW ??
|
// C.TCSANOW ??
|
||||||
C.tcsetattr(C.STDIN_FILENO, C.TCSAFLUSH, &ui.termios_at_startup)
|
C.tcsetattr(C.STDIN_FILENO, C.TCSAFLUSH, &ui.termios_at_startup)
|
||||||
print('\x1b[?1003l\x1b[?1006l\x1b[?25h')
|
print('\x1b[?1003l\x1b[?1006l\x1b[?25h')
|
||||||
|
flush_stdout()
|
||||||
c := ctx_ptr
|
c := ctx_ptr
|
||||||
if unsafe { c != 0 } && c.cfg.use_alternate_buffer {
|
if unsafe { c != 0 } && c.cfg.use_alternate_buffer {
|
||||||
print('\x1b[?1049l')
|
print('\x1b[?1049l')
|
||||||
|
|
|
@ -116,6 +116,7 @@ pub fn (mut ctx Context) clear() {
|
||||||
[inline]
|
[inline]
|
||||||
pub fn (mut ctx Context) set_window_title(s string) {
|
pub fn (mut ctx Context) set_window_title(s string) {
|
||||||
print('\x1b]0;$s\x07')
|
print('\x1b]0;$s\x07')
|
||||||
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw_point draws a point at position `x`,`y`.
|
// draw_point draws a point at position `x`,`y`.
|
||||||
|
|
Loading…
Reference in New Issue