[][src]Module rustofi::components

extra rofi window types usable to create an application, essentially navigation result pages ItemList, ActionList and EntryBox are additional components or controls you can use to build your application.

Examples

Using ItemList

This example demonstrates using the ItemList by creating a list of strings then printing off which was selected. Run with:

cargo run --example simple

// examples/simple.rs
use rustofi::components::ItemList;
use rustofi::RustofiResult;

fn simple_app() -> RustofiResult {
    // create a list of strings to pass as rofi options. Note that this can be any type you want,
    // the callback will always return the type you passed in
    let rustofi_entries = vec![
        "Entry 1".to_string(),
        "Entry 2".to_string(),
        "Entry 3".to_string(),
    ];
    // create a ItemList with a callback that prints which item was selected.
    ItemList::new(rustofi_entries, Box::new(simple_callback)).display("Select an entry".to_string())
}

pub fn simple_callback(s: &String) -> RustofiResult {
    // when an item is clicked, print the name!
    println!("Clicked on item: {}", s);
    RustofiResult::Success
}

fn main() {
    loop {
        match simple_app() {
            //!  loop unless the user requests we exit
            RustofiResult::Error => break,
            RustofiResult::Exit => break,
            RustofiResult::Cancel => break,
            RustofiResult::Blank => break,
            _ => {}
        }
    }
}

Using ActionList

This example demonstrates using the ActionList to manipulate an object's state. As it can't return a modified item through the callback, you'll need to store your modified changes with real storage or a global variable of some sort. In this example the data is only temporary. Run with:

cargo run --example simple_action

// examples/simple_action.rs
use rustofi::components::ActionList;
use rustofi::RustofiResult;

// notice the Clone derive and Display implementation? These are
// necessary if you want to pass in a custom type!
// Otherwise you'll get an error like this
// error[E0277]: the trait bound `Person: std::clone::Clone` is not satisfied
#[derive(Clone)]
pub struct Person {
    pub age: i32,
    pub name: String
}
impl std::fmt::Display for Person {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.name) // just display the name when calling to_string()
    }
}
fn simple_app(person: Person) -> RustofiResult {
    // create two actions, one that simulates an age increase, one a decrease
    let rustofi_entries = vec!["Age Up".to_string(), "Age Down".to_string()];
    // construct the ActionList and immediately display it with a prompt
    // showing which person is being modified
    ActionList::new(person.clone(), rustofi_entries, Box::new(simple_callback))
        .display(format!("looking at {}, age {}", person.name, person.age))
}

pub fn simple_callback(person: &Person, action: &String) -> RustofiResult {
    println!("selected action: {}", action);
    // match which action was selected
    if action == "Age Up" {
        println!("{} age + 5 is: {} ", person.name, person.age);
    } else if action == "Age Down" {
        println!("{} age - 5 is: {}", person.name, person.age);
    } else { // user entered a custom string
        println!("invalid action!");
        return RustofiResult::Error;
    }
    RustofiResult::Success
}

fn main() {
    let mut p = Person {
        age: 15,
        name: "joe".to_string()
    };
    loop {
        match simple_app(p.clone()) {
            // loop until an exit or error occurs
            RustofiResult::Error => break,
            RustofiResult::Exit => break,
            RustofiResult::Cancel => break,
            RustofiResult::Blank => break, // we could give the blank entry special powers
            _ => {}
        }
    }
}

Structs

ActionList

ActionList is a simple rofi window with a selection of strings that operate on a single item T. When a selection is made, the action_callback is called with the item and action name passed as arguments

EntryBox

empty struct representing a rofi window used to take and return user input as a string

ItemList

ItemList is a simple rofi window with a selection of items backed by a type T. Each item runs the same callback.