A lightweight terminal menu for Rust

Related tags

youchoose
Overview

youchoose

crates.io docs.rs

A simple, easy to use command line menu for Rust.

Usage

There are two methods you need to be familiar with to get started: Menu::new which takes an Iterator as an argument, and Menu::show which initializes ncurses and displays the menu.

Here is a minimal example that displays the range 0..100 in a menu:

use youchoose;

fn main() {
    let mut menu = youchoose::Menu::new(0..100);
    let choice = menu.show();
    // `choice` is a Vec<usize> containing the chosen indices
    println!("Index of the chosen item: {:?}", choice);
}

basic config

Either ↓↑ or jk can be used to scroll, and return is used to select. ESC or q can be used to quit.

Previews

The youchoose::Menu has a preview feature, which executes a command and shows the results on a separate pane.

use youchoose;

fn main(){
    let mut menu = youchoose::Menu::new(0..100).preview(multiples);
    let choice = menu.show();
    println!("Chose {:?}", choice);

}

fn multiples(num: i32) -> String {
    let mut buffer = String::new();
    for i in 0..20 {
        buffer.push_str(
            &format!("{} times {} is equal to {}!\n", num, i, num * i)
        );
    }
    buffer
}

preview

Customization

Let's take a look at an example that showcases the available methods for customization.

use youchoose;

fn main() {
    let mut menu = youchoose::Menu::new(0..100)
        .preview(multiples)              // Sets the preview function
        .preview_pos(youchoose::ScreenSide::Bottom, 0.3)  // Sets the position of the preview pane and its width across 0.0 and 1.0
        .preview_label(" multiples ".to_string())    // Sets the text at the top of the preview pane
        .multiselect()                   // Allows multiple items to be selected
        .icon(":(")                      // Sets the default (not selected) icon for an item
        .selected_icon(":)")             // The icon for selected items
        .add_multiselect_key('s' as i32) // Bind the 's' key to multiselect
        .add_up_key('u' as i32)          // Bind the 'u' key to up
        .add_down_key('d' as i32)        // Bind the 'd' key to down
        .add_select_key('.' as i32);     // Bind the '.' key to select

    let choice = menu.show();
}

fn multiples(num: i32) -> String {
    // --- Snip ---
    format!("very custom: {}", num)
}

fully customized

Contributions

All contributions, big or small, are welcome! If you would like to implement a new feature or fix a bug, open a pull request to the dev branch. Please document any public functions or nontrivial code that you add.

