add mzteinit

This commit is contained in:
LordMZTE 2022-09-08 17:03:54 +02:00
parent 27200ea612
commit c39644a5a3
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
8 changed files with 203 additions and 0 deletions

View file

@ -0,0 +1,5 @@
if set -q MZTEINIT
exit
end
set -x MZTEINIT
exec mzteinit

View file

@ -14,6 +14,7 @@ zls-bin
install-scripts target=(`echo $HOME` + "/.local/bin"): build-scripts
cp scripts/randomwallpaper/zig-out/bin/randomwallpaper {{target}}/randomwallpaper
cp scripts/playtwitch/zig-out/bin/playtwitch {{target}}/playtwitch
cp scripts/mzteinit/zig-out/bin/mzteinit {{target}}/mzteinit
ln -sf \
`pwd`/scripts/{start-joshuto,withjava} \
@ -23,6 +24,7 @@ install-scripts target=(`echo $HOME` + "/.local/bin"): build-scripts
build-scripts:
cd scripts/randomwallpaper && zig build -Drelease-fast
cd scripts/playtwitch && gyro build -Drelease-fast
cd scripts/mzteinit && gyro build -Drelease-fast
install-lsps-paru:
#!/bin/sh

5
scripts/mzteinit/.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
zig-cache/
zig-out/
deps.zig
gyro.lock
.gyro

View file

@ -0,0 +1,38 @@
const std = @import("std");
const pkgs = @import("deps.zig").pkgs;
pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("mzteinit", "src/main.zig");
exe.setTarget(target);
pkgs.addAllTo(exe);
exe.setBuildMode(mode);
exe.install();
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const exe_tests = b.addTest("src/main.zig");
exe_tests.setTarget(target);
exe_tests.setBuildMode(mode);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step);
}

View file

@ -0,0 +1,6 @@
deps:
ansi-term:
git:
url: "https://github.com/ziglibs/ansi-term.git"
ref: b1541b3982d28562060b79cfcb2e561a3307cd0c
root: src/main.zig

View file

@ -0,0 +1,29 @@
const std = @import("std");
const at = @import("ansi-term");
// r means switch to red
const figlet =
\\ __ __r__ ________ ____________
\\ / / ____ _________/ r/ |/ /__ //_ __/ ____/
\\ / / / __ \/ ___/ __ r/ /|_/ / / / / / / __/
\\ / /___/ /_/ / / / /_/ r/ / / / / /__/ / / /___
\\ /_____/\____/_/ \__,_r/_/ /_/ /____/_/ /_____/
;
pub fn writeFiglet(writer: anytype) !void {
var style: ?at.style.Style = null;
var iter = std.mem.split(u8, figlet, "\n");
while (iter.next()) |line| {
for (line) |char| {
if (char == 'r') {
try at.format.updateStyle(writer, .{ .foreground = .Red }, style);
style = .{ .foreground = .Red };
} else {
try writer.writeByte(char);
}
}
try at.format.updateStyle(writer, .{ .foreground = .Default }, style);
style = .{ .foreground = .Default };
try writer.writeByte('\n');
}
}

View file

@ -0,0 +1,70 @@
const std = @import("std");
const at = @import("ansi-term");
const run = @import("run.zig");
pub fn main() !void {
while (true) {
try std.io.getStdOut().writeAll("\x1b[2J\x1b[1;1H"); // clear
const cmd = ui() catch |e| {
std.debug.print("Error rendering the UI: {}\n", .{e});
break;
};
cmd.run() catch |e| {
try std.io.getStdOut().writeAll("\x1b[2J\x1b[1;1H"); // clear
std.debug.print("Error running command: {}\n", .{e});
};
}
}
fn ui() !run.Command {
var stdout = std.io.bufferedWriter(std.io.getStdOut().writer());
const w = stdout.writer();
try @import("figlet.zig").writeFiglet(w);
try w.writeAll(
\\
\\What do you want to do?
\\
\\
);
var style: ?at.style.Style = null;
for (std.enums.values(run.Command)) |tag| {
try updateStyle(w, .{ .foreground = .Blue }, &style);
try w.print("[{c}] ", .{tag.char()});
try updateStyle(w, .{ .foreground = .Green }, &style);
try w.print("{s}\n", .{@tagName(tag)});
}
try at.format.resetStyle(w);
try stdout.flush();
const old_termios = try std.os.tcgetattr(std.os.STDIN_FILENO);
var new_termios = old_termios;
new_termios.lflag &= ~std.os.linux.ICANON; // No line buffering
new_termios.lflag &= ~std.os.linux.ECHO; // No echoing stuff
try std.os.tcsetattr(std.os.STDIN_FILENO, .NOW, new_termios);
var cmd: ?run.Command = null;
var c: [1]u8 = undefined;
while (cmd == null) {
std.debug.assert(try std.io.getStdIn().read(&c) == 1);
cmd = run.Command.fromChar(c[0]);
if (cmd == null) {
try w.print("Unknown command '{s}'\n", .{c});
try stdout.flush();
}
}
try std.os.tcsetattr(std.os.STDIN_FILENO, .NOW, old_termios);
return cmd.?;
}
fn updateStyle(
writer: anytype,
new_style: at.style.Style,
old_style: *?at.style.Style,
) !void {
try at.format.updateStyle(writer, new_style, old_style.*);
old_style.* = new_style;
}

View file

@ -0,0 +1,48 @@
const std = @import("std");
pub const Command = enum {
startx,
shell,
zellij,
shutdown,
reboot,
pub fn fromChar(c: u8) ?Command {
return switch (c) {
'x', 'X' => .startx,
's', 'S' => .shell,
'z', 'Z' => .zellij,
'p', 'P' => .shutdown,
'r', 'R' => .reboot,
else => null,
};
}
pub fn char(self: Command) u8 {
return switch (self) {
.startx => 'X',
.shell => 'S',
.zellij => 'Z',
.shutdown => 'P',
.reboot => 'R',
};
}
pub fn run(self: Command) !void {
var mem: [512]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&mem);
const arg = self.argv();
var child = std.ChildProcess.init(arg, fba.allocator());
_ = try child.spawnAndWait();
}
fn argv(self: Command) []const []const u8 {
return switch (self) {
.startx => &.{"startx"},
.shell => &.{"fish"},
.zellij => &.{"zellij"},
.shutdown => &.{ "systemctl", "poweroff" },
.reboot => &.{ "systemctl", "reboot" },
};
}
};