interactive shell navigation and history
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131const std = @import("std");const assert = std.debug.assert;
var rt_handle: ?std.Io = null;var env_map: ?*std.process.Environ.Map = null;
pub fn init(io_handle: std.Io, environ: *std.process.Environ.Map) void { rt_handle = io_handle; env_map = environ;}
pub fn runtime() std.Io { assert(rt_handle != null); return rt_handle.?;}
pub fn getenv(key: []const u8) ?[]const u8 { assert(key.len > 0); const map = env_map orelse return null; return map.get(key);}
pub fn environMap() *std.process.Environ.Map { return env_map orelse fatal("io used before init", .{});}
pub const Writer = struct { inner: std.Io.File.Writer, failed: bool = false,
pub fn print(self: *Writer, comptime fmt: []const u8, args: anytype) !void { assert(fmt.len > 0); if (self.failed) return; self.inner.interface.print(fmt, args) catch |err| { if (err == error.WriteFailed) { self.failed = true; return; } return err; }; }
pub fn end(self: *Writer) !void { if (self.failed) return; self.inner.interface.flush() catch |err| { if (err == error.WriteFailed) { self.failed = true; return; } return err; }; }};
pub fn getStdout(buffer: []u8) Writer { assert(buffer.len > 0); return .{ .inner = std.Io.File.stdout().writerStreaming(runtime(), buffer) };}
pub fn warn(comptime fmt: []const u8, args: anytype) void { warnTo(runtime(), fmt, args);}
/// Works like warn, but takes the Io handle as an argument. Use it in code/// that can run before init, or in tests.pub fn warnTo(io_handle: std.Io, comptime fmt: []const u8, args: anytype) void { assert(fmt.len > 0); var buffer: [4096]u8 = undefined; var writer = std.Io.File.stderr().writerStreaming(io_handle, &buffer); writer.interface.print(fmt, args) catch |err| { std.log.warn("failed to write to stderr: {}", .{err}); }; writer.interface.flush() catch |err| { std.log.warn("failed to flush stderr writer: {}", .{err}); };}
pub fn fatal(comptime fmt: []const u8, args: anytype) noreturn { warn(fmt ++ "\n", args); std.process.exit(1);}
/// Options to check that a path exists. The default follows symlinks and/// requests no access mode.pub const exists_access: std.Io.Dir.AccessOptions = .{};
pub const CreateOptions = struct { truncate: bool, lock: std.Io.File.Lock, mode: std.posix.mode_t,};
pub fn createFile( io_handle: std.Io, directory: std.Io.Dir, path: []const u8, options: CreateOptions,) !std.Io.File { assert(path.len > 0); return directory.createFile(io_handle, path, .{ .read = false, .truncate = options.truncate, .exclusive = false, .lock = options.lock, .lock_nonblocking = false, .permissions = .fromMode(options.mode), .resolve_beneath = false, });}
/// Writes content to path in directory. It replaces any file already there.pub fn writeFile( io_handle: std.Io, directory: std.Io.Dir, path: []const u8, content: []const u8, mode: std.posix.mode_t,) !void { assert(content.len > 0); var file = try createFile(io_handle, directory, path, .{ .truncate = true, .lock = .none, .mode = mode, }); defer file.close(io_handle); var buffer: [4096]u8 = undefined; var writer = file.writer(io_handle, &buffer); try writer.interface.writeAll(content); try writer.interface.flush();}