summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosias <justjosias@tutanota.com>2020-06-26 16:04:53 +0000
committerJosias <justjosias@tutanota.com>2020-06-26 16:04:53 +0000
commit979124241566666109dda7fadcc90db1fb67e472 (patch)
treedf1732f69d97d0d73706586e5618d8da573d0a1f
parent4bff7abeb57fbaf6158c725f8eeb6ce1e7bfbd98 (diff)
Clean up
-rw-r--r--.gitignore1
-rwxr-xr-xexamplebin865856 -> 0 bytes
-rw-r--r--example.zig2
-rw-r--r--example.zig~38
-rwxr-xr-xsrc/cursebin737608 -> 0 bytes
-rw-r--r--src/curse.zig17
-rw-r--r--src/curse.zig~9
-rw-r--r--src/gapbuffer.zig2
-rw-r--r--src/gapbuffer.zig~149
-rw-r--r--src/main.zig3
-rw-r--r--src/main.zig~181
-rw-r--r--src/new.txt150
-rw-r--r--src/stack.zig~66
13 files changed, 5 insertions, 613 deletions
diff --git a/.gitignore b/.gitignore
index 81e7ed7..f11cdde 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
*.o
curses
zig-cache/
+*~
diff --git a/example b/example
deleted file mode 100755
index 61fbdc3..0000000
--- a/example
+++ /dev/null
Binary files differ
diff --git a/example.zig b/example.zig
index 18ca39b..046564b 100644
--- a/example.zig
+++ b/example.zig
@@ -1,6 +1,6 @@
const std = @import("std");
const ascii = std.ascii;
-const print = std.debug.print;
+const print = std.debug.warn;
const curses = @import("src/main.zig");
diff --git a/example.zig~ b/example.zig~
deleted file mode 100644
index ecf0816..0000000
--- a/example.zig~
+++ /dev/null
@@ -1,38 +0,0 @@
-const std = @import("std");
-const ascii = std.ascii;
-const warn = std.debug.warn;
-
-const curses = @import("src/main.zig");
-
-pub fn main() anyerror!void {
- const in_stream = std.io.getStdOut().inStream();
-
- var w = try curses.Window.init(std.testing.allocator);
- while (true) {
- var c: u8 = undefined;
- c = try in_stream.readByte();
-
- if (ascii.isCntrl(c)) {
- warn("It's control! {}\r\n", .{c});
- var text = "hi\n";
-
- try w.mvprint(0, 0, text[0..]);
- } else {
- warn("{}\r\n", .{c});
- }
-
- if (ascii.isCntrl(c) and c == 'q') {
- break;
- }
-
- if (c == 'c') {
- try w.clear();
- }
- //warn("{}\n", .{CTRL_KEY(c)});
- if (c == 17) {
- break;
- }
- }
-
- try w.end();
-}
diff --git a/src/curse b/src/curse
deleted file mode 100755
index ea03e2b..0000000
--- a/src/curse
+++ /dev/null
Binary files differ
diff --git a/src/curse.zig b/src/curse.zig
deleted file mode 100644
index 51bdbf5..0000000
--- a/src/curse.zig
+++ /dev/null
@@ -1,17 +0,0 @@
-const print = @import("std").debug.print;
-
-const c = @cImport({
- // See https://github.com/ziglang/zig/issues/515
- @cDefine("_NO_CRT_STDIO_INLINE", "1");
- @cInclude("ncurses.h");
- @cInclude("stdio.h");
-});
-pub fn main() void {
- // _ = c.printf("hello\n");
- _ = c.initscr();
- _ = c.mvprintw(0, 0, "Hi");
- _ = c.endwin();
- _ = c.printf("HA\n");
-
- print("hi\n", .{});
-}
diff --git a/src/curse.zig~ b/src/curse.zig~
deleted file mode 100644
index d1e9c34..0000000
--- a/src/curse.zig~
+++ /dev/null
@@ -1,9 +0,0 @@
-const c = @cImport({
- // See https://github.com/ziglang/zig/issues/515
- @cDefine("_NO_CRT_STDIO_INLINE", "1");
- @cInclude("ncurses.h");
-});
-pub fn main() void {
- // _ = c.printf("hello\n");
- c.initscr();
-}
diff --git a/src/gapbuffer.zig b/src/gapbuffer.zig
index 9e112f4..9793d6a 100644
--- a/src/gapbuffer.zig
+++ b/src/gapbuffer.zig
@@ -1,5 +1,5 @@
const std = @import("std");
-const print = std.debug.print;
+const print = std.debug.warn;
const GapBuffer = struct {
buffer: []u32,
diff --git a/src/gapbuffer.zig~ b/src/gapbuffer.zig~
deleted file mode 100644
index cccce9c..0000000
--- a/src/gapbuffer.zig~
+++ /dev/null
@@ -1,149 +0,0 @@
-const std = @import("std");
-const print = std.debug.print;
-
-const GapBuffer = struct {
- buffer: []u32,
- leftIndex: usize,
- rightIndex: usize,
-
- allocator: *std.mem.Allocator,
-
- // the caller is responsible for freeing memory
- pub fn init(allocator: *std.mem.Allocator, buffer: []u32) !GapBuffer {
- var newbuffer = try allocator.alloc(u32, buffer.len * 2);
-
- for (buffer) |c, i| {
- newbuffer[i] = c;
- }
-
- return GapBuffer{
- .buffer = newbuffer,
- .leftIndex = buffer.len,
- .rightIndex = 0,
-
- .allocator = allocator,
- };
- }
-
- pub fn resize() void {}
-
- pub fn left(self: *GapBuffer) !void {
- // if (self.leftIndex == self.buffer.len + 1) {
- // return error.StackOverflow;
- // }
- // self.leftIndex -= 1;
-
- // self.buffer[self.leftIndex] = self.buffer[self.rightIndex];
-
- // if (self.rightIndex == 0) {
- // return error.StackUnderflow;
- // }
- // self.rightIndex -= 1;
- if (self.leftIndex == 0) {
- return error.StackUnderflow;
- }
-
- self.rightIndex += 1;
- self.leftIndex -= 1;
-
- self.buffer[self.buffer.len - self.rightIndex] = self.buffer[self.leftIndex];
-
- self.buffer[self.leftIndex] = 0;
- }
-
- pub fn right(self: *GapBuffer) !void {
- // if (self.rightIndex == 0) {
- // return error.StackUnderflow;
- // }
- // self.rightIndex += 1;
-
- // self.buffer[self.rightIndex] = self.buffer[self.leftIndex];
-
- // if (self.leftIndex == self.buffer.len + 1) {
- // return error.StackOverflow;
- // }
- // self.leftIndex += 1;
- if (self.rightIndex == 0) {
- return error.StackOverflow;
- }
-
- self.leftIndex += 1;
-
- self.buffer[self.leftIndex] = self.buffer[self.buffer.len - self.rightIndex];
-
- self.rightIndex -= 1;
- }
-
- pub fn delete(self: *GapBuffer) !u32 {
- if (self.leftIndex == 0) {
- return error.StackUnderflow;
- }
- self.buffer[self.leftIndex] = 0;
- self.leftIndex -= 1;
- return self.buffer[self.leftIndex];
- }
-
- pub fn insert(self: *GapBuffer, item: u32) !void {
- self.buffer[self.rightIndex] = item;
- self.rightIndex += 1;
- }
-
- /// returns the non-buffer parts of the slice
- pub fn slice(self: GapBuffer) []u32 {
- var buf = self.allocator.alloc(u32, self.buffer.len) catch unreachable;
- //var buf = self.buffer;
- var i: usize = 0;
- while (i < self.leftIndex) : (i += 1) {
- buf[i] = self.buffer[i];
- }
-
- var n: usize = self.rightIndex;
- while (n < self.buffer.len) : ({
- n += 1;
- i += 1;
- }) {
- print(" {} {} \n", .{ i, self.buffer[n] });
- buf[i] = self.buffer[n];
- }
-
- return buf;
- }
-};
-
-test "" {
- const assert = std.debug.assert;
-
- //var buffer: [100]u32 = undefined;
- var buffer = [_]u32{ 2, 3, 4, 1, 5, 6, 5, 1, 5, 2, 5, 1, 58, 433, 562, 45, 253, 23, 4, 4, 1, 5, 7, 45, 50 };
- var b = try GapBuffer.init(std.testing.allocator, buffer[0..]);
-
- print("\n{}\n", .{b});
-
- assert(b.buffer[b.leftIndex - 1] == 50);
-
- assert(b.buffer[0] == 2);
-
- try b.left();
- print("Current: {}\n", .{buffer[b.leftIndex]});
- print("P: {}\n", .{b.leftIndex});
- assert(b.buffer[b.leftIndex - 1] == 45);
-
- for (b.buffer) |c| {
- print("{} ", .{c});
- }
-
- var slice = b.slice();
- for (slice) |c| {
- print("{} ", .{c});
- }
- b.allocator.free(slice);
-
- print("\n", .{});
-
- assert((try b.delete()) == 45);
-
- try b.left();
- assert(b.buffer[b.leftIndex - 1] == 5);
-
- std.testing.allocator.free(b.buffer);
-}
diff --git a/src/main.zig b/src/main.zig
index 53b3e74..1030dba 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -1,6 +1,7 @@
const std = @import("std");
const os = std.os;
-const print = std.debug.print;
+const warn = std.debug.warn;
+const print = warn;
const termios = os.termios;
const ascii = std.ascii;
diff --git a/src/main.zig~ b/src/main.zig~
deleted file mode 100644
index e01051b..0000000
--- a/src/main.zig~
+++ /dev/null
@@ -1,181 +0,0 @@
-const std = @import("std");
-const os = std.os;
-const print = std.debug.print;
-const termios = os.termios;
-const ascii = std.ascii;
-
-const STDIN_FILENO = 0;
-const stdout = std.io.getStdOut().outStream();
-const in_stream = std.io.getStdOut().inStream();
-
-//pub fn new(allocator: *Allocator) !Window {}
-
-pub const Window = struct {
- original: termios,
- height: usize,
- width: usize,
- buffer: []u8,
- /// buf contains a array of slices
- buf: []u8,
-
- //allocator: *std.mem.Allocator,
-
- /// init puts the window in raw mode and saves the original termios for switching back to normal mode
- pub fn init(allocator: *std.mem.Allocator) !Window {
- var window = Window{
- .original = try os.tcgetattr(STDIN_FILENO),
- .buffer = "",
- .buf = "",
- .height = 0,
- .width = 0,
- };
-
- var raw = window.original;
-
- raw.iflag &= ~(@as(u16, os.BRKINT | os.ICRNL | os.INPCK | os.ISTRIP | os.IXON));
- raw.oflag &= ~(@as(u8, os.OPOST));
- raw.cflag |= (os.CS8);
- raw.lflag &= ~(@as(u16, os.ECHO | os.ICANON | os.IEXTEN | 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);
-
- if (!os.isatty(STDIN_FILENO)) {
- return error.NotATTY;
- }
-
- const arrayList = std.ArrayList([]u8);
- var buf = arrayList.init(allocator);
- _ = try buf.append("h"[0..]);
- _ = try buf.append("i"[0..]);
- print("What? {}\n", .{buf.items});
-
- buf.deinit();
-
- //const wh = try getmax();
- // fill in the height and width information
- try window.updatehw();
-
- var text = try allocator.alloc(u8, window.height * window.width);
- //warn("{}", .{text});
- defer allocator.free(text);
-
- var n: usize = 0;
- while (n < window.height) : (n += 1) {
- print("{}", .{n});
- text[n] = 'h';
-
- //warn("{}", .{});
-
- if (n % window.width == 0) {
- text[n * window.width] = 'c';
- }
-
- // var i: usize = 0;
- // while (i < window.width) : (i += 1) {
- // if (i == window.width) {
- // text[n] = '\n';
- // }
- // }
- }
-
- print("{}", .{text});
-
- // initialize text with spaces and newlines
- //for ([]usize{ 0, 1, 2, 3, 4, 10 }) |i| {}
-
- try window.clear();
-
- // disable cursor
- _ = try stdout.write("\x1B[?25l");
-
- var i: usize = 0;
-
- while (i < window.height) {
- _ = try stdout.write("\r\n~");
- i += 1;
- }
-
- return window;
- }
-
- /// updatehw gets the height and width for the window by making a syscall.
- pub fn updatehw(self: *Window) !void {
- // TODO: support other platforms and provide fallback
- var wsz: os.winsize = undefined;
- // TODO: test for false and return error
- _ = std.os.linux.syscall3(.ioctl, @bitCast(usize, @as(isize, 0)), os.TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
- //const te = [2]u32{ wsz.ws_col, wsz.ws_row };
- self.height = wsz.ws_row;
- self.width = wsz.ws_col;
- }
-
- /// setchar puts a character at a specific point in the text based on height and width
- pub fn setchar(self: *Window, x: u32, y: u32, insert: u8) !void {
- if (x > self.width or y > self.height) {
- return error.InvalidRange;
- }
-
- var newlines: usize = 0;
- for (self.buffer) |char, i| {
- if (newlines == y) {
- for (self.buffer[i + 1 .. hw[1]]) |character, n| {
- if (character == '\n' or character == '\r') {
- break;
- }
-
- self.buffer[n] = insert;
- }
- }
- }
- }
-
- pub fn mvprint(self: Window, x: usize, y: usize, text: []const u8) !void {
- _ = try stdout.write("\x1b[10;10H");
- _ = try stdout.write(text);
- //_ = try buf.append('');
- }
-
- pub fn clear(self: Window) !void {
- _ = try stdout.write("\x1b[2J");
- _ = try stdout.write("\x1b[H");
- }
-
- pub fn erase() !void {}
-
- pub fn flush() !void {}
-
- pub fn refresh(self: Window) !void {
- _ = stdout.write(self.buffer);
- }
-
- pub fn end(self: Window) !void {
- // re-enable cursor
- _ = try stdout.write("\x1B[?25h");
- try self.clear();
- try os.tcsetattr(STDIN_FILENO, os.TCSA.FLUSH, self.original);
- }
-};
-
-test "Window open and close" {
- var window = try Window.init(std.testing.allocator);
- try window.end();
-}
-
-pub fn readKey() !u8 {
- var c: u8 = try in_stream.readByte();
- return c;
-}
-
-test "try to read a key" {
- var window = try Window.init(std.testing.allocator);
-
- print("\r\nEnter a key: ", .{});
- const c: u8 = try readKey();
- print("You entered: {}\n", .{c});
-
- try window.end();
-}
diff --git a/src/new.txt b/src/new.txt
deleted file mode 100644
index 2e5cfbb..0000000
--- a/src/new.txt
+++ /dev/null
@@ -1,150 +0,0 @@
-/src/main.zig:13:12: error: missing parameter name
-pub fn new(*Allocator) Window {
- ^
-./src/main.zig:6:19: note: referenced here
-const Allocator = std.mem.Allocator;
- ^
-./src/main.zig:13:13: note: referenced here
-pub fn new(*Allocator) Window {
- ^
-./src/main.zig:13:24: note: referenced here
-pub fn new(*Allocator) Window {
- ^
-./src/main.zig:4:17: note: referenced here
-const termios = os.termios;
- ^
-./src/main.zig:4:19: note: referenced here
-const termios = os.termios;
- ^
-./src/main.zig:73:15: note: referenced here
- original: termios,
- ^
-/home/josias/git/zig-bin/lib/zig/std/os/bits/linux.zig:1699:12: note: referenced here
- iflag: tcflag_t,
- ^
-/home/josias/git/zig-bin/lib/zig/std/os/bits/linux.zig:1703:11: note: referenced here
- line: cc_t,
- ^
-/home/josias/git/zig-bin/lib/zig/std/os/bits/linux.zig:1704:10: note: referenced here
- cc: [NCCS]cc_t,
- ^
-/home/josias/git/zig-bin/lib/zig/std/os/bits/linux.zig:1705:13: note: referenced here
- ispeed: speed_t,
- ^
-./example.zig:10:19: note: referenced here
- var w = curses.new(std.testing.allocator);
- ^
-./example.zig:10:27: note: referenced here
- var w = curses.new(std.testing.allocator);
- ^
-/home/josias/git/zig-bin/lib/zig/std/testing.zig:4:75: note: referenced here
-pub const LeakCountAllocator = @import("testing/leak_count_allocator.zig").LeakCountAllocator;
- ^
-/home/josias/git/zig-bin/lib/zig/std/testing.zig:9:30: note: referenced here
-pub var allocator_instance = LeakCountAllocator.init(&base_allocator_instance.allocator);
- ^
-/home/josias/git/zig-bin/lib/zig/std/testing/leak_count_allocator.zig:13:29: note: referenced here
- pub fn init(allocator: *std.mem.Allocator) LeakCountAllocator {
- ^
-/home/josias/git/zig-bin/lib/zig/std/testing.zig:9:48: note: referenced here
-pub var allocator_instance = LeakCountAllocator.init(&base_allocator_instance.allocator);
- ^
-/home/josias/git/zig-bin/lib/zig/std/testing.zig:14:35: note: referenced here
-pub var base_allocator_instance = std.heap.ThreadSafeFixedBufferAllocator.init(allocator_mem[0..]);
- ^
-/home/josias/git/zig-bin/lib/zig/std/testing.zig:14:43: note: referenced here
-pub var base_allocator_instance = std.heap.ThreadSafeFixedBufferAllocator.init(allocator_mem[0..]);
- ^
-/home/josias/git/zig-bin/lib/zig/std/testing.zig:14:74: note: referenced here
-pub var base_allocator_instance = std.heap.ThreadSafeFixedBufferAllocator.init(allocator_mem[0..]);
- ^
-/home/josias/git/zig-bin/lib/zig/std/testing.zig:14:80: note: referenced here
-pub var base_allocator_instance = std.heap.ThreadSafeFixedBufferAllocator.init(allocator_mem[0..]);
- ^
-/home/josias/git/zig-bin/lib/zig/std/heap.zig:584:38: note: referenced here
- .reallocFn = realloc,
- ^
-/home/josias/git/zig-bin/lib/zig/std/heap.zig:612:40: note: referenced here
- const result = try alloc(allocator, new_size, new_align);
- ^
-/home/josias/git/zig-bin/lib/zig/std/heap.zig:585:37: note: referenced here
- .shrinkFn = shrink,
- ^
-/home/josias/git/zig-bin/lib/zig/std/testing.zig:9:55: note: referenced here
-pub var allocator_instance = LeakCountAllocator.init(&base_allocator_instance.allocator);
- ^
-/home/josias/git/zig-bin/lib/zig/std/testing/leak_count_allocator.zig:17:30: note: referenced here
- .reallocFn = realloc,
- ^
-/home/josias/git/zig-bin/lib/zig/std/testing/leak_count_allocator.zig:18:29: note: referenced here
- .shrinkFn = shrink,
- ^
-/home/josias/git/zig-bin/lib/zig/std/testing.zig:8:24: note: referenced here
-pub const allocator = &allocator_instance.allocator;
- ^
-./example.zig:10:35: note: referenced here
- var w = curses.new(std.testing.allocator);
- ^
-/home/josias/git/zig-bin/lib/zig/std/io/reader.zig:175:31: note: referenced here
- pub fn readByte(self: Self) !u8 {
- ^
-./example.zig:13:26: note: referenced here
- c = try in_stream.readByte();
- ^
-/home/josias/git/zig-bin/lib/zig/std/io/reader.zig:26:47: note: referenced here
- pub fn read(self: Self, buffer: []u8) Error!usize {
- ^
-/home/josias/git/zig-bin/lib/zig/std/io/reader.zig:177:38: note: referenced here
- const amt_read = try self.read(result[0..]);
- ^
-./example.zig:2:18: note: referenced here
-const ascii = std.ascii;
- ^
-./example.zig:15:13: note: referenced here
- if (ascii.isCntrl(c)) {
- ^
-./example.zig:15:18: note: referenced here
- if (ascii.isCntrl(c)) {
- ^
-./example.zig:16:13: note: referenced here
- warn("It's control! {}\r\n", .{c});
- ^
-./example.zig:26:18: note: referenced here
- try w.clear();
- ^
-/home/josias/git/zig-bin/lib/zig/std/io.zig:54:19: note: referenced here
- .handle = getStdOutHandle(),
- ^
-/home/josias/git/zig-bin/lib/zig/std/io.zig:56:29: note: referenced here
- .intended_io_mode = default_mode,
- ^
-./src/main.zig:9:34: note: referenced here
-const stdout = std.io.getStdOut().outStream();
- ^
-./src/main.zig:113:17: note: referenced here
- _ = try stdout.write("\x1b[2J");
- ^
-./example.zig:34:10: note: referenced here
- try w.end();
- ^
-/home/josias/git/zig-bin/lib/zig/std/os.zig:4905:49: note: referenced here
-pub fn tcsetattr(handle: fd_t, optional_action: TCSA, termios_p: termios) TermiosSetError!void {
- ^
-/home/josias/git/zig-bin/lib/zig/std/os.zig:4903:29: note: referenced here
-pub const TermiosSetError = TermiosGetError || error{ProcessOrphaned};
- ^
-/home/josias/git/zig-bin/lib/zig/std/os.zig:4905:75: note: referenced here
-pub fn tcsetattr(handle: fd_t, optional_action: TCSA, termios_p: termios) TermiosSetError!void {
- ^
-./src/main.zig:127:15: note: referenced here
- try os.tcsetattr(STDIN_FILENO, os.TCSA.FLUSH, self.original);
- ^
-./src/main.zig:127:26: note: referenced here
- try os.tcsetattr(STDIN_FILENO, os.TCSA.FLUSH, self.original);
- ^
-./src/main.zig:15:21: error: expected type 'src.main.Window', found 'std.os.TermiosGetError'
- .original = try os.tcgetattr(STDIN_FILENO),
- ^
-./src/main.zig:72:20: note: src.main.Window declared here
-pub const Window = struct {
- ^
diff --git a/src/stack.zig~ b/src/stack.zig~
deleted file mode 100644
index 2c442a4..0000000
--- a/src/stack.zig~
+++ /dev/null
@@ -1,66 +0,0 @@
-const std = @import("std");
-const warn = std.debug.warn;
-const mem = std.mem;
-const assert = std.debug.assert;
-
-pub const Stack = struct {
- buffer: []u8,
- index: usize,
-
- pub fn init(buffer: []u8) Stack {
- return Stack{
- .buffer = buffer,
- .index = 0,
- };
- }
-
- pub fn pop(self: *Stack) !u8 {
- if (self.index == 0) return error.StackUnderflow;
- self.index -= 1;
- return self.buffer[self.index];
- }
-
- pub fn push(self: *Stack, item: u8) !void {
- if (self.index == 0xff) return error.StackOverflow;
- self.buffer[self.index] = item;
- self.index += 1;
- }
-
- /// slice returns a slice of the array ending at the current index
- pub fn slice(self: Stack) []u8 {
- return self.buffer[0..self.index];
- }
-};
-
-test "test stack" {
- var buffer: [100]u8 = undefined;
- var stack = Stack.init(buffer[0..]);
-
- try stack.push('h');
- try stack.push('e');
- try stack.push('l');
- try stack.push('l');
- try stack.push('o');
- try stack.push('!');
-
- assert(mem.eql(u8, stack.buffer[0..6], "hello!"));
- warn("\n{}\n", .{stack.slice()});
-
- const char = try stack.pop();
- assert(char == '!');
-
- try stack.push('?');
- assert(mem.eql(u8, stack.buffer[0..6], "hello?"));
- warn("\n{}\n", .{stack.slice()});
-
- _ = try stack.pop();
- _ = try stack.pop();
- _ = try stack.pop();
-
- _ = try stack.pop();
- _ = try stack.pop();
- _ = try stack.pop();
- _ = try stack.pop();
-
- warn("\n{}\n", .{stack.slice()});
-}