ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - andybest/linenoise-swift: A pure Swift replacement for readline
A pure Swift replacement for readline. Contribute to andybest/linenoise-swift development by creating an account on GitHub.
Visit Site

GitHub - andybest/linenoise-swift: A pure Swift replacement for readline

GitHub - andybest/linenoise-swift: A pure Swift replacement for readline

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)

More Resources
to explore the angular.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory