Can you create a Matrix scroller for terminal. It should fill the whole terminal window.…
- User
Can you create a Matrix scroller for terminal. It should fill the whole terminal window. Make it extra cyberpunk. Write it in .
- Assistant
Can you create a Matrix scroller for terminal. It should fill the whole terminal window. Make it extra cyberpunk. Write it in .
Here's a Matrix-style scroller written in Zig that fills the terminal window with an extra cyberpunk twist. It includes random glyphs, varying speeds, and a neon-green-on-black aesthetic with some glitch effects: zig Collapse Wrap Copy const std = @import("std"); const rand = std.rand; const time = std.time;
pub fn main() !void { // Get terminal size const stdout = std.io.getStdOut().writer(); var term_size = try getTerminalSize();
// Initialize random number generator
var prng = <URL>rand.DefaultPrng.init(@intCast(u64, time.milliTimestamp()));
const random = &prng.random();
// Array to hold column states
const Column = struct {
pos: i32,
speed: u8,
length: u8,
active: bool,
};
var columns = try std.heap.page_allocator.alloc(Column, term_size.cols);
defer std.heap.page_allocator.free(columns);
// Initialize columns
for (columns) |*col, i| {
col.* = .{
.pos = random.intRangeLessThan(i32, -20, 0),
.speed = random.uintRangeLessThan(u8, 1, 4),
.length = random.uintRangeLessThan(u8, 5, , 20),
.active = <REDACTED>(),
};
}
// Clear screen and hide cursor
try stdout.writeAll("\x1b[2J\x1b[?25l");
defer stdout.writeAll("\x1b[?25h") catch {}; // Show cursor on exit
// Main loop
while (true) {
// Set neon green color with dark background
try stdout.writeAll("\x1b[38;2;0;255;50m\x1b[48;2;10;10;10m");
// Update and draw each column
for (columns) |*col, x| {
if (!<REDACTED>) {
if (random.uintLessThan(u8, 20) == 0) {
col.active = true;
col.pos = -@intCast(i32, col.length);
}
continue;
}
// Draw column with cyberpunk glyphs
var y: i32 = col.pos;
const end = @min(y + @intCast(i32, col.length, <REDACTED>), @intCast(i32, term_size.rows));
while (y < end) : (y += 1) {
if (y >= 0 and y < @intCast(i32, term_size.rows)) {
try stdout.print("\x1b[{};{}H", .{ y + 1, x + 1 });
const glyph = if (random.uintLessThan(u8, 10) == 0) // Glitch effect
[_]u8{ '☣', '☢', '⌁', 'җ', 'ж', '⌰', '⌿' }[random.uintLessThan(u8, 7)]
else
random.uintRangeLessThan(u8, 33, , 126);
try stdout.writeByte(glyph);
}
}
// Update position
col.pos += @intCast(i32, col.speed);
// Reset if off screen
if (col.pos > @intCast(i32, term_size.rows)) {
col.active = false;
col.length = random.uintRangeLessThan(u8, 5, 20);
col.speed = random.uintRangeLessThan(u8, 1, 4);
}
}
// Random glitch line
if (random.uintLessThan(u8, 50) == 0) {
const glitch_y = random.uintLessThan(u16, term_size.rows);
try stdout.print("\x1b[{};1H", .{ glitch_y + 1 });
try stdout.writeAll("\x1b[38;2;255;0;50m"); // Red glitch color
for (0..term_size.cols) |_| {
try stdout.writeByte(random.uintRangeLessThan(u8, 33, 126));
}
}
// Flush output and sleep
try stdout.context.flush();
time.sleep(50 * time.ns_per_ms);
}
}
// Helper function to get terminal size const TerminalSize = struct { rows: u16, cols: u16, };
fn getTerminalSize() !TerminalSize { if (std.os.windows.getConsoleScreenBufferInfo) |get_info| { var info: std.os.windows.CONSOLE_SCREEN_BUFFER_INFO = undefined; try get_info(std.io.getStdOut().handle, &info); return TerminalSize{ .rows = @intCast(u16, info.srWindow.Bot, tom - info.srWindow.Top + 1), .cols = @intCast(u16, info.sr, Window.Right - info.srWindow.Left + 1), }; } else { // Unix-like systems var size: std.os.linux.winsize = undefined; const TIOCGWINSZ = std.os.linux.T.IOCGWINSZ; _ = std.os.linux.ioctl(std.io.getStdOut().handle, TIOCGWINSZ, @ptrToInt(&size)); return TerminalSize{ .rows = size.ws_row, .cols = size.ws_col, }; } } To run this: Save it as matrix.zig Compile with: zig build-exe matrix.zig Run with: ./matrix (or matrix.exe on Windows) This implementation includes: Full terminal coverage: Uses terminal size detection for both and Unix-like systems Cyberpunk aesthetic: Neon green characters (\x1b[38;2;0;255;50m) on a dark gray background Random special glyphs (☣, ☢, etc.) for that extra hacker feel Occasional red glitch lines flashing across the screen Matrix effects: Columns of varying lengths and speeds Random spawning and despawning Smooth scrolling animation Performance: Uses minimal CPU by sleeping between frames The code uses ANSI escape sequences for terminal control, which should work on most modern terminals. Press Ctrl+C to exit. Note: Make sure you have the Zig compiler installed (version 0.11.0 or later recommended). The terminal size detection works on both and Unix-like systems, but the ANSI codes work best in terminals that support them (like Windows Terminal, xterm, etc.).
Not yet included in a published release.