From 61f85cfdda0098d3227b8e9d3d09b23fec3235d9 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 9 Oct 2024 22:27:46 -0400 Subject: [PATCH] Fixed seek method of the read command --- bulb/bulb.ha | 25 ++++++++++++++++--------- cmd/bulb/main.ha | 4 ++-- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/bulb/bulb.ha b/bulb/bulb.ha index fe485f8..198909f 100644 --- a/bulb/bulb.ha +++ b/bulb/bulb.ha @@ -11,7 +11,7 @@ let bulb_path: []str = []; export type no_such_board = !void; export type invalid_board = !void; -export type error = (io::error | fs::error | no_such_board | invalid_board); +export type error = !(io::error | fs::error | no_such_board | invalid_board); export fn strerror(err: error) str = match(err) { case no_such_board => return "No such board"; @@ -20,13 +20,15 @@ case let err: fs::error => return fs::strerror(err); case let err: io::error => return io::strerror(err); }; -export fn read(board: str, number: int) (void | error) = { +export fn read(output: io::handle, board: str, number: int) (void | error) = { validate_board_name(board)?; let location = look_up_board(board)?; let file = os::open(location)?; defer io::close(file)!; - seek_to_nth_last_line(file, number)?; - io::copy(os::stdout, file)?; + // we add 1 to acount for the blank line that will always be at the + // bottom + seek_to_nth_last_line(file, number + 1)?; + io::copy(output, file)?; }; export fn post(board: str, user_name: (str | void), message: str) (void | error) = { @@ -42,8 +44,8 @@ export fn post(board: str, user_name: (str | void), message: str) (void | error) fmt::fprintf(file, "{}: {}\n", user, message)?; }; -export fn list() (void | error) = { - for (let directory .. bulb_path) list_boards_in(directory)?; +export fn list(output: io::handle) (void | error) = { + for (let directory .. bulb_path) list_boards_in(output, directory)?; }; export fn look_up_board(board: str) (str | no_such_board | invalid_board) = { @@ -74,11 +76,11 @@ export fn validate_board_name(board: str) (void | invalid_board) = { }; }; -fn list_boards_in(directory: str) (void | fs::error) = { +fn list_boards_in(output: io:: handle, directory: str) (void | error) = { let entries = os::readdir(directory)?; defer fs::dirents_free(entries); for (let entry .. entries) { - fmt::println(entry.name)!; + fmt::fprintln(output, entry.name)?; }; }; @@ -108,7 +110,12 @@ fn seek_to_nth_last_line(file: io::handle, number: int) (void | io::error) = { for (let index = len(buffer): int - 1; index >= 0; index -= 1) { if (buffer[index: size] == '\n') number -= 1; if (number <= 0) { - io::seek(file, current + index: io::off, io::whence::SET)?; + current += index; + io::seek(file, current: io::off, io::whence::SET)?; + if (current != end) { + current += 1; + io::seek(file, current, io::whence::SET)?; + }; return void; }; }; diff --git a/cmd/bulb/main.ha b/cmd/bulb/main.ha index a1802e7..7e39470 100644 --- a/cmd/bulb/main.ha +++ b/cmd/bulb/main.ha @@ -85,7 +85,7 @@ fn subcmd_read(cmd: *getopt::command) (void | errors::invalid) = { }; }; - match (bulb::read(board, number)) { + match (bulb::read(os::stdout, board, number)) { case let err: bulb::error => fmt::errorf("{}: Could not read {}: {}\n", name, board, bulb::strerror(err))!; case void => void; @@ -93,7 +93,7 @@ fn subcmd_read(cmd: *getopt::command) (void | errors::invalid) = { }; fn subcmd_ls(cmd: *getopt::command) void = { - match (bulb::list()) { + match (bulb::list(os::stdout)) { case let err: bulb::error => fmt::errorf("{}: Could not list boards: {}\n", name, bulb::strerror(err))!; case void => void;