mzte-nv now acts as login shell

This commit is contained in:
LordMZTE 2023-04-16 18:12:38 +02:00
parent 4060416540
commit b26af6899a
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
7 changed files with 194 additions and 33 deletions

View file

@ -1,22 +0,0 @@
if set -q MZTE_ENV_SET
exit
end
set -gx MZTE_ENV_SET
# mix should respect XDG standard
export MIX_XDG=1
# use clang compiler
export CC=clang
export CXX=clang++
# neovim editor
export EDITOR=nvim
# paths
export PATH="$HOME/.mix/escripts:$HOME/.cargo/bin:$HOME/.local/bin:$HOME/go/bin:$PATH:$HOME/.roswell/bin"
export LUA_CPATH="$HOME/.local/lib/lua/?.so;$HOME/.local/lib/lua/?.lua;;"
if which racket >/dev/null
set -ax PATH (racket -l racket/base -e '(require setup/dirs) (display (path->string (find-user-console-bin-dir)))')
end

View file

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

View file

@ -1,4 +1,5 @@
;<! tmpl:setPostProcessor(opt.fennelCompile) !>
; vim: filetype=fennel
(local wt (require :wezterm))
@ -37,7 +38,8 @@
(table.insert keys (kmap dir-key :ALT|SHIFT :AdjustPaneSize
[dir-name resize-amt]))))
{:color_scheme "Dracula (Official)"
{:default_prog [:fish]
:color_scheme "Dracula (Official)"
:font (wt.font "<% opt.term_font %>")
:warn_about_missing_glyphs false
:window_background_opacity 0.8

View file

@ -0,0 +1,55 @@
//! Utility struct for building delimeter-separated strings
const std = @import("std");
str: []u8,
alloc: std.mem.Allocator,
delimeter: u8,
const DelimitedBuilder = @This();
pub fn init(alloc: std.mem.Allocator, delimeter: u8) DelimitedBuilder {
return .{
.str = "",
.alloc = alloc,
.delimeter = delimeter,
};
}
pub fn deinit(self: DelimitedBuilder) void {
if (self.str.len > 0) {
self.alloc.free(self.str);
}
}
/// Push a string, inserting a delimiter if necessary
pub fn push(self: *DelimitedBuilder, str: []const u8) !void {
if (self.str.len == 0) {
self.str = try self.alloc.dupe(u8, str);
} else {
const old_len = self.str.len;
self.str = try self.alloc.realloc(self.str, old_len + str.len + 1);
self.str[old_len] = self.delimeter;
std.mem.copy(u8, self.str[old_len + 1 ..], str);
}
}
/// Push a string without a delimiter
pub fn pushDirect(self: *DelimitedBuilder, str: []const u8) !void {
if (self.str.len == 0) {
self.str = try self.alloc.dupe(u8, str);
} else {
const old_len = self.str.len;
self.str = try self.alloc.realloc(self.str, old_len + str.len);
std.mem.copy(u8, self.str[old_len ..], str);
}
}
/// Converts the builder's string to an allocated string.
/// Caller owns returned memory, the builder will be fully deinitialized.
pub fn toOwned(self: *const DelimitedBuilder) ![]u8 {
if (self.str.len == 0) {
return try self.alloc.alloc(u8, 0);
} else {
return self.str;
}
}

View file

@ -0,0 +1,89 @@
const std = @import("std");
const DelimitedBuilder = @import("DelimitedBuilder.zig");
pub fn populateEnvironment(env: *std.process.EnvMap) !void {
if (env.get("MZTE_ENV_SET")) |_| {
return;
}
const alloc = env.hash_map.allocator;
const home = if (env.get("HOME")) |home| try alloc.dupe(u8, home) else blk: {
std.log.warn("Home not set, defaulting to current directory", .{});
break :blk try std.fs.realpathAlloc(alloc, ".");
};
defer alloc.free(home);
try env.put("MZTE_ENV_SET", "1");
// mix (elixir package manager) should respect XDG
try env.put("MIX_XDG", "1");
// use clang
try env.put("CC", "clang");
try env.put("CXX", "clang++");
// neovim
try env.put("EDITOR", "nvim");
// PATH
{
var b = DelimitedBuilder.init(alloc, ':');
errdefer b.deinit();
var buf: [512]u8 = undefined;
const fixed_home = [_][]const u8{
".mix/escripts",
".cargo/bin",
".local/bin",
"go/bin",
".roswell/bin",
};
for (fixed_home) |fixed| {
try b.push(try std.fmt.bufPrint(&buf, "{s}/{s}", .{ home, fixed }));
}
// racket bins
racket: {
const res = std.ChildProcess.exec(.{
.allocator = alloc,
.argv = &.{
"racket",
"-l",
"racket/base",
"-e",
"(require setup/dirs) (display (path->string (find-user-console-bin-dir)))",
},
}) catch break :racket;
defer alloc.free(res.stdout);
defer alloc.free(res.stderr);
try b.push(res.stdout);
}
if (env.get("PATH")) |system_path| {
try b.push(system_path);
}
try env.putMove(try alloc.dupe(u8, "PATH"), try b.toOwned());
}
// LUA_CPATH
{
var b = DelimitedBuilder.init(alloc, ';');
errdefer b.deinit();
var buf: [512]u8 = undefined;
const fixed_home = [_][]const u8{
".local/lib/lua/?.so",
".local/lib/lua/?.lua",
};
for (fixed_home) |fixed| {
try b.push(try std.fmt.bufPrint(&buf, "{s}/{s}", .{ home, fixed }));
}
try b.pushDirect(";;");
try env.putMove(try alloc.dupe(u8, "LUA_CPATH"), try b.toOwned());
}
}

View file

@ -1,5 +1,6 @@
const std = @import("std");
const at = @import("ansi-term");
const env = @import("env.zig");
const run = @import("run.zig");
const util = @import("util.zig");
@ -7,6 +8,43 @@ pub fn main() !void {
var stdout = std.io.bufferedWriter(std.io.getStdOut().writer());
var exit = false;
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const alloc = gpa.allocator();
var env_map = std.process.EnvMap.init(alloc);
defer env_map.deinit();
for (std.os.environ) |env_var| {
var idx: usize = 0;
while (env_var[idx] != '=') {
idx += 1;
}
const eq_idx = idx;
while (env_var[idx] != 0) {
idx += 1;
}
const key = env_var[0..eq_idx];
const value = env_var[eq_idx + 1..idx];
try env_map.put(key, value);
}
if (env_map.get("MZTEINIT")) |_| {
try stdout.writer().writeAll("mzteinit running already, starting shell\n");
try stdout.flush();
var child = std.ChildProcess.init(&.{"fish"}, alloc);
_ = try child.spawnAndWait();
return;
} else {
try env_map.put("MZTEINIT", "1");
}
try env.populateEnvironment(&env_map);
while (true) {
try util.writeAnsiClear(stdout.writer());
@ -18,7 +56,7 @@ pub fn main() !void {
try util.writeAnsiClear(stdout.writer());
try stdout.flush();
cmd.run(&exit) catch |e| {
cmd.run(alloc, &exit, &env_map) catch |e| {
try stdout.writer().print("Error running command: {}\n\n", .{e});
continue;
};

View file

@ -31,16 +31,20 @@ pub const Command = enum {
};
}
pub fn run(self: Command, exit: *bool) !void {
pub fn run(
self: Command,
alloc: std.mem.Allocator,
exit: *bool,
env: *const std.process.EnvMap,
) !void {
if (self == .logout) {
exit.* = true;
return;
}
var mem: [512]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&mem);
const arg = self.argv();
var child = std.ChildProcess.init(arg, fba.allocator());
var child = std.ChildProcess.init(arg, alloc);
child.env_map = env;
_ = try child.spawnAndWait();
}