Created
August 2, 2023 12:14
-
-
Save alberic89/54fd0ccf4ed097e6a4a7a65870dc8212 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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