Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/joachimschmidt557/linenoize
A port of linenoise to zig
https://github.com/joachimschmidt557/linenoize
readline zig zig-package
Last synced: 9 days ago
JSON representation
A port of linenoise to zig
- Host: GitHub
- URL: https://github.com/joachimschmidt557/linenoize
- Owner: joachimschmidt557
- License: mit
- Created: 2020-01-30T13:46:37.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-09-07T15:50:47.000Z (2 months ago)
- Last Synced: 2024-10-19T03:58:02.098Z (21 days ago)
- Topics: readline, zig, zig-package
- Language: Zig
- Homepage:
- Size: 96.7 KB
- Stars: 53
- Watchers: 6
- Forks: 9
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-zig - linenoize🗒️A port of linenoise to zig
README
# linenoize
A port of [linenoise](https://github.com/antirez/linenoise) to zig
aiming to be a simple readline for command-line applications written
in zig. It is written in pure zig and doesn't require
libc. `linenoize` works with the latest stable zig version (0.13.0).In addition to being a full-fledged zig library, `linenoize` also
serves as a drop-in replacement for linenoise. As a proof of concept,
the example application from linenoise can be built with `zig build
c-example`.## Features
- Line editing
- Completions
- Hints
- History
- Multi line mode
- Mask input mode### Supported platforms
- Linux
- macOS (Experimental)
- TODO: Windows
- TODO: FreeBSD## Examples
### Minimal example
```zig
const std = @import("std");
const Linenoise = @import("linenoise").Linenoise;pub fn main() !void {
const allocator = std.heap.page_allocator;var ln = Linenoise.init(allocator);
defer ln.deinit();while (try ln.linenoise("hello> ")) |input| {
defer allocator.free(input);
std.debug.print("input: {s}\n", .{input});
try ln.history.add(input);
}
}
```### Example of more features
``` zig
const std = @import("std");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;const Linenoise = @import("linenoise").Linenoise;
fn completion(alloc: *Allocator, buf: []const u8) ![][]const u8 {
if (std.mem.eql(u8, "z", buf)) {
var result = ArrayList([]const u8).init(alloc);
try result.append(try alloc.dupe(u8, "zig"));
try result.append(try alloc.dupe(u8, "ziglang"));
return result.toOwnedSlice();
} else {
return &[_][]const u8{};
}
}fn hints(alloc: *Allocator, buf: []const u8) !?[]const u8 {
if (std.mem.eql(u8, "hello", buf)) {
return try alloc.dupe(u8, " World");
} else {
return null;
}
}pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = &arena.allocator;var ln = Linenoise.init(allocator);
defer ln.deinit();// Load history and save history later
ln.history.load("history.txt") catch std.debug.print("Failed to load history\n", .{});
defer ln.history.save("history.txt") catch std.debug.print("Failed to save history\n", .{});// Set up hints callback
ln.hints_callback = hints;// Set up completions callback
ln.completions_callback = completion;// Enable mask mode
// ln.mask_mode = true;// Enable multiline mode
// ln.multiline_mode = true;while (try ln.linenoise("hello> ")) |input| {
defer allocator.free(input);
std.debug.print("input: {s}\n", .{input});
try ln.history.add(input);
}
}
```