This commit is contained in:
63
src/main.zig
63
src/main.zig
@@ -1,12 +1,35 @@
|
|||||||
const stdout = std.io.getStdOut().writer();
|
pub fn main() !void {
|
||||||
|
clear();
|
||||||
|
|
||||||
fn clear() !void {
|
var gpa = std.heap.GeneralPurposeAllocator(.{}).init;
|
||||||
try stdout.print("\u{001b}[H\u{001b}[J", .{});
|
defer _ = gpa.deinit();
|
||||||
|
|
||||||
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
|
var ln = Linenoise.init(allocator);
|
||||||
|
defer ln.deinit();
|
||||||
|
|
||||||
|
const p = try getPrompt(allocator);
|
||||||
|
defer allocator.free(p);
|
||||||
|
|
||||||
|
while (try ln.linenoise(p)) |input| {
|
||||||
|
defer allocator.free(input);
|
||||||
|
if (input.len > 0) {
|
||||||
|
try ln.history.add(input);
|
||||||
|
const command = try tokenizeCommand(input, allocator);
|
||||||
|
defer allocator.free(command);
|
||||||
|
_ = runCommand(command, allocator) catch |err| switch (err) {
|
||||||
|
error.FileNotFound => print("mash: {s}: command not found\n", .{command[0]}),
|
||||||
|
error.AccessDenied => print("mash: {s}: Permission denied\n", .{command[0]}),
|
||||||
|
else => print("Unkown error: {}\n", .{err}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getPrompt(allocator: Allocator) ![]const u8 {
|
fn getPrompt(allocator: Allocator) ![]const u8 {
|
||||||
const script = @embedFile("./get_prompt.bash");
|
const script = @embedFile("./get_prompt.bash");
|
||||||
const result = try std.process.Child.run(.{
|
const result = try process.Child.run(.{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.argv = &[_][]const u8{ "bash", "-i", "-c", script },
|
.argv = &[_][]const u8{ "bash", "-i", "-c", script },
|
||||||
});
|
});
|
||||||
@@ -15,7 +38,7 @@ fn getPrompt(allocator: Allocator) ![]const u8 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn runCommand(command: [][]const u8, allocator: Allocator) !u8 {
|
fn runCommand(command: [][]const u8, allocator: Allocator) !u8 {
|
||||||
var child = std.process.Child.init(command, allocator);
|
var child = process.Child.init(command, allocator);
|
||||||
const result = try child.spawnAndWait();
|
const result = try child.spawnAndWait();
|
||||||
return result.Exited;
|
return result.Exited;
|
||||||
}
|
}
|
||||||
@@ -31,34 +54,18 @@ fn tokenizeCommand(command: []const u8, allocator: Allocator) ![][]const u8 {
|
|||||||
return argv_array_list.toOwnedSlice();
|
return argv_array_list.toOwnedSlice();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() !void {
|
fn clear() void {
|
||||||
try clear();
|
print("\u{001b}[H\u{001b}[J", .{});
|
||||||
|
}
|
||||||
|
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}).init;
|
fn print(comptime fmt: []const u8, args: anytype) void {
|
||||||
defer _ = gpa.deinit();
|
stdout.print(fmt, args) catch unreachable;
|
||||||
|
|
||||||
const allocator = gpa.allocator();
|
|
||||||
|
|
||||||
var ln = Linenoise.init(allocator);
|
|
||||||
defer ln.deinit();
|
|
||||||
|
|
||||||
const p = try getPrompt(allocator);
|
|
||||||
defer allocator.free(p);
|
|
||||||
|
|
||||||
while (try ln.linenoise(p)) |input| {
|
|
||||||
defer allocator.free(input);
|
|
||||||
try ln.history.add(input);
|
|
||||||
const command = try tokenizeCommand(input, allocator);
|
|
||||||
defer allocator.free(command);
|
|
||||||
_ = runCommand(command, allocator) catch |err| switch (err) {
|
|
||||||
error.FileNotFound => stdout.print("mash: {s}: command not found", .{command[0]}) catch unreachable,
|
|
||||||
else => std.log.debug("Unkown error: {}", .{err}),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const stdout = std.io.getStdOut().writer();
|
||||||
const tokenizeAny = std.mem.tokenizeAny;
|
const tokenizeAny = std.mem.tokenizeAny;
|
||||||
|
const process = std.process;
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const ArrayList = std.ArrayList;
|
const ArrayList = std.ArrayList;
|
||||||
const Linenoise = @import("linenoise").Linenoise;
|
const Linenoise = @import("linenoise").Linenoise;
|
||||||
|
|||||||
Reference in New Issue
Block a user