Skip to content

mantas6/fzf-php

Repository files navigation

Fzf Php

GitHub Workflow Status (main) Total Downloads Latest Version License


This package allows you to create fzf powered menus straight from your PHP code.

Demo

Features

  • Automatic fzf binary download
  • Inline fzf configuration
  • Option list styling
  • Selected option preview
  • Laravel support

Installation

Install the package:

composer require mantas6/fzf-php

Usage

Options

Options can be provided in a multitude of ways.

Strings

<?php
use function Mantas6\FzfPhp\fzf;

$selected = fzf(['Apple', 'Orange', 'Grapefruit']);

// 'Apple'
  • Returns null if user cancels the prompt

Arrays

<?php

$selected = fzf(
    options: [
        ['Apples', '1kg'],
        ['Oranges', '2kg'],
        ['Grapefruits', '3kg'],
    ]
);

// ['Apples', '1kg']
  • Each array element represents different column in the finder

Objects

Using toArray
<?php

$selected = fzf(
    options: [
        new Model('Apple'),
        new Model('Orange'),
        new Model('Grapefruits'),
    ]
);

// new Model('Apple')
  • The object class must implement toArray
Implementing PresentsForFinder interface

If using toArray method is not feasible, an interface can be implemented instead.

<?php

use Mantas6\FzfPhp\Concerns\PresentsForFinder;

class Model implements PresentsForFinder
{
    protected string $name;

    public function presentForFinder(): string
    {
        return $this->name;
    }
}

Options presentation

Callback can be provided for presentation. This will work for any options type.

<?php

$selected = fzf(
    options: [
        'Apple',
        'Orange',
        'Grapefruits',
    ],

    present: fn (string $item): string => strtoupper($item),
);

Multiple columns can be present by returning an array.

<?php

$selected = fzf(
    // ...

    present: fn (string $item): array => [
        $item,
        strtoupper($item),
    ],
);

Options as object

Instead of passing options as array, object can be used.

<?php

$selected = fzf(
    options: new MyCustomCollection,
);

The class needs to meet one of the following requirements:

  • Must implement the native Traversable interface
  • Needs to implement toArray() method

Options styling

Option columns can be styling using cell() helper function in the presenter callback.

<?php

use function Mantas6\FzfPhp\cell;

$selected = fzf(
    options: [
        ['name' => 'Apples', 'weight' => 1000],
        ['name' => 'Oranges', 'weight' => 2000],
        ['name' => 'Grapefruits', 'weight' => 3000],
    ],

    // Styling individual items
    present: fn (array $item): array => [
        $item['name'],

        cell($item['weight'], fg: $item['weight'] > 2000 ? 'red' : 'green'),
    ],
);

Formatting options are:

<?php

cell(
    // Text of the cell
    value: 'Text displayed',

    // Alignment in the table (left, right, center)
    align: 'right',

    // Foreground color
    fg: 'white',

    // Background color
    bg: 'red',

    // Column span
    colspan: 2,
);

More information can be found at Symfony Docs: Table

Options preview

Preview window can be enabled for each selected option.

<?php

$selected = fzf(
    options: ['Apple', 'Orange', 'Grapefruit'],

    preview: fn (string $item) => strtoupper($item),
);

If more advanced styling is needed, style() helper can be used.

<?php
use function Mantas6\FzfPhp\style;

$selected = fzf(
    // ...
    preview: function (string $item) {
        return style()
            ->table([
                ['Original', $item],
                ['Uppercase', strtoupper($item)],
            ]);
    }
);
  • Use ->table() for creating compact tables
  • Use ->block() for creating text blocks

Additional variables

fzf provides additional variables to the preview (and other) child processes.

<?php
use Mantas6\FzfPhp\ValueObjects\FinderEnv;

$selected = fzf(
    // ...
    preview: function (string $item, FinderEnv $env) {
        // ...
        $env->key, // The name of the last key pressed
        $env->action, // The name of the last action performed
        // ...
    }
);

Full set of variables are available at fzf Reference - Environment variables exported to child processes

Multi mode

Retrieve multiple options from a list.

<?php

$selected = fzf(
    options: ['Apple', 'Orange', 'Grapefruit'],
    arguments: ['multi' => true],
);

// ['Apple', 'Orange']
  • Returns [] if user cancels the prompt

Arguments

Pass any other fzf configuration arguments:

<?php

$selected = fzf(
    options: ['Apple', 'Orange', 'Grapefruit'],
    arguments: [
        'height' => '40%',
        'cycle' => true,
    ],
);
  • Arguments delimiter (or d), with-nth are used internally, and will be overridden if specified
  • Arguments that transform output may not be supported
  • Consult fzf Reference for all available options

Headers

Fixed header will be displayed if header list is passed.

<?php

$selected = fzf(
    options: ['Apple', 'Orange', 'Grapefruit'],
    headers: ['Fruit'],
);

Reusable object approach

Class instance can be created directly for more reusable approach.

<?php

use Mantas6\FzfPhp\FuzzyFinder;

$finder = new FuzzyFinder;

$fruit = $finder->ask(['Apple', 'Orange', 'Grapefruit']);

// ...

$weight = $finder->ask(['250g', '500g', '1kg']);

Additional configuration is available through the class methods.

<?php

$finder->present(...);

$finder->arguments(...);

Configuration

Use system fzf binary instead of fetching it.

<?php
// YourAppServiceProvider.php

use Mantas6\FzfPhp\FuzzyFinder;

FuzzyFinder::usingCommand(['/usr/bin/env', 'fzf']);
  • Automatic binary download will be disabled when custom command is set

Set global arguments for all prompts.

<?php
// YourAppServiceProvider.php

FuzzyFinder::usingDefaultArguments(['pointer' => '->']);

Binary download

The fzf binary is downloaded automatically on the first use, however you can initiate the download manually.

./vendor/bin/fzf-php-install

Automatic update

To optimize the initial start, especially when CI/CD is used, it is recommended to add this to your composer.json file.

{
    "scripts": {
        "post-update-cmd": [
            "./vendor/bin/fzf-php-install"
        ],
        "post-install-cmd": [
            "./vendor/bin/fzf-php-install"
        ]
    }
}

See also:

About

Interactive fzf command line menus in PHP

Resources

License

Stars

Watchers

Forks

Packages

No packages published