sued 0.24.2

shut up editor - a minimalist line-based text editor written in Rust
Documentation
--[[ General info:
    This file contains the main entry point for sued's Lua bindings. Visit
    /src/lua.rs for the Rust code that imports this file.

    This file is part of sued.

    sued is free software licensed under the Apache License, Version 2.0.
]]

--[[ This file:
    This file will be pulled into the Rust side of sued using `include_str!`,
    so it'll be compiled into a string literal.

    This means you SHOULD NOT require or dofile other Lua files in this file
    unless you're including those Lua files alongside the sued executable, which
    is almost certainly not what you want to deal with.

    Use sued's `~script` command if you want to run Lua scripts from inside the
    editor.
--]]

--[[ Globals:
    sued exposes two globals, `sued_buffer` and `sued_file_path`.
    
    - `sued_buffer: table<string>`, linked to `state.buffer.contents: Vec<String>`
    - `sued_file_path: string`, linked to `state.buffer.file_path: Option<String>`
    
    `sued_buffer` will be synchronised with sued's `state.buffer.contents`,
    but `sued_file_path` will not be. This means you can modify `sued_buffer`
    from the Lua side, but `sued_file_path` is effectively read-only.
--]]

--[[ Lua environment:
    sued's Lua environment is lazy-loaded and will not be made available until
    an `~eval` or `~script` command is issued, at which point this file will be
    included.

    This has no noticable impact on usage, but it's good to be informed.
--]]

--[[ The `print_buffer` function:
    This function serves as a test function for sued's Lua bindings.
        
    It simply prints the current contents of `sued_buffer`, which is practically
    the same as the current buffer in sued itself.

    This function prints a line for every line in the buffer, with line numbers,
    featuring the same separation character (`│`) as sued's `~show` and `~point`
    commands use.

    To run this in sued, run `~eval print_buffer()`.
--]]
function print_buffer()
    for index, line in ipairs(sued_buffer) do
        print(index .. "" .. line)
    end
end

--[[ The `count_lines` function:
    This function counts the number of lines in a file.

    To run this in sued, run `~eval count_lines("<filename>")`.
    To count the lines of the file opened in sued, run `~eval count_lines(sued_file_path)`.
]]
function count_lines(filename)
    local file = io.open(filename, "r")
    if not file then
        print("Error: Could not open file " .. filename)
        return 0
    end

    local line_count = 0

    for line in file:lines() do
        line_count = line_count + 1
    end

    file:close()
    return line_count
end

--[[ The `startup` function:
    This will be run as soon as this file is loaded into sued's Lua environment.
    You can think of it as the `main` entry point for the bindings.
--]]
function startup()
    print("" .. _VERSION:lower() .. " bindings loaded")
end
startup()