summaryrefslogtreecommitdiffstats
path: root/src/client/input.rs
blob: 0c9d99dc637e39ba9e6aa1ff332d736290e05b0b (plain) (blame)
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
//! Terminal input for the adapter: the command-line editor and key mapping.
//!
//! `ClientCore` knows nothing about keys or the prompt; this module turns
//! keystrokes into `Command`s or a submitted line.

use crate::client::games::roulette::Direction;
use crate::client::ui::{
    command_names, completion_lines, display_width, input_cursor_column, record_history,
};
use crate::client::view::Command;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Key {
    Char(char),
    Enter,
    Backspace,
    Up,
    Down,
    Left,
    Right,
    Tab,
    Esc,
    CtrlC,
}

/// What the prompt wants the adapter to do after a key.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Input {
    /// Nothing changed.
    None,
    /// Redraw the prompt.
    Prompt,
    /// Run this command line.
    Submit(String),
    /// Print lines above the prompt.
    Print(Vec<String>),
    Quit,
}

/// What a key means inside a running game.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GameInput {
    None,
    Quit,
    Command(Command),
}

/// The prompt buffer, its history and its cursor.
pub struct Editor {
    input: String,
    history: Vec<String>,
    history_index: usize,
}

impl Default for Editor {
    fn default() -> Self {
        Self::new()
    }
}

impl Editor {
    pub fn new() -> Self {
        Self {
            input: String::new(),
            history: Vec::new(),
            history_index: 0,
        }
    }

    pub fn input(&self) -> &str {
        &self.input
    }

    pub fn clear(&mut self) {
        self.input.clear();
    }

    /// Column of the cursor after the `> ` prompt.
    pub fn cursor_column(&self) -> u16 {
        input_cursor_column(2, display_width(&self.input))
    }

    pub fn handle(&mut self, key: Key) -> Input {
        match key {
            Key::CtrlC => Input::Quit,
            Key::Enter => {
                let line = self.input.trim().to_string();
                if line.is_empty() {
                    return Input::None;
                }
                record_history(&mut self.history, &line);
                self.history_index = self.history.len();
                self.input.clear();
                Input::Submit(line)
            }
            Key::Backspace => {
                self.input.pop();
                Input::Prompt
            }
            Key::Char(c) => {
                self.input.push(c);
                Input::Prompt
            }
            Key::Up => {
                if self.history_index > 0 {
                    self.history_index -= 1;
                    self.input = self.history[self.history_index].clone();
                }
                Input::Prompt
            }
            Key::Down => {
                if self.history_index < self.history.len() {
                    self.history_index += 1;
                    self.input = if self.history_index == self.history.len() {
                        String::new()
                    } else {
                        self.history[self.history_index].clone()
                    };
                }
                Input::Prompt
            }
            Key::Tab => {
                let prefix = self.input.trim();
                let matches: Vec<&'static str> = command_names()
                    .into_iter()
                    .filter(|command| command.starts_with(prefix))
                    .collect();
                match matches.len() {
                    1 => {
                        self.input = matches[0].to_string();
                        Input::Prompt
                    }
                    0 => Input::None,
                    _ => Input::Print(completion_lines(&matches, 76)),
                }
            }
            // Esc, Left and Right do nothing on the prompt line.
            Key::Esc | Key::Left | Key::Right => Input::None,
        }
    }
}

/// Map a key to a command inside `game`.
///
/// Invalid combinations simply produce a command the core ignores (for
/// example `h` when it is not the player's turn), so this stays stateless.
pub fn game_input(game: &str, key: Key) -> GameInput {
    match key {
        Key::CtrlC => GameInput::Quit,
        Key::Char('q') => GameInput::Command(Command::Leave),
        Key::Char('+') | Key::Char('=') => GameInput::Command(Command::AdjustBet(10)),
        Key::Char('-') => GameInput::Command(Command::AdjustBet(-10)),
        _ => match game {
            "roulette" => match key {
                Key::Up => GameInput::Command(Command::MoveCursor(Direction::Up)),
                Key::Down => GameInput::Command(Command::MoveCursor(Direction::Down)),
                Key::Left => GameInput::Command(Command::MoveCursor(Direction::Left)),
                Key::Right => GameInput::Command(Command::MoveCursor(Direction::Right)),
                Key::Enter => GameInput::Command(Command::PlaceBet),
                Key::Char(' ') => GameInput::Command(Command::Spin),
                Key::Char('c') => GameInput::Command(Command::ClearBets),
                _ => GameInput::None,
            },
            "blackjack" => match key {
                Key::Char(' ') => GameInput::Command(Command::Deal),
                Key::Char('h') => GameInput::Command(Command::Hit),
                Key::Char('s') => GameInput::Command(Command::Stand),
                Key::Char('d') => GameInput::Command(Command::Double),
                _ => GameInput::None,
            },
            "crash" => match key {
                Key::Char(' ') => GameInput::Command(Command::Start),
                Key::Char('c') => GameInput::Command(Command::CashOut),
                _ => GameInput::None,
            },
            _ => match key {
                Key::Char(' ') => GameInput::Command(Command::Spin),
                Key::Esc => GameInput::Command(Command::Leave),
                _ => GameInput::None,
            },
        },
    }
}