mzteinit: set _JAVA_OPTIONS

This commit is contained in:
LordMZTE 2023-06-03 12:59:48 +02:00
parent 34c529b20c
commit dc2c20364e
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
3 changed files with 66 additions and 67 deletions

View file

@ -1,55 +0,0 @@
//! 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,34 @@
const std = @import("std");
pub fn delimitedWriter(writer: anytype, delimeter: u8) DelimitedWriter(@TypeOf(writer)) {
return DelimitedWriter(@TypeOf(writer)).init(writer, delimeter);
}
/// Utility struct for building delimeter-separated strings
pub fn DelimitedWriter(comptime Writer: type) type {
return struct {
writer: Writer,
delimeter: u8,
has_written: bool,
const Self = @This();
pub fn init(writer: Writer, delimeter: u8) Self {
return .{
.writer = writer,
.delimeter = delimeter,
.has_written = false,
};
}
/// Push a string, inserting a delimiter if necessary
pub fn push(self: *Self, str: []const u8) !void {
if (self.has_written) {
try self.writer.writeByte(self.delimeter);
}
self.has_written = true;
try self.writer.writeAll(str);
}
};
}

View file

@ -3,12 +3,16 @@ const sysdaemon = @import("sysdaemon.zig");
const log = std.log.scoped(.env); const log = std.log.scoped(.env);
const DelimitedBuilder = @import("DelimitedBuilder.zig"); const delimitedWriter = @import("delimeted_writer.zig").delimitedWriter;
/// Initialize the environment. /// Initialize the environment.
/// Returns true if the environment should be transferred to the system daemon. /// Returns true if the environment should be transferred to the system daemon.
pub fn populateEnvironment(env: *std.process.EnvMap) !bool { pub fn populateEnvironment(env: *std.process.EnvMap) !bool {
var buf: [512]u8 = undefined; // buffer for building values for env vars
var buf: [1024 * 8]u8 = undefined;
// buffer for small one-off operations while `buf` is in use
var sbuf: [512]u8 = undefined;
if (env.get("MZTE_ENV_SET")) |_| { if (env.get("MZTE_ENV_SET")) |_| {
return false; return false;
@ -30,7 +34,7 @@ pub fn populateEnvironment(env: *std.process.EnvMap) !bool {
.{ "XDG_STATE_HOME", ".local/state" }, .{ "XDG_STATE_HOME", ".local/state" },
.{ "XDG_CACHE_HOME", ".local/cache" }, .{ "XDG_CACHE_HOME", ".local/cache" },
}) |kv| { }) |kv| {
try env.put(kv.@"0", try std.fmt.bufPrint(&buf, "{s}/{s}", .{ home, kv.@"1" })); try env.put(kv.@"0", try std.fmt.bufPrint(&sbuf, "{s}/{s}", .{ home, kv.@"1" }));
} }
// set shell to fish to prevent anything from defaulting to mzteinit // set shell to fish to prevent anything from defaulting to mzteinit
@ -46,10 +50,26 @@ pub fn populateEnvironment(env: *std.process.EnvMap) !bool {
// neovim // neovim
try env.put("EDITOR", "nvim"); try env.put("EDITOR", "nvim");
// Java options
{
var bufstream = std.io.fixedBufferStream(&buf);
var b = delimitedWriter(bufstream.writer(), ' ');
// anti-alias text
try b.push("-Dawt.useSystemAAFontSettings=on");
try b.push("-Dswing.aatext=true");
// GTK theme
try b.push("-Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
try b.push("-Dswing.crossplatformlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
try env.put("_JAVA_OPTIONS", bufstream.getWritten());
}
// PATH // PATH
{ {
var b = DelimitedBuilder.init(alloc, ':'); var bufstream = std.io.fixedBufferStream(&buf);
errdefer b.deinit(); var b = delimitedWriter(bufstream.writer(), ':');
const fixed_home = [_][]const u8{ const fixed_home = [_][]const u8{
".mix/escripts", ".mix/escripts",
@ -59,7 +79,7 @@ pub fn populateEnvironment(env: *std.process.EnvMap) !bool {
".roswell/bin", ".roswell/bin",
}; };
for (fixed_home) |fixed| { for (fixed_home) |fixed| {
try b.push(try std.fmt.bufPrint(&buf, "{s}/{s}", .{ home, fixed })); try b.push(try std.fmt.bufPrint(&sbuf, "{s}/{s}", .{ home, fixed }));
} }
// racket bins // racket bins
@ -86,24 +106,24 @@ pub fn populateEnvironment(env: *std.process.EnvMap) !bool {
try b.push(system_path); try b.push(system_path);
} }
try env.putMove(try alloc.dupe(u8, "PATH"), try b.toOwned()); try env.put("PATH", bufstream.getWritten());
} }
// LUA_CPATH // LUA_CPATH
{ {
var b = DelimitedBuilder.init(alloc, ';'); var bufstream = std.io.fixedBufferStream(&buf);
errdefer b.deinit(); var b = delimitedWriter(bufstream.writer(), ';');
const fixed_home = [_][]const u8{ const fixed_home = [_][]const u8{
".local/lib/lua/?.so", ".local/lib/lua/?.so",
".local/lib/lua/?.lua", ".local/lib/lua/?.lua",
}; };
for (fixed_home) |fixed| { for (fixed_home) |fixed| {
try b.push(try std.fmt.bufPrint(&buf, "{s}/{s}", .{ home, fixed })); try b.push(try std.fmt.bufPrint(&sbuf, "{s}/{s}", .{ home, fixed }));
} }
try b.pushDirect(";;"); try b.writer.writeAll(";;");
try env.putMove(try alloc.dupe(u8, "LUA_CPATH"), try b.toOwned()); try env.put("LUA_CPATH", bufstream.getWritten());
} }
return true; return true;