mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-09 05:06:43 +01:00
85 lines
3.3 KiB
Rust
85 lines
3.3 KiB
Rust
use crate::*;
|
|
|
|
#[derive(Clone, PartialEq)]
|
|
pub enum PhrasePoolViewCommand {
|
|
Select(usize),
|
|
Edit(PhrasePoolCommand),
|
|
Rename(PhraseRenameCommand),
|
|
Length(PhraseLengthCommand),
|
|
}
|
|
|
|
impl Handle<Tui> for PhrasePoolView<Tui> {
|
|
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
|
|
PhrasePoolViewCommand::execute_with_state(self, from)
|
|
}
|
|
}
|
|
|
|
impl InputToCommand<Tui, PhrasePoolView<Tui>> for PhrasePoolViewCommand {
|
|
fn input_to_command (state: &PhrasePoolView<Tui>, input: &TuiInput) -> Option<Self> {
|
|
use PhrasePoolViewCommand as Cmd;
|
|
use PhrasePoolCommand as Edit;
|
|
use PhraseRenameCommand as Rename;
|
|
use PhraseLengthCommand as Length;
|
|
match input.event() {
|
|
key!(KeyCode::Up) => Some(Cmd::Select(0)),
|
|
key!(KeyCode::Down) => Some(Cmd::Select(0)),
|
|
key!(KeyCode::Char(',')) => Some(Cmd::Edit(Edit::Swap(0, 0))),
|
|
key!(KeyCode::Char('.')) => Some(Cmd::Edit(Edit::Swap(0, 0))),
|
|
key!(KeyCode::Delete) => Some(Cmd::Edit(Edit::Delete(0))),
|
|
key!(KeyCode::Char('a')) => Some(Cmd::Edit(Edit::Add(0))),
|
|
key!(KeyCode::Char('i')) => Some(Cmd::Edit(Edit::Add(0))),
|
|
key!(KeyCode::Char('d')) => Some(Cmd::Edit(Edit::Duplicate(0))),
|
|
key!(KeyCode::Char('c')) => Some(Cmd::Edit(Edit::RandomColor(0))),
|
|
key!(KeyCode::Char('n')) => Some(Cmd::Rename(Rename::Begin)),
|
|
key!(KeyCode::Char('t')) => Some(Cmd::Length(Length::Begin)),
|
|
_ => match state.mode {
|
|
Some(PhrasePoolMode::Rename(..)) => {
|
|
Rename::input_to_command(state, input).map(Cmd::Rename)
|
|
},
|
|
Some(PhrasePoolMode::Length(..)) => {
|
|
Length::input_to_command(state, input).map(Cmd::Length)
|
|
},
|
|
_ => None
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<E: Engine> Command<PhrasePoolView<E>> for PhrasePoolViewCommand {
|
|
fn execute (self, view: &mut PhrasePoolView<E>) -> Perhaps<Self> {
|
|
use PhraseRenameCommand as Rename;
|
|
use PhraseLengthCommand as Length;
|
|
match self {
|
|
Self::Select(phrase) => {
|
|
view.phrase = phrase
|
|
},
|
|
Self::Edit(command) => {
|
|
return Ok(command.execute(&mut view.state)?.map(Self::Edit))
|
|
}
|
|
Self::Rename(command) => match command {
|
|
Rename::Begin => {
|
|
view.mode = Some(PhrasePoolMode::Rename(
|
|
view.phrase,
|
|
view.state.phrases[view.phrase].read().unwrap().name.clone()
|
|
))
|
|
},
|
|
_ => {
|
|
return Ok(command.execute(view)?.map(Self::Rename))
|
|
}
|
|
},
|
|
Self::Length(command) => match command {
|
|
Length::Begin => {
|
|
view.mode = Some(PhrasePoolMode::Length(
|
|
view.phrase,
|
|
view.state.phrases[view.phrase].read().unwrap().length,
|
|
PhraseLengthFocus::Bar
|
|
))
|
|
},
|
|
_ => {
|
|
return Ok(command.execute(view)?.map(Self::Length))
|
|
}
|
|
},
|
|
}
|
|
Ok(None)
|
|
}
|
|
}
|