diff options
| author | hachem <im@hachem.wtf> | 2026-10-09 11:08:09 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-10-09 11:08:09 +0200 |
| commit | e0572c37d970d601c17085bdb082cc6ef4356891 (patch) | |
| tree | f0b4d71c92978e74d127d7090d3e11380378a8b3 /gils/head.rv | |
| parent | 8cb66100fe802c38d180ea29a11a902decf0fd8b (diff) | |
chore: fmt some revo code based on lung's example
Diffstat (limited to 'gils/head.rv')
| -rw-r--r-- | gils/head.rv | 33 |
1 files changed, 23 insertions, 10 deletions
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] <path>\n") end -else do + +if context.argv:len() < 2 do + fail("usage: head [n] <path>\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 |
