Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andybest/linenoise-swift
A pure Swift replacement for readline
https://github.com/andybest/linenoise-swift
console library linenoise pure-swift swift terminal
Last synced: about 1 month ago
JSON representation
A pure Swift replacement for readline
- Host: GitHub
- URL: https://github.com/andybest/linenoise-swift
- Owner: andybest
- License: bsd-2-clause
- Created: 2017-06-19T21:23:14.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-20T11:06:34.000Z (almost 2 years ago)
- Last Synced: 2024-10-01T08:35:13.733Z (2 months ago)
- Topics: console, library, linenoise, pure-swift, swift, terminal
- Language: Swift
- Size: 64.5 KB
- Stars: 143
- Watchers: 5
- Forks: 13
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- fucking-awesome-swift - LineNoise - A zero-dependency replacement for readline. (Libs / Command Line)
- awesome-swift - LineNoise - A zero-dependency replacement for readline. (Libs / Command Line)
- awesome-swift - LineNoise - A pure Swift replacement for readline ` 📝 3 months ago ` (Command Line [🔝](#readme))
- awesome-swift - LineNoise - A zero-dependency replacement for readline. (Libs / Command Line)
README
# Linenoise-Swift
A pure Swift implementation of the [Linenoise](http://github.com/antirez/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](#api)
* [Quick Start](#quick-start)
* [Basics](#basics)
* [History](#history)
+ [Adding to History](#adding-to-history)
+ [Limit the Number of Items in History](#limit-the-number-of-items-in-history)
+ [Saving the History to a File](#saving-the-history-to-a-file)
+ [Loading History From a File](#loading-history-from-a-file)
+ [History Editing Behavior](#history-editing-behavior)
* [Completion](#completion)
* [Hints](#hints)
- [Acknowledgements](#acknowledgements)# API
## Quick Start
Linenoise-Swift is easy to use, and can be used as a replacement for [`Swift.readLine`](https://developer.apple.com/documentation/swift/1641199-readline). Here is a simple example:```swift
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:```swift
// '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:```swift
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.
```swift
ln.setHistoryMaxLength(100)
```### Saving the History to a File
```swift
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
```swift
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:
```swift
ln.preserveHistoryEdits = true
```## Completion
![Completion example](https://github.com/andybest/linenoise-swift/raw/master/images/completion.gif)Linenoise supports completion with `tab`. You can provide a callback to return an array of possible completions:
```swift
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](https://github.com/andybest/linenoise-swift/raw/master/images/hints.gif)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.:
```swift
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](http://github.com/antirez/linenoise) by [Salvatore Sanfilippo (antirez)](http://github.com/antirez)