do local unistd = require("posix.unistd") local wait = require("posix.sys.wait") local fcntl = require("posix.fcntl") lotus = require("lotus") builtin = { cd = function(dir) unistd.chdir(dir) end, } local function execute_command(command, arguments) if builtin[command] then builtin[command](table.unpack(arguments)) return end if lotus.shell.pipe_input then lotus.shell.pipe_read, lotus.shell.pipe_write = unistd.pipe(), nil lotus.shell.stdout = unistd.dup(unistd.STDOUT_FILENO) unistd.dup2(lotus.shell.pipe_write, unistd.STDOUT_FILENO) unistd.close(lotus.shell.pipe_write) lotus.shell.pipe_input = false lotus.shell.pipe_output = true end if lotus.shell.pipe_output then table.remove(arguments, 1) lotus.shell.stdin = unistd.dup(unistd.STDIN_FILENO) unistd.dup2(lotus.shell.pipe_read, unistd.STDIN_FILENO) unistd.close(lotus.shell.pipe_read) end local pid = unistd.fork() if pid == 0 then table.insert(arguments, nil) local _, error_message, errnum = unistd.execp(command, arguments), nil, nil if error_message then io.stderr:write(error_message) end else wait.wait(pid) end if lotus.shell.pipe_output then unistd.close(lotus.shell.pipe_read) unistd.close(lotus.shell.pipe_write) unistd.dup2(lotus.shell.stdout, unistd.STDOUT_FILENO) unistd.dup2(lotus.shell.stdin, unistd.STDIN_FILENO) lotus.shell.pipe_output = false end end local function parse_arguments(args) local arguments = {} for _, arg in ipairs(args) do table.insert(arguments, tostring(arg)) end return arguments end setmetatable(_G, { __index = function(_, input) local command_table = {} local command_metatable = { __call = function(init, ...) local args = { [1] = ..., } local parsed = parse_arguments(args) execute_command(input, parsed) end, } setmetatable(command_table, command_metatable) return command_table end, }) sudo = {} setmetatable(sudo, { __index = function(_, input) return function(...) local args = { [1] = ..., } local parsed = ("%s %s"):format(input, parse_arguments(args)) execute_command(("sudo %s"):format(parsed)) end end, }) command = {} setmetatable(command, { __call = function(_, ...) local args = { [1] = ..., } local input = parse_arguments(args) execute_command(input) end, __index = function(_, input) return _G[input] end, }) end function cfg(app) local config_files = { awesome = "/home/neph/.config/awesome/rc.lua", bash = "/home/neph/.bashrc", lotus = "/home/neph/.config/lotus/rc.lotus", nvim = "/home/neph/.config/nvim/init.lua", wezterm = "/home/neph/.config/wezterm/wezterm.lua", } if config_files[app] then editor(config_files[app]) else print(("Unknown app: %s"):format(app)) end end function cd(directory) builtin.cd(directory) local icons = require("web-devicons") clear() print(("┏━ %s"):format(os.getenv("PWD"))) local files = (function() lotus.shell.pipe_input = true local pipe_return_value = ipairs((function() lotus.shell.pipe_input = true local pipe_return_value = lotus.string.lines(lotus.shell.capture(function() return (function() lotus.shell.pipe_input = true local pipe_return_value = grep(ls("-pv"), "-v", "/") lotus.shell.pipe_input = false return pipe_return_value end)() end)) lotus.shell.pipe_input = false return pipe_return_value end)()) lotus.shell.pipe_input = false return pipe_return_value end)() local directories = (function() lotus.shell.pipe_input = true local pipe_return_value = ipairs((function() lotus.shell.pipe_input = true local pipe_return_value = lotus.string.lines(lotus.shell.capture(function() return (function() lotus.shell.pipe_input = true local pipe_return_value = grep(ls("-pv"), "/") lotus.shell.pipe_input = false return pipe_return_value end)() end)) lotus.shell.pipe_input = false return pipe_return_value end)()) lotus.shell.pipe_input = false return pipe_return_value end)() for _, directory in directories do print(("┃  %s"):format(directory)) end for _, file in files do local extension = file:match(".*%.(.+)") or "" local icon = icons.get_icon(file, extension, { default = true, strict = true, }) print(("┃ %s %s"):format(icon, file)) end end cd(".")