1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use std::cmp;
use prototty::*;
use prototty::inputs::*;
use text_info::*;
use defaults::*;

/// A single entry in a menu. It owns the value
/// which will be yielded if this entry is
/// finalised.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MenuEntry<T: Copy> {
    pub name: String,
    pub value: T,
}

impl<T: Copy, S: Into<String>> From<(S, T)> for MenuEntry<T> {
    fn from((s, t): (S, T)) -> Self {
        Self {
            name: s.into(),
            value: t,
        }
    }
}

/// A list of `MenuEntry`s, in the order they appear when rendered,
/// with a description of how the text of the selected and normal
/// (ie. not selected) entries should be rendered.
///
/// `Menu`s (`MenuEntry`s rather) own their value, and remain in
/// scope after a value has been chosen (by running the menu).
/// A copy of a `MenuEntry`'s value is returned by `MenuRunner::run_menu`.
///
/// Note that a `Menu` doesn't contain information about the current
/// selection. When a `Menu` is rendered, all its entries use
/// `normal_info` when rendering. To combine a `Menu` with selection
/// state, use a `MenuInstance`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Menu<T: Copy> {
    pub entries: Vec<MenuEntry<T>>,
    pub size: Size,
    pub selected_info: TextInfo,
    pub normal_info: TextInfo,
}

fn selected_info() -> TextInfo {
    TextInfo::default()
        .bold()
        .foreground_colour(DEFAULT_BG)
        .background_colour(DEFAULT_FG)
}

impl<T: Copy> Menu<T> {
    /// Create a new menu.
    pub fn new<S, V>(mut e: Vec<(S, T)>, size: V) -> Self
        where S: Into<String>,
              V: Into<Size>,
    {
        Self {
            entries: e.drain(..).map(Into::into).collect(),
            size: size.into(),
            normal_info: Default::default(),
            selected_info: selected_info(),
        }
    }

    /// Create a new menu, occupying the smallest amount of
    /// space required to fit all entries.
    pub fn smallest<S>(mut e: Vec<(S, T)>) -> Self
        where S: Into<String>,
    {
        let entries: Vec<MenuEntry<T>> = e.drain(..).map(Into::into).collect();
        let width = entries.iter().fold(0, |acc, e| cmp::max(acc, e.name.len()));
        let height = entries.len();
        Self {
            entries,
            size: Size::new(width as u32, height as u32),
            normal_info: Default::default(),
            selected_info: selected_info(),
        }
    }
}

/// The result of a user interacting with a menu.
pub enum MenuOutput<T> {
    Quit,
    Cancel,
    Finalise(T),
}

/// An instance of a menu, with a selected entry.
/// When a `MenuInstance` is rendered, the
/// currently-selected entry is  rendered using
/// the `Menu`'s `selected_info`.
#[derive(Debug, Clone)]
pub struct MenuInstance<T: Copy> {
    menu: Menu<T>,
    index: usize,
}

impl<T: Copy> MenuInstance<T> {
    /// Create a new `MenuInstance` with the first entry selected.
    /// Returns `None` if the `Menu` has 0 elements.
    pub fn new(menu: Menu<T>) -> Option<Self> {
        Self::with_index(menu, 0)
    }

    /// Create a new `MenuInstance` with the given index selected.
    /// Returns `None` if `index >= menu.entries.len()`.
    pub fn with_index(menu: Menu<T>, index: usize) -> Option<Self> {
        if index < menu.entries.len() {
            Some(Self {
                menu,
                index,
            })
        } else {
            None
        }
    }

    /// Returns the current index
    pub fn index(&self) -> usize {
        self.index
    }

    /// Sets the index, if the specified `index < menu.entries.len()`
    pub fn set_index(&mut self, index: usize) {
        if index < self.menu.entries.len() {
            self.index = index;
        }
    }

    /// Select the entry above the current selection,
    /// unless the first entry is currently selected.
    pub fn up(&mut self) {
        self.index = self.index.saturating_sub(1);
    }

    /// Select the entry below the current selection,
    /// unless the last entry is currently selected.
    pub fn down(&mut self) {
        if self.index < self.menu.entries.len() - 1 {
            self.index += 1;
        }
    }

    /// Returns a copy of the currently selected
    /// entry's value.
    pub fn selected(&self) -> T {
        self.menu.entries[self.index].value
    }

    /// Feed input into the menu instance, possibly
    /// changing the selected entry, cancelling the
    /// menu, attempting to exit the program, or
    /// finalising the selection.
    pub fn tick<I>(&mut self, inputs: I) -> Option<MenuOutput<T>>
        where I: IntoIterator<Item=Input>,
    {
        for input in inputs {
            match input {
                ETX => return Some(MenuOutput::Quit),
                ESCAPE => return Some(MenuOutput::Cancel),
                RETURN => {
                    return Some(MenuOutput::Finalise(self.selected()));
                }
                Input::Up => self.up(),
                Input::Down => self.down(),
                _ => (),

            }
        }
        None
    }
}

/// Default view of a `MenuInstance`.
pub struct DefaultMenuInstanceView;

impl<T: Copy> View<MenuInstance<T>> for DefaultMenuInstanceView {
    fn view<G: ViewGrid>(&self, value: &MenuInstance<T>,
                         offset: Coord, depth: i32, grid: &mut G)
    {
        for (i, entry) in value.menu.entries.iter().enumerate() {
            if i == value.menu.size.y() as usize {
                break;
            }
            let info = if i == value.index {
                &value.menu.selected_info
            } else {
                &value.menu.normal_info
            };
            for (j, ch) in entry.name.chars().enumerate() {
                if j == value.menu.size.x() as usize {
                    break;
                }
                let coord = offset + Coord::new(j as i32, i as i32);
                if let Some(cell) = grid.get_mut(coord, depth) {
                    cell.set_character(ch);
                    info.write_cell(cell);
                }
            }
        }
    }
}

impl<T: Copy> ViewSize<MenuInstance<T>> for DefaultMenuInstanceView {
    fn size(&self, data: &MenuInstance<T>) -> Size {
        data.menu.size
    }
}