From e0572c37d970d601c17085bdb082cc6ef4356891 Mon Sep 17 00:00:00 2001 From: hachem Date: Fri, 9 Oct 2026 11:08:09 +0200 Subject: chore: fmt some revo code based on lung's example --- gils/cp.rv | 17 +++++++++++------ gils/gils.rv | 35 ++++++++++++++++++++++++----------- gils/head.rv | 33 +++++++++++++++++++++++---------- gils/ls.rv | 15 ++++++++++----- gils/mkdir.rv | 8 ++++++-- gils/mv.rv | 18 ++++++++++++------ gils/rm.rv | 8 ++++++-- gils/tail.rv | 32 ++++++++++++++++++++++---------- gils/touch.rv | 12 ++++++++---- gils/tree.rv | 30 ++++++++++++++++++++---------- gils/wc.rv | 26 ++++++++++++++++++-------- 11 files changed, 160 insertions(+), 74 deletions(-) (limited to 'gils') diff --git a/gils/cp.rv b/gils/cp.rv index 20d4ae3..6fd06a8 100644 --- a/gils/cp.rv +++ b/gils/cp.rv @@ -2,14 +2,19 @@ # HAAAAAANNNNKKKK const context = env.ctx -if context.argv:len() < 3 do fail("usage: cp \n") end -else do + +if context.argv:len() < 3 do + fail("usage: cp \n") +end else do const source = expand_path(context.argv[1]) const destination = expand_path(context.argv[2]) const size = fs:size(source) - if size < 0 do fail("cp: #{source}: no such file\n") end - else do - const contents = if size > 0 fs:read_bytes(source, 0, size) else "" - if fs:store(destination, contents) == 1 do puts("#{source} -> #{destination}\n") end + + if size < 0 do + fail("cp: #{source}: no such file\n") + end else do + const contents = if (size > 0) fs:read_bytes(source, 0, size) else "" + if (fs:store(destination, contents) == 1) + puts("#{source} -> #{destination}\n") end end diff --git a/gils/gils.rv b/gils/gils.rv index a500322..2432890 100644 --- a/gils/gils.rv +++ b/gils/gils.rv @@ -6,41 +6,54 @@ fn register(usage, group, help, handler) do commands:push({ name = name, usage = usage, group = group, help = help, handler = handler }) end -fn lookup(name) do +fn lookup(name) -> table | :nil do for index in 0..commands:len() do - if commands[index].name == name return commands[index] + if (commands[index].name == name) + return commands[index] end return :nil end -fn make_ctx(line, argv) do +fn make_ctx(line, argv) -> { line: string, argv: table, rest: string } do const name = argv[0] let rest = "" + const cut = name:len() + 1 - if line:len() > cut do rest = line[cut..] end + if (line:len() > cut) + rest = line[cut..] + return { line = line, argv = argv, rest = rest } end # scripts read their args off env.ctx, stashed here before eval global env = { ctx = :nil } -# first use of unless in history -global expand_path = fn(path) do - if path:len() == 0 return wd.path +# first use of unless in revo history +global expand_path = fn(path) -> string do + if (path:len() == 0) + return wd.path + unless (path[0] == "/") do const slash = unless (wd.path == "/") "/" else "" path = "#{wd.path}#{slash}#{path}" end + return path end -fn run_script_path(path, label, ctx) do +fn run_script_path(path, label, ctx) -> bool do const size = fs:size(path) - if size < 0 return :false + if (size < 0) + return :false + env.ctx = ctx const error_msg = eval_script(fs:read_bytes(path, 0, size)) - if error_msg != :nil do fail("#{label}: #{error_msg}\n") end + if (error_msg != :nil) + fail("#{label}: #{error_msg}\n") + return :true end -fn run_script(name, ctx) run_script_path("/bin/#{name}.rv", name, ctx) +fn run_script(name, ctx) -> bool do + return run_script_path("/bin/#{name}.rv", name, ctx) +end diff --git a/gils/head.rv b/gils/head.rv index 2c2a82f..eb50087 100644 --- a/gils/head.rv +++ b/gils/head.rv @@ -1,27 +1,40 @@ # gimme some head lung -fn file_lines(path) do +fn file_lines(path) -> table | :nil do const size = fs:size(path) - if size < 0 return :nil - const contents = if size > 0 fs:read_bytes(path, 0, size) else "" + if (size < 0) + return :nil + + const contents = if (size > 0) fs:read_bytes(path, 0, size) else "" const raw_lines = contents:split("\n") let result = {} for index in 0..raw_lines:len() do - if (index < raw_lines:len() - 1 or raw_lines[index]:len() > 0) result:push(raw_lines[index]) + if (index < raw_lines:len() - 1 or raw_lines[index]:len() > 0) + result:push(raw_lines[index]) end return result end const context = env.ctx -if context.argv:len() < 2 do fail("usage: head [n] \n") end -else do + +if context.argv:len() < 2 do + fail("usage: head [n] \n") +end else do let limit = 10 let path_arg = context.argv[1] - if (context.argv:len() >= 3 and parse_pid(context.argv[1]) >= 0) do limit = parse_pid(context.argv[1]) path_arg = context.argv[2] end + if context.argv:len() >= 3 and parse_pid(context.argv[1]) >= 0 do + limit = parse_pid(context.argv[1]) + path_arg = context.argv[2] + end + const lines = file_lines(expand_path(path_arg)) - if lines == :nil do fail("head: no such file\n") end - else do + if lines == :nil do + fail("head: no such file\n") + end else do let index = 0 - while (index < lines:len() and index < limit) do puts("#{lines[index]}\n") index += 1 end + while (index < lines:len() and index < limit) do + puts("#{lines[index]}\n") + index += 1 + end end end diff --git a/gils/ls.rv b/gils/ls.rv index c069857..37f4b05 100644 --- a/gils/ls.rv +++ b/gils/ls.rv @@ -1,10 +1,15 @@ # list a directory const context = env.ctx -const target = if context.argv:len() > 1 expand_path(context.argv[1]) else wd.path -if vfs_owns(target) do vfs_ls(target) end -else do +const target = if (context.argv:len() > 1) expand_path(context.argv[1]) else wd.path + +if vfs_owns(target) do + vfs_ls(target) +end else do const count = fs:ls(target) - if target == "/" do puts(" proc/\n") end - else if count == 0 do puts(" (empty)\n") end + if target == "/" do + puts(" proc/\n") + end else if count == 0 do + puts(" (empty)\n") + end end diff --git a/gils/mkdir.rv b/gils/mkdir.rv index c7d12f7..9da7689 100644 --- a/gils/mkdir.rv +++ b/gils/mkdir.rv @@ -1,5 +1,9 @@ # make a directory const context = env.ctx -if context.argv:len() < 2 do fail("usage: mkdir \n") end -else do fs:mkdir(expand_path(context.argv[1])) end + +if context.argv:len() < 2 do + fail("usage: mkdir \n") +end else do + fs:mkdir(expand_path(context.argv[1])) +end diff --git a/gils/mv.rv b/gils/mv.rv index b44995b..d44b898 100644 --- a/gils/mv.rv +++ b/gils/mv.rv @@ -1,14 +1,20 @@ # move or rename a file const context = env.ctx -if context.argv:len() < 3 do fail("usage: mv \n") end -else do +if context.argv:len() < 3 do + fail("usage: mv \n") +end else do const source = expand_path(context.argv[1]) const destination = expand_path(context.argv[2]) const size = fs:size(source) - if size < 0 do fail("mv: #{source}: no such file\n") end - else do - const contents = if size > 0 fs:read_bytes(source, 0, size) else "" - if fs:store(destination, contents) == 1 do fs:unlink(source) puts("#{source} -> #{destination}\n") end + + if size < 0 do + fail("mv: #{source}: no such file\n") + end else do + const contents = if (size > 0) fs:read_bytes(source, 0, size) else "" + if fs:store(destination, contents) == 1 do + fs:unlink(source) + puts("#{source} -> #{destination}\n") + end end end diff --git a/gils/rm.rv b/gils/rm.rv index 236d2ba..8c954f3 100644 --- a/gils/rm.rv +++ b/gils/rm.rv @@ -2,5 +2,9 @@ const context = env.ctx const parsed = arg_name(context.rest, 0) -if parsed.name:len() == 0 do fail("usage: rm \n") end -else do fs:rm(expand_path(parsed.name)) end + +if parsed.name:len() == 0 do + fail("usage: rm \n") +end else do + fs:rm(expand_path(parsed.name)) +end diff --git a/gils/tail.rv b/gils/tail.rv index c1ca124..c0ce1df 100644 --- a/gils/tail.rv +++ b/gils/tail.rv @@ -1,29 +1,41 @@ # my tail is 16 inches long # and its between my legs -fn file_lines(path) do +fn file_lines(path) -> table | :nil do const size = fs:size(path) - if size < 0 return :nil - const contents = if size > 0 fs:read_bytes(path, 0, size) else "" + if (size < 0) + return :nil + + const contents = if (size > 0) fs:read_bytes(path, 0, size) else "" const raw_lines = contents:split("\n") let result = {} + for index in 0..raw_lines:len() do - if (index < raw_lines:len() - 1 or raw_lines[index]:len() > 0) result:push(raw_lines[index]) + if (index < raw_lines:len() - 1 or raw_lines[index]:len() > 0) + result:push(raw_lines[index]) end return result end const context = env.ctx -if context.argv:len() < 2 do fail("usage: tail [n] \n") end -else do +if context.argv:len() < 2 do + fail("usage: tail [n] \n") +end else do let limit = 10 let path_arg = context.argv[1] - if (context.argv:len() >= 3 and parse_pid(context.argv[1]) >= 0) do limit = parse_pid(context.argv[1]) path_arg = context.argv[2] end + if context.argv:len() >= 3 and parse_pid(context.argv[1]) >= 0 do + limit = parse_pid(context.argv[1]) + path_arg = context.argv[2] + end + const lines = file_lines(expand_path(path_arg)) - if lines == :nil do fail("tail: no such file\n") end - else do + if lines == :nil do + fail("tail: no such file\n") + end else do let start = lines:len() - limit - if start < 0 do start = 0 end + if (start < 0) + start = 0 + for index in start..lines:len() do puts("#{lines[index]}\n") end end end diff --git a/gils/touch.rv b/gils/touch.rv index f8e3e18..3032e96 100644 --- a/gils/touch.rv +++ b/gils/touch.rv @@ -1,9 +1,13 @@ # dont touch my nono square const context = env.ctx -if context.argv:len() < 2 do fail("usage: touch \n") end -else do +if context.argv:len() < 2 do + fail("usage: touch \n") +end else do const path = expand_path(context.argv[1]) - if fs:is_dir(path) do fail("touch: #{path}: is a directory\n") end - else if fs:size(path) < 0 do fs:store(path, "") end + if fs:is_dir(path) do + fail("touch: #{path}: is a directory\n") + end else if fs:size(path) < 0 do + fs:store(path, "") + end end diff --git a/gils/tree.rv b/gils/tree.rv index d1a6af6..d146403 100644 --- a/gils/tree.rv +++ b/gils/tree.rv @@ -1,29 +1,39 @@ -# recursive directory listing. +# recursive directory listing fn tree_walk(path, prefix) do const items = fs:entries(path) - if items == :nil return :nil + if (items == :nil) + return :nil + for index in 0..items:len() do const entry = items[index] const last = index == items:len() - 1 let branch = "|-- " - if last do branch = "`-- " end # font doesnt support fancy ascii symbols - const slash = if entry.type == 2 "/" else "" + # font doesnt support fancy ascii symbols + if (last) + branch = "`-- " + + const slash = if (entry.type == 2) "/" else "" puts("#{prefix}#{branch}#{entry.name}#{slash}\n") + if entry.type == 2 do - const child = if path == "/" "/#{entry.name}" else "#{path}/#{entry.name}" + const child = if (path == "/") "/#{entry.name}" else "#{path}/#{entry.name}" let indent = "| " - if last do indent = " " end + if (last) + indent = " " tree_walk(child, prefix ~ indent) end end end const context = env.ctx -const target = if context.argv:len() > 1 expand_path(context.argv[1]) else wd.path -if (vfs_owns(target) and target != "/") do fail("tree: #{target}: virtual dir\n") end -else if (target != "/" and fs:is_dir(target) == :false) do fail("tree: #{target}: not a directory\n") end -else do +const target = if (context.argv:len() > 1) expand_path(context.argv[1]) else wd.path + +if vfs_owns(target) and target != "/" do + fail("tree: #{target}: virtual dir\n") +end else if target != "/" and fs:is_dir(target) == :false do + fail("tree: #{target}: not a directory\n") +end else do puts("#{target}\n") tree_walk(target, "") # /proc is the one thing not on disk so tack it on at root diff --git a/gils/wc.rv b/gils/wc.rv index 1127299..75a3ed2 100644 --- a/gils/wc.rv +++ b/gils/wc.rv @@ -1,22 +1,32 @@ # word count const context = env.ctx -if context.argv:len() < 2 do fail("usage: wc \n") end -else do +if context.argv:len() < 2 do + fail("usage: wc \n") +end else do const path = expand_path(context.argv[1]) const size = fs:size(path) - if size < 0 do fail("wc: #{path}: no such file\n") end - else do - const contents = if size > 0 fs:read_bytes(path, 0, size) else "" + + if size < 0 do + fail("wc: #{path}: no such file\n") + end else do + const contents = if (size > 0) fs:read_bytes(path, 0, size) else "" let line_count = 0 let word_count = 0 let in_word = :false + for index in 0..contents:len() do const character = contents[index] - if character == "\n" do line_count += 1 end - if (character == " " or character == "\n" or character == "\t") do in_word = :false end - else if in_word == :false do word_count += 1 in_word = :true end + if (character == "\n") + line_count += 1 + if character == " " or character == "\n" or character == "\t" do + in_word = :false + end else if in_word == :false do + word_count += 1 + in_word = :true + end end + puts(" #{line_count} #{word_count} #{size} #{context.argv[1]}\n") end end -- cgit v1.3