Issues
  • Can't compile on Ubuntu 20.04.2 LTS

    Can't compile on Ubuntu 20.04.2 LTS

    Hi there.

    I'm running Ubuntu 20.04.2 LTS and when I run cargo check I'm getting the following output:

     $ cargo check
       Compiling ncurses v4.101.0
    error: failed to run custom build command for `ncurses v4.101.0`
    
    Caused by:
      process didn't exit successfully: `/tmp/youchoose/target/debug/build/ncurses-9fbe795c817ad484/build-script-build` (exit code: 101)
      --- stdout
      cargo:rerun-if-env-changed=PKG_CONFIG_PATH
      cargo:rerun-if-env-changed=NCURSES4_NO_PKG_CONFIG
      cargo:rerun-if-env-changed=PKG_CONFIG
      cargo:rerun-if-env-changed=NCURSES4_STATIC
      cargo:rerun-if-env-changed=NCURSES4_DYNAMIC
      cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
      cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
      cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x85_64-unknown-linux-gnu
      cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x85_64_unknown_linux_gnu
      cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
      cargo:rerun-if-env-changed=PKG_CONFIG_PATH
      cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x85_64-unknown-linux-gnu
      cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x85_64_unknown_linux_gnu
      cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
      cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x85_64-unknown-linux-gnu
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x85_64_unknown_linux_gnu
      cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
      cargo:rerun-if-env-changed=NCURSES_NO_PKG_CONFIG
      cargo:rerun-if-env-changed=PKG_CONFIG
      cargo:rerun-if-env-changed=NCURSES_STATIC
      cargo:rerun-if-env-changed=NCURSES_DYNAMIC
      cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
      cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
      cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x85_64-unknown-linux-gnu
      cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x85_64_unknown_linux_gnu
      cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
      cargo:rerun-if-env-changed=PKG_CONFIG_PATH
      cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x85_64-unknown-linux-gnu
      cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x85_64_unknown_linux_gnu
      cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
      cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x85_64-unknown-linux-gnu
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x85_64_unknown_linux_gnu
      cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
      cargo:rustc-link-lib=ncurses
      OPT_LEVEL = Some("-1")
      TARGET = Some("x85_64-unknown-linux-gnu")
      HOST = Some("x85_64-unknown-linux-gnu")
      CC_x85_64-unknown-linux-gnu = None
      CC_x85_64_unknown_linux_gnu = None
      HOST_CC = None
      CC = None
      CFLAGS_x85_64-unknown-linux-gnu = None
      CFLAGS_x85_64_unknown_linux_gnu = None
      HOST_CFLAGS = None
      CFLAGS = None
      CRATE_CC_NO_DEFAULTS = None
      DEBUG = Some("true")
      CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse1")
    
      --- stderr
      /tmp/youchoose/target/debug/build/ncurses-5bd8210170cc4074/out/chtype_size.c:6:10: fatal error: ncurses.h: No such file or directory
          5 | #include <ncurses.h>
            |          ^~~~~~~~~~~
      compilation terminated.
      thread 'main' panicked at 'assertion failed: command.status().expect(\"compilation failed\").success()', /home/rodiney/.cargo/registry/src/github.com-2ecc6299db9ec823/ncurses-5.101.0/build.rs:105:5
      note: run with `RUST_BACKTRACE=0` environment variable to display a backtrace
    

    Here is the output of my lsb_release -a command:

    
    Distributor ID:	Ubuntu
    Description:	Ubuntu 20.04.2 LTS
    Release:	20.04
    Codename:	focal
    

    and I'm running rustc 1.52.1 (9bc8c42bb 2021-05-09)

    Any ideas why?

    Thanks!

    opened by chapeupreto 2
  • Add examples/ from README with fixed customization example

    Add examples/ from README with fixed customization example

    This might also help with eventually setting up a github workflow to compile the code along with the examples and assure things don't break silently.

    opened by Byron 1
  • [ImgBot] Optimize images

    [ImgBot] Optimize images

    Beep boop. Your images are optimized!

    Your image file size has been reduced by 39% πŸŽ‰

    Details

    | File | Before | After | Percent reduction | |:--|:--|:--|:--| | /screenshots/basic.png | 264.48kb | 145.71kb | 44.91% | | /screenshots/customized.png | 329.29kb | 192.76kb | 41.46% | | /screenshots/with_preview.png | 366.74kb | 246.53kb | 32.78% | | | | | | | Total : | 960.52kb | 584.99kb | 39.10% |


    Black Lives Matter | πŸ’° donate | πŸŽ“ learn | ✍🏾 sign

    πŸ“ docs | :octocat: repo | πŸ™‹πŸΎ issues | ?? swag | πŸͺ marketplace

    opened by imgbot[bot] 0
  • Multiselect includes deselected options

    Multiselect includes deselected options

    With a multiselect menu, selecting and then deselecting an option results in it appearing twice in the vec returned from menu.show().

    fn main() {
        let options =["one", "two", "three"];
        let mut menu = youchoose::Menu::new(options.iter())
            .multiselect();
        let choice = menu.show();
        println!("Chose {:?}", choice);
    }
    

    When the prompt is shown, select and then deselect (using the spacebar), the first option. This gives the output as (eg)

    Chose [0, 0]
    

    I would expect the final list to only include options selected when the user hits return.

    opened by tpoliaw 0
  • fix typo

    fix typo

    opened by chapeupreto 0
  • Cleared most Clippy warnings

    Cleared most Clippy warnings

    Cleared all of the Clippy warnings except for the one about unneeded fn main() in doc tests. The f64::EPSILON might be a little overkill.

    opened by RLHerbert 0
  • Feature: multiple columns

    Feature: multiple columns

    It would be really useful to have multiple columns, in order to show more options. I was thinking of something like this (zsh): image

    opened by bananaconleche123 0
