Skip to content

Commit

Permalink
Fzf lib update
Browse files Browse the repository at this point in the history
  • Loading branch information
kLabz committed May 21, 2024
1 parent 80313c5 commit 308cd86
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
13 changes: 13 additions & 0 deletions libs/fzf/haxelib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "fzf",
"description": "Haxe implementation of fzf-like picker",
"version": "0.0.1",
"releasenote": "Basic impl for eval",
"url": "https://github.com/kLabz/haxe-manager/tree/master/libs/fzf",
"license": "MIT",
"tags": [],
"classPath": "src",
"contributors": [
"klabz"
]
}
18 changes: 15 additions & 3 deletions libs/fzf/src/fzf/Fzf.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ import fuzzaldrin.Filter;
import fuzzaldrin.Scorer;
import fzf.WordNav;

typedef FzfOptions = {
@:optional var prompt:String;
@:optional var geom:{width:Int, height:Int};
}

class Fzf {
final items:Array<String>;
final cb:Option<String>->Void;
final prompt:String;
final strippedPrompt:String;
final tty:Tty;
final options:FzfOptions;

var scroll:Int = 0;
var cursor:Int = 0;
Expand Down Expand Up @@ -58,9 +64,14 @@ class Fzf {
static inline var Grey = ANSI.CSI + '38;5;241m';
static inline var GreyBack = ANSI.CSI + '48;5;236m';

public function new(items:Array<String>, ?prompt:String = "", cb:Option<String>->Void) {
static function defaultOptions():FzfOptions {
return {};
}

public function new(items:Array<String>, ?options:FzfOptions, cb:Option<String>->Void) {
this.items = items;
this.cb = cb;
this.options = options ?? defaultOptions();
this.filteredItems = items.map(i -> {
candidate: i,
string: i,
Expand All @@ -71,11 +82,12 @@ class Fzf {
});

// TODO: strip sequences in prompt input?
var prompt = options.prompt ?? "";
this.strippedPrompt = (prompt == "" ? "" : prompt + " ") + "> ";
this.prompt = ANSI.set(Bold) + LightBlue + this.strippedPrompt + ANSI.set(Off);

// TODO: find a way to have a proper _new_ tty
this.tty = Tty.init(Loop.defaultLoop(), File.stderr).resolve();
if (options.geom == null) this.tty = Tty.init(Loop.defaultLoop(), File.stderr).resolve();

var esc = [];
while (true) {
Expand Down Expand Up @@ -195,7 +207,7 @@ class Fzf {

function redraw():Void {
var screen = "";
final geom = tty.getWinSize().resolve();
final geom = options.geom ?? tty.getWinSize().resolve();
final height = geom.height - 2;
final hasScroll = height < filteredItems.length;

Expand Down
2 changes: 1 addition & 1 deletion src/HaxeSelect.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class HaxeSelect {
public static function fzf():Void {
final prompt = 'Current: ' + Utils.getCurrentFull().or('none');

new Fzf(Utils.getVersions(), prompt, res -> {
new Fzf(Utils.getVersions(), {prompt: prompt}, res -> {
switch res {
case None: Sys.println('No Haxe version selected');
case Some(v): select(v);
Expand Down

0 comments on commit 308cd86

Please sign in to comment.