diff options
author | Josias <justjosias@tutanota.com> | 2020-04-17 14:30:10 +0300 |
---|---|---|
committer | Josias <justjosias@tutanota.com> | 2020-04-17 14:30:10 +0300 |
commit | 6ab79c2cfba261d495f2b6aebfb4fba5a8ba2de9 (patch) | |
tree | 8813aca0bc64acb35f6fab8f693a37595e9fead6 |
Add basic program
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | LICENSE | 21 | ||||
-rw-r--r-- | curses.zig | 90 |
3 files changed, 114 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..765d08a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +zigcurses +zig_cache @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Josias Allestad <justjosias@tutanota.com> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/curses.zig b/curses.zig new file mode 100644 index 0000000..cc700c5 --- /dev/null +++ b/curses.zig @@ -0,0 +1,90 @@ +const std = @import("std"); +const os = std.os; +const warn = std.debug.warn; +const termios = os.termios; +const ascii = std.ascii; + +const STDIN_FILENO = 0; + +// I need to figure out how to implement this +//pub fn CTRL_KEY(k: u8) u8 { +// return ((k) & 0x1f); +//} + +const Window = struct { + original: termios, + + pub fn init() !Window { + var original_termios = try os.tcgetattr(STDIN_FILENO); + var raw = original_termios; + + raw.iflag &= ~(@as(u8, os.BRKINT) | @as(u16, os.ICRNL) | + @as(u8, os.INPCK) | @as(u8, os.ISTRIP) | @as(u16, os.IXON)); + raw.oflag &= ~(@as(u8, os.OPOST)); + raw.cflag |= (os.CS8); + raw.lflag &= ~(@as(u8, os.ECHO) | @as(u8, os.ICANON) | + @as(u16, os.IEXTEN) | @as(u8, os.ISIG)); + + // My attempt at VTIME and VMIN + raw.cc[5] = 0; + raw.cc[7] = 1; + + try os.tcsetattr(STDIN_FILENO, os.TCSA.FLUSH, raw); + + return Window{ + .original = original_termios, + }; + } + + pub fn end(self: Window) !void { + try os.tcsetattr(0, os.TCSA.FLUSH, self.original); + } +}; + +test "Window open and close" { + var window = try Window.init(); + try window.end(); +} + +pub fn readKey() !u8 { + const in_stream = std.io.getStdOut().inStream(); + var c: u8 = try in_stream.readByte(); + return c; +} + +test "try to read a key" { + var window = try Window.init(); + + warn("\r\nEnter a key: ", .{}); + var c: u8 = try readKey(); + warn("You entered: {}\n", .{c}); + + try window.end(); +} + +// An example for main -- to be moved to an example directory +pub fn main() !void { + const in_stream = std.io.getStdOut().inStream(); + + var w = try Window.init(); + while (true) { + var c: u8 = undefined; + c = try in_stream.readByte(); + + if (ascii.isCntrl(c)) { + warn("It's control! {}\r\n", .{c}); + } else { + warn("{}\r\n", .{c}); + } + + if (ascii.isCntrl(c) and c == 'q') { + break; + } + //warn("{}\n", .{CTRL_KEY(c)}); + if (c == 17) { + break; + } + } + + try w.end(); +} |