Yesterday I thought of putting together my recent learning’s about ANSI escape codes and zig.
In my last blog, I used ANSI escape codes to give color to the text in terminal. In this exercise, I will use [1A
to erase start of line to the cursor so that I can build a basic progress indicator for the terminal.
Disclaimer : This is for learning purposes only, if you like to use a progress indicator with zig for your app, I suggest you to look into Std.Progress
const std = @import("std");
pub fn main() !void {
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
var progress = [10]u8{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' };
for (1..101) |i| {
if (i % 10 == 0) {
const a: usize = i / 10;
progress[a - 1] = ':';
}
try stdout.print("\u{001b}[1AProcessing : [{s}] {d}%\n", .{ progress, i });
try bw.flush();
std.time.sleep(60 * 1000 * 1000);
}
try bw.flush();
}
Here is the small preview of the output.
I hope it helped.
Thank You.
Versions of Language/packages used in this post.
Library/Language | Version |
---|---|
Zig | 0.11.0 |