cursive-split-panel 0.0.6

Split panel for the Cursive TUI library
Documentation
use {
    cursive::{direction::*, event::*},
    std::collections::*,
};

/// Split panel action map.
pub type Actions = HashMap<Event, Action>;

//
// Action
//

/// Split panel action.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Action {
    /// Move divider towards front.
    MoveDividerToFront,

    /// Move divider towards back.
    MoveDividerToBack,

    /// Toggle border.
    ///
    /// Intended for debugging.
    ToggleBorder,

    /// Toggle visible divider.
    ///
    /// Intended for debugging.
    ToggleVisibleDivider,

    /// Toggle movable divider.
    ///
    /// Intended for debugging.
    ToggleMovableDivider,

    /// Toggle orientation.
    ///
    /// Will set the default action events for the orientation.
    ///
    /// Intended for debugging.
    ToggleOrientation,
}

impl Action {
    /// Default split panel action map.
    pub fn defaults(orientation: Orientation) -> Actions {
        match orientation {
            Orientation::Horizontal => [
                (Event::Shift(Key::Left), Self::MoveDividerToFront),
                (Event::Shift(Key::Right), Self::MoveDividerToBack),
            ],
            Orientation::Vertical => {
                [(Event::Shift(Key::Up), Self::MoveDividerToFront), (Event::Shift(Key::Down), Self::MoveDividerToBack)]
            }
        }
        .into()
    }

    /// Remove from action map.
    ///
    /// Return true if removed.
    pub fn remove(&self, actions: &mut Actions) -> bool {
        let events = self.events(actions);
        for event in &events {
            actions.remove(event);
        }
        !events.is_empty()
    }

    /// Events for the action.
    pub fn events(&self, actions: &Actions) -> Vec<Event> {
        actions
            .into_iter()
            .filter_map(|(event, action)| if action == self { Some(event.clone()) } else { None })
            .collect()
    }
}