/linenoise-swift

A pure Swift replacement for readline

Primary LanguageSwiftBSD 2-Clause "Simplified" LicenseBSD-2-Clause

Linenoise-Swift

A pure Swift implementation of the Linenoise library. A minimal, zero-config readline replacement.

Supports

  • Mac OS and Linux
  • Line editing with emacs keybindings
  • History handling
  • Completion
  • Hints

Pure Swift

Implemented in pure Swift, with a Swifty API, this library is easy to embed in projects using Swift Package Manager, and requires no additional dependencies.

Contents

API

Quick Start

Linenoise-Swift is easy to use, and can be used as a replacement for Swift.readLine. Here is a simple example:

let ln = LineNoise()

do {
	let input = try ln.getLine(prompt: "> ")
} catch {
	print(error)
}
	

Basics

Simply creating a new LineNoise object is all that is necessary in most cases, with STDIN used for input and STDOUT used for output by default. However, it is possible to supply different files for input and output if you wish:

// 'in' and 'out' are standard POSIX file handles
let ln = LineNoise(inputFile: in, outputFile: out)

History

Adding to History

Adding to the history is easy:

let ln = LineNoise()

do {
	let input = try ln.getLine(prompt: "> ")
	ln.addHistory(input)
} catch {
	print(error)
}

Limit the Number of Items in History

You can optionally set the maximum amount of items to keep in history. Setting this to 0 (the default) will keep an unlimited amount of items in history.

ln.setHistoryMaxLength(100)

Saving the History to a File

ln.saveHistory(toFile: "/tmp/history.txt")

Loading History From a File

This will add all of the items from the file to the current history

ln.loadHistory(fromFile: "/tmp/history.txt")

History Editing Behavior

By default, any edits by the user to a line in the history will be discarded if the user moves forward or back in the history without pressing Enter. If you prefer to have all edits preserved, then use the following:

ln.preserveHistoryEdits = true

Completion

Completion example

Linenoise supports completion with tab. You can provide a callback to return an array of possible completions:

let ln = LineNoise()

ln.setCompletionCallback { currentBuffer in
    let completions = [
        "Hello, world!",
        "Hello, Linenoise!",
        "Swift is Awesome!"
    ]
    
    return completions.filter { $0.hasPrefix(currentBuffer) }
}

The completion callback gives you whatever has been typed before tab is pressed. Simply return an array of Strings for possible completions. These can be cycled through by pressing tab multiple times.

Hints

Hints example

Linenoise supports providing hints as you type. These will appear to the right of the current input, and can be selected by pressing Return.

The hints callback has the contents of the current line as input, and returns a tuple consisting of an optional hint string and an optional color for the hint text, e.g.:

let ln = LineNoise()

ln.setHintsCallback { currentBuffer in
    let hints = [
        "Carpe Diem",
        "Lorem Ipsum",
        "Swift is Awesome!"
    ]
    
    let filtered = hints.filter { $0.hasPrefix(currentBuffer) }
    
    if let hint = filtered.first {
        // Make sure you return only the missing part of the hint
        let hintText = String(hint.dropFirst(currentBuffer.count))
        
        // (R, G, B)
        let color = (127, 0, 127)
        
        return (hintText, color)
    } else {
        return (nil, nil)
    }
}

Acknowledgements

Linenoise-Swift is heavily based on the original linenoise library by Salvatore Sanfilippo (antirez)