linenoise

linenoise line editing library bindings for Kit

Files

FileDescription
.editorconfigEditor formatting configuration
.gitignoreGit ignore rules for build artifacts and dependencies
.tool-versionsasdf tool versions (Zig, Kit)
LICENSEMIT license file
README.mdThis file
c/kit_linenoise.cC FFI wrapper exposing a Kit-friendly API
c/kit_linenoise.hC header for the FFI wrapper
c/linenoise.cBundled linenoise implementation
c/linenoise.hBundled linenoise public header
examples/basic.kitInteractive REPL example
kit.tomlPackage manifest with metadata, native build settings, and tasks
src/linenoise.kitKit bindings and high-level API
tests/linenoise.test.kitAPI existence tests

Dependencies

No Kit package dependencies. No external system libraries are required.

The linenoise C source is bundled in this package and is compiled into libkit_linenoise.dylib during kit install and kit dev.

Installation

kit add gitlab.com/kit-lang/packages/kit-linenoise.git

This package requires the ffi capability because it calls the bundled native library.

Usage

import Kit.Linenoise as Linenoise

main = fn =>
  println "Type 'quit' to exit\n"

  # Configure history.
  Linenoise.history-set-max 100

  match Linenoise.history-load "/tmp/kit-linenoise-history.txt"
    | Ok _ -> println "(History loaded)"
    | Err _ -> println "(No previous history)"

  repl = fn(running) =>
    if running then
      line = Linenoise.readline "kit> "
      if String.length line == 0 then
        println "\nGoodbye!"
        match Linenoise.history-save "/tmp/kit-linenoise-history.txt"
          | Ok _ -> println "(History saved)"
          | Err e -> println "Failed to save history: ${e}"
        no-op
      else if line == "quit" then
        println "Goodbye!"
        match Linenoise.history-save "/tmp/kit-linenoise-history.txt"
          | Ok _ -> println "(History saved)"
          | Err e -> println "Failed to save history: ${e}"
        no-op
      else if line == "clear" then
        Linenoise.clear-screen
        repl true
      else
        Linenoise.history-add line
        println "You typed: ${line}"
        repl true
    else
      no-op

  repl true

main

Run examples with --allow=ffi:

kit run examples/basic.kit --allow=ffi

API

FunctionDescription
readline promptRead a line using linenoise editing with the given prompt
readline-defaultRead a line with the default > prompt
history-add lineAdd a line to in-memory history
history-set-max lenSet the maximum number of history entries
history-save filenameSave history to a file, returning Result Unit String
history-load filenameLoad history from a file, returning Result Unit String
set-multiline enabledEnable or disable multi-line editing
set-mask-mode enabledEnable or disable password-style masking
clear-screenClear the terminal screen

Development

Running Examples

Run examples with the interpreter:

kit run examples/basic.kit --allow=ffi

Compile examples to a native binary:

kit build examples/basic.kit -o basic --allow=ffi && ./basic

Running Tests

Run the test suite:

kit test

Run the test suite with coverage:

kit test --coverage

Running kit dev

Run the standard development workflow (native build, format, check, test):

kit dev

This will:

  1. Build the bundled native library
  2. Format and check source files in src/
  3. Type check examples in examples/
  4. Run tests in tests/ with coverage

Running Parity Checks

Run interpreter/compiler parity checks for examples:

kit parity --no-spinner --failures-only

The basic example is marked @check(interactive), so parity build-checks it without trying to drive the REPL interactively.

Generating Documentation

Generate API documentation from doc comments:

kit doc src/linenoise.kit

Note: Kit sources with doc comments (##) will generate HTML documents in docs/*.html.

Cleaning Build Artifacts

Remove generated files, caches, and build artifacts:

kit task clean

Note: Defined in kit.toml.

Native Build Notes

The native library is configured in kit.toml:

  • csources = ["c/kit_linenoise.c", "c/linenoise.c"]
  • includes = ["c"]
  • libraries = ["kit_linenoise"]

Each extern-c binding in src/linenoise.kit links against kit_linenoise, which lets both kit run and compiled binaries resolve the wrapper symbols from the package library.

Local Installation

To install this package locally for development:

kit install

This installs the package to ~/.kit/packages/@kit/linenoise/, making it available for import as Kit.Linenoise in other projects.

License

This package is released under the MIT License - see LICENSE for details.

linenoise is released under the BSD 2-Clause License.

Exported Functions & Types

readline

Read a line of input with the given prompt.

Supports line editing (arrow keys, backspace, etc.) and history navigation (up/down arrows). Returns empty string on EOF (Ctrl-D).

Parameters:

Returns:

String -> String

readline-default

Read a line with the default prompt ("> ").

String

history-add

Add a line to the history.

Parameters:

    String -> Unit

history-set-max

Set the maximum number of history entries.

Parameters:

    Int -> Unit

history-save

Save history to a file.

Parameters:

Returns:

NonEmptyString -> Result Unit String

history-load

Load history from a file.

Parameters:

Returns:

NonEmptyString -> Result Unit String

set-multiline

Enable or disable multi-line editing mode.

When enabled, long lines wrap to the next line instead of scrolling.

Parameters:

    Bool -> Unit

set-mask-mode

Enable or disable password masking mode.

When enabled, typed characters are shown as asterisks.

Parameters:

    Bool -> Unit

clear-screen

Clear the terminal screen.

Unit