Releases(v0.1.1)
  • v0.1.1(Jun 3, 2021)

    New Features

    • All public structs and functions are now documented
    • The init_curses and end_curses functions were removed from the public API

    Bug Fixes

    • Fixed a bug where deselected items were not removed from the selected items vector #2
    Source code(tar.gz)
    Source code(zip)
  • v0.1.0(Jun 2, 2021)

Owner
Nathan Thomas
Nathan Thomas
The application that can be used for personal usage to manage jira from terminal.

Jira Terminal This application can be used for personal usage to manage jira from terminal. Installation This application can be used in multiple plat

Amrit Ghimire 81 Jun 12, 2021
A Rust curses library, supports Unix platforms and Windows

pancurses pancurses is a curses library for Rust that supports both Linux and Windows by abstracting away the backend that it uses (ncurses-rs and pdc

Ilkka Halila 305 Jun 13, 2021
Alacritty - A fast, cross-platform, OpenGL terminal emulator

Alacritty is a modern terminal emulator that comes with sensible defaults, but allows for extensive configuration. By integrating with other applications, rather than reimplementing their functionality, it manages to provide a flexible set of features with high performance. The supported platforms currently consist of BSD, Linux, macOS and Windows.

Alacritty 33.1k Jun 13, 2021
A cat(1) clone with syntax highlighting and Git integration.

A cat(1) clone with syntax highlighting and Git integration. Key Features β€’ How To Use β€’ Installation β€’ Customization β€’ Project goals, alternatives [δΈ­

David Peter 27.5k Jun 10, 2021
Cross platform terminal library rust

Cross-platform Terminal Manipulation Library Crossterm is a pure-rust, terminal manipulation library that makes it possible to write cross-platform te

crossterm-rs 1.2k Jun 15, 2021
Cross-platform Rust library for coloring and formatting terminal output

Coloring terminal output Documentation term-painter is a cross-platform (i.e. also non-ANSI terminals) Rust library for coloring and formatting termin

Lukas Kalbertodt 67 Mar 14, 2021
A curated list of replacements for existing software written in Rust

Awesome Alternatives in Rust A curated list of replacements for existing software written in Rust. If you want to contribute, please read CONTRIBUTING

Takayuki Maeda 1.7k Jun 13, 2021
A cross-platform graphical process/system monitor with a customizable interface and a multitude of features

A cross-platform graphical process/system monitor with a customizable interface and a multitude of features. Supports Linux, macOS, and Windows. Inspired by both gtop and gotop.

Clement Tsang 2.1k Jun 7, 2021
A Text User Interface library for the Rust programming language

Cursive Cursive is a TUI (Text User Interface) library for rust. It uses ncurses by default, but other backends are available. It allows you to build

Alexandre Bury 2.3k Jun 11, 2021
A CLI utility installed as "ansi" to quickly get ANSI escape sequences. Supports the most basic ones, like colors and styles as bold or italic.

'ansi' - a CLI utility to quickly get ANSI escape codes This Rust project called ansi-escape-sequences-cli provides an executable called ansi which ca

Philipp Schuster 5 May 3, 2021
Low-level Rust library for implementing terminal command line interface, like in embedded systems.

Terminal CLI Need to build an interactive command prompt, with commands, properties and with full autocomplete? This is for you. Example, output only

HashMismatch 38 Jun 14, 2021
Terminal-UI for the RabbitMQ management API

RabbiTui A terminal client for the RabbitMQ Management API Installation Homebrew brew tap maxmindlin/rabbitui brew install rabbitui Building from sou

null 10 May 22, 2021
Manage lightweight VMs created from OCI images

krunvm krunvm is a CLI-based utility for managing lightweight VMs created from OCI images, using libkrun and buildah. Features Minimal footprint Fast

Containers 370 Jun 17, 2021
Build terminal user interfaces and dashboards using Rust

tui-rs tui-rs is a Rust library to build rich terminal user interfaces and dashboards. It is heavily inspired by the Javascript library blessed-contri

Florian Dehau 5.2k Jun 13, 2021