0.0
No commit activity in last 3 years
No release in over 3 years
This gem allows you to select array elements (where an array is some arbitrary input) with a pretty text user interface.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.9
~> 10.0
 Project Readme

Installation

$ gem install ansi-select

Usage

There are two options:

  • A standalone executable. Lines passed to STDIN will form your options. The result will be printed to STDOUT.
echo some words to choose from | tr ' ' '\n' | ansi-select

cd $(ls -d */ | ansi-select) # Go to a visually selected subdirectory.
git checkout $(git branch | ansi-select) # The same, but with git branches.

rm -r $(ls | ansi-select --multi) # Delete all the selected files.
  • A Ruby library.
require "ansi/selector"

beverage = Ansi::Selector.select(["coffee", "tee"])

puts "Would you like some additions?"
additions = Ansi::Selector.multi_select(["sugar", "cream", "milk"])

print "Here's your #{beverage}. "
print "We've also added #{additions.join(', ')}." if additions.present?

The Ruby interface has an additional benefit of accepting and returning any objects instead of strings. Also, if you'd like a custom formatter for them, you can pass it as a second option (the default one calls #to_s):

Ansi::Selector.select(objects, ->(object) { "❤❤❤#{object}❤❤❤" })

Keyboard

  • Up and down arrows, j/k, or Ctrl+N/P to navigate.
  • Space to toggle in multi select mode.
  • Return to choose.
  • Ctrl+C or q to quit.