srctree

Gregory Mullen parent c64ff5e3 1811537a
simplify the tokeizer -> history api

src/fs.zig added: 25, removed: 21, total 4
@@ -120,12 +120,8 @@ pub fn inotifyInstall(self: *fs, target: []const u8, cb: ?INotify.Callback) !voi
pub fn inotifyInstallRc(self: *fs, cb: ?INotify.Callback) !void {
if (self.rc) |_| {
if (self.names.home) |home| {
// I know... I'm sorry you had to read this too
const cfile = "/.config/hsh/hshrc";
const path = try self.alloc.alloc(u8, home.len + cfile.len);
defer self.alloc.free(path);
@memcpy(path[0..home.len], home);
@memcpy(path[home.len..], cfile);
var buf: [2048]u8 = undefined;
var path = try std.fmt.bufPrint(&buf, "{s}/.config/hsh/hshrc", .{home});
try self.inotifyInstall(path, cb);
}
}
@@ -289,7 +285,7 @@ pub fn globAt(a: Allocator, dir: std.fs.IterableDir, search: []const u8) ![][]u8
 
/// Caller owns returned file
/// TODO remove allocator
pub fn findPath(
fn findPath(
a: Allocator,
env: *const std.process.EnvMap,
name: []const u8,
 
src/input.zig added: 25, removed: 21, total 4
@@ -339,25 +339,23 @@ fn history(in: *Input, tkn: *Tokenizer, k: Keys.Key) Event {
return .Redraw;
}
}
tkn.resetRaw();
_ = hist.readAtFiltered(&tkn.raw, in.hist_orig);
tkn.c_idx = tkn.raw.items.len;
_ = hist.readAtFiltered(tkn.lineReplaceHistory(), in.hist_orig);
tkn.cPos(.end);
return .Redraw;
},
.Down => {
if (hist.cnt > 1) {
hist.cnt -= 1;
tkn.resetRaw();
tkn.reset();
} else {
hist.cnt -|= 1;
tkn.resetRaw();
tkn.reset();
tkn.consumes(in.hist_orig) catch unreachable;
in.hist_orig = in.hist_data[0..0];
return .Redraw;
}
tkn.resetRaw();
_ = hist.readAtFiltered(&tkn.raw, in.hist_orig);
tkn.c_idx = tkn.raw.items.len;
_ = hist.readAtFiltered(tkn.lineReplaceHistory(), in.hist_orig);
tkn.cPos(.end);
return .Redraw;
},
else => unreachable,
 
src/logic.zig added: 25, removed: 21, total 4
@@ -49,7 +49,7 @@ fn execBody(a: Allocator, h: *HSH, body: []const u8) !void {
tzr.consumec(b) catch |err| {
if (err == tokenizer.Error.Exec) {
try exec_.exec(h, tzr.raw.items);
tzr.resetRaw();
tzr.reset();
}
};
}
 
src/tokenizer.zig added: 25, removed: 21, total 4
@@ -355,6 +355,11 @@ pub const Tokenizer = struct {
self.editor_mktmp = null;
}
 
pub fn lineReplaceHistory(self: *Tokenizer) *ArrayList(u8) {
self.resetRaw();
return &self.raw;
}
 
pub fn saveLine(self: *Tokenizer) void {
self.raw = ArrayList(u8).init(self.alloc);
self.c_idx = 0;
@@ -369,9 +374,10 @@ pub const Tokenizer = struct {
 
pub fn reset(self: *Tokenizer) void {
self.resetRaw();
self.resetPrevExec();
}
 
pub fn resetRaw(self: *Tokenizer) void {
fn resetRaw(self: *Tokenizer) void {
self.raw.clearRetainingCapacity();
self.c_idx = 0;
self.err_idx = 0;
@@ -380,6 +386,10 @@ pub const Tokenizer = struct {
self.maybeClear();
}
 
fn resetPrevExec(self: *Tokenizer) void {
if (self.prev_exec) |*pr| pr.clearAndFree();
}
 
/// Doesn't exec, called to save previous "local" command
pub fn exec(self: *Tokenizer) void {
if (self.prev_exec) |*pr| pr.clearAndFree();