Skip to content

repl

stable

Utilities for building interactive REPLs and line editors: bracket balance checking, bracket highlighting, ANSI stripping, text wrapping, and a persistent command History class.

use plugin repl::{is_balanced, find_matching_bracket, highlight_brackets, …}
17 functions Utilities
/ filter jk navigate Esc clear
Functions (17)
  1. is_balanced Check if brackets and quotes are balanced
  2. find_matching_bracket Find the position of a matching bracket
  3. highlight_brackets List all brackets with depth info
  4. strip_ansi Remove ANSI escape sequences from text
  5. wrap_text Word-wrap text to a column width
  6. common_prefix Find common prefix of a list of strings
  7. History Creates a new command history instance.
  8. add Append a line to history
  9. get Retrieve a history entry by index
  10. search Search history entries by substring
  11. count Number of entries in history
  12. last Most recent history entry
  13. clear Remove all history entries
  14. to_table Return all entries as a table
  15. remove_duplicates Deduplicate entries, keeping latest
  16. remove Remove a single entry by index
  17. slice Get a range of history entries

Check if brackets and quotes are balanced

Returns true if all brackets ((), [], {}) and string quotes (', ") in input are properly balanced and closed. Use this to decide whether a user's input is complete before executing it.

use plugin repl::{is_balanced}

print(is_balanced("(a + b)"))         // true
print(is_balanced("(a + b"))          // false
print(is_balanced('{"key": "val"}'))  // true

Find the position of a matching bracket

Given a string and a position of an opening or closing bracket, returns the position of the paired bracket. Returns nil if no match is found or pos is not a bracket character.

use plugin repl::{find_matching_bracket}

let src = "(hello (world))"
let close = find_matching_bracket(src, 0)
print(close)  // 14

List all brackets with depth info

Returns a table of all bracket characters found in input. Each entry has pos (byte offset), depth (nesting level), and char (the bracket character).

use plugin repl::{highlight_brackets}

let info = highlight_brackets("fn(a, [b, c])")
for _, b in info {
  print("{b["char"]} at {b["pos"]} depth {b["depth"]}")
}

Remove ANSI escape sequences from text

Removes ANSI escape sequences (color codes, cursor movement, etc.) from a string. Useful when storing or processing terminal output.

use plugin repl::{strip_ansi}

let clean = strip_ansi("\x1b[31mError:\x1b[0m file not found")
print(clean)  // Error: file not found

Word-wrap text to a column width

Word-wraps text to at most width characters per line, splitting on whitespace boundaries. Preserves existing newlines.

use plugin repl::{wrap_text}

let msg = "This is a very long description that should be wrapped at a reasonable column width for terminal display."
print(wrap_text(msg, 40))

Find common prefix of a list of strings

Returns the longest common prefix shared by all strings in the table. Useful for tab-completion suggestions.

use plugin repl::{common_prefix}

let candidates = ["print", "println", "printf"]
print(common_prefix(candidates))  // "print"

Creates a new command history instance.

Creates a new command history instance. History entries are stored in insertion order with 1-based indexing.

use plugin repl::{History}

let hist = History()
hist.add("let x = 42")
hist.add("print(x)")
print(hist.count())  // 2

Append a line to history

Appends a line string to the end of the history.

hist.add("use plugin json::{parse}")

Retrieve a history entry by index

Returns the history entry at the given 1-based index, or nil if the index is out of range.

print(hist.get(1))  // first entry

Number of entries in history

Returns the number of entries currently in the history.

print("{hist.count()} commands in history")

Most recent history entry

Returns the most recently added entry, or nil if history is empty.

let prev = hist.last()
if prev != nil {
  print("last command: {prev}")
}

Remove all history entries

Removes all entries from the history.

hist.clear()

Return all entries as a table

Returns all entries as a numbered table. Useful for displaying the full history list.

let all = hist.to_table()
for i, line in all {
  print("{i}: {line}")
}

Deduplicate entries, keeping latest

Removes duplicate entries in-place, keeping the most recent occurrence of each unique line.

hist.add("print(x)")
hist.add("let x = 1")
hist.add("print(x)")
hist.remove_duplicates()
print(hist.count())  // 2

Remove a single entry by index

Removes the entry at the given 1-based index. Returns true on success, false if the index is out of range.

hist.remove(1)

Get a range of history entries

Returns entries from index start (inclusive) to end (exclusive), re-indexed from 1.

let recent = hist.slice(hist.count() - 4, hist.count())
enespt-br