Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The stdout.write doesn't print to terminal until new line symbol is sent #23632

Closed
Xkonti opened this issue May 21, 2024 · 3 comments
Closed

Comments

@Xkonti
Copy link

Xkonti commented May 21, 2024

Description

The stdout.write doesn't actually print out to the terminal window until the \n newline symbol has been "written". Because of that, it's impossible to print something to terminal output without moving the cursor to the new line.

Procs and macros that depend on writing to stdout have the same issue, for example styledWrite in std/terminal.

Nim Version

Nim Compiler Version 2.0.4 [Linux: amd64]
Compiled at 2024-03-28

Operating system

Windows 11 WSL2 Ubuntu 22.04.3 LTS

Current Output

stdout.write("1") # Terminal: ""
sleep(1000)
stdout.write("2") # Terminal: ""
sleep(1000)
stdout.write("3") # Terminal: ""
sleep(1000)
stdout.write("\n4") # Terminal: "123\n"
sleep(1000)
stdout.write("5") # Terminal: "123"
sleep(1000)
stdout.write("6") # Terminal: "123"
sleep(1000)
stdout.write('\n') # Terminal: "123\n456\n"

Expected Output

stdout.write("1") # Terminal: "1"
sleep(1000)
stdout.write("2") # Terminal: "2"
sleep(1000)
stdout.write("3") # Terminal: "3"
sleep(1000)
stdout.write("\n4") # Terminal: "123\n4"
sleep(1000)
stdout.write("5") # Terminal: "123\n45"
sleep(1000)
stdout.write("6") # Terminal: "123\n456"
sleep(1000)
stdout.write('\n') # Terminal: "123\n456\n"

Possible Solution

No response

Additional Information

The equivalent in Zig works properly in the same WSL2 environment:

const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("1", .{});
    std.time.sleep(1_000_000_000); // 1 second in nanoseconds
    try stdout.print("2", .{});
    std.time.sleep(1_000_000_000);
    try stdout.print("3", .{});
    std.time.sleep(1_000_000_000);
    try stdout.print("\n4", .{});
    std.time.sleep(1_000_000_000);
    try stdout.print("5", .{});
    std.time.sleep(1_000_000_000);
    try stdout.print("6", .{});
    std.time.sleep(1_000_000_000);
    try stdout.print("\n", .{});
}

The flushFile proc makes it work as intended:

stdout.write("1")
stdout.flushFile()
sleep(1000)
stdout.write("2")
stdout.flushFile()
sleep(1000)
stdout.write("3")
stdout.flushFile()
sleep(1000)
stdout.write("\n4")
stdout.flushFile()
sleep(1000)
stdout.write("5")
stdout.flushFile()
sleep(1000)
stdout.write("6")
stdout.flushFile()
sleep(1000)
stdout.write('\n')
stdout.flushFile()
@Araq
Copy link
Member

Araq commented May 21, 2024

Use flushFile.

@Araq Araq closed this as completed May 21, 2024
@Xkonti
Copy link
Author

Xkonti commented May 31, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants