Skip to content

Instantly share code, notes, and snippets.

@alberic89
Created August 2, 2023 12:14
Show Gist options
  • Save alberic89/54fd0ccf4ed097e6a4a7a65870dc8212 to your computer and use it in GitHub Desktop.
Save alberic89/54fd0ccf4ed097e6a4a7a65870dc8212 to your computer and use it in GitHub Desktop.
const std = @import("std");
const debug = std.debug;
const fs = std.fs;
const io = std.io;
const mem = std.mem;
const os = std.os;
pub fn main() !void {
var tty = try fs.cwd().openFile("/dev/tty", .{ .mode = .read_write });
defer tty.close();
const original = try os.tcgetattr(tty.handle);
var raw = original;
raw.lflag &= ~@as(
os.linux.tcflag_t,
os.linux.ECHO | os.linux.ICANON | os.linux.ISIG | os.linux.IEXTEN,
);
raw.iflag &= ~@as(
os.linux.tcflag_t,
os.linux.IXON | os.linux.ICRNL | os.linux.BRKINT | os.linux.INPCK | os.linux.ISTRIP,
);
raw.cc[os.system.V.TIME] = 0;
raw.cc[os.system.V.MIN] = 1;
try os.tcsetattr(tty.handle, .FLUSH, raw);
while (true) {
var buffer: [1]u8 = undefined;
_ = try tty.read(&buffer);
if (buffer[0] == 'q') {
try os.tcsetattr(tty.handle, .FLUSH, original);
return;
} else if (buffer[0] == '\x1B') {
debug.print("input: escape\r\n", .{});
} else if (buffer[0] == '\n' or buffer[0] == '\r') {
debug.print("input: return\r\n", .{});
} else {
debug.print("input: {} {s}\r\n", .{ buffer[0], buffer });
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment