summaryrefslogtreecommitdiffstats
path: root/src/client/ui.rs
blob: 6fc2f62bf1db9a4edcef6969561a7d3a49228ac3 (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
//! Pure text/history helpers for the client TUI.
//!
//! Nothing here touches the terminal, so it is unit-tested from `tests/`
//! without a PTY.

struct CommandHelp {
    name: &'static str,
    usage: &'static str,
    description: &'static str,
}

const COMMANDS: &[CommandHelp] = &[
    CommandHelp {
        name: "connect",
        usage: "connect <ip>:<port>",
        description: "connect to the server",
    },
    CommandHelp {
        name: "send",
        usage: "send <message>",
        description: "send a message to the server",
    },
    CommandHelp {
        name: "disconnect",
        usage: "disconnect",
        description: "disconnect from the server",
    },
    CommandHelp {
        name: "ping",
        usage: "ping",
        description: "ping the server",
    },
    CommandHelp {
        name: "login",
        usage: "login",
        description: "login to the server",
    },
    CommandHelp {
        name: "register",
        usage: "register",
        description: "register a new user",
    },
    CommandHelp {
        name: "setcred",
        usage: "setcred",
        description: "set credentials for a host",
    },
    CommandHelp {
        name: "logout",
        usage: "logout",
        description: "logout from the server",
    },
    CommandHelp {
        name: "info",
        usage: "info",
        description: "show account and connection info",
    },
    CommandHelp {
        name: "play",
        usage: "play <game> [bet]",
        description: "start a game (e.g. play slots 50)",
    },
    CommandHelp {
        name: "games",
        usage: "games",
        description: "list available games",
    },
    CommandHelp {
        name: "help",
        usage: "help",
        description: "show this message",
    },
    CommandHelp {
        name: "forget-host",
        usage: "forget-host [ip:port]",
        description: "clear saved trust fingerprint and credentials",
    },
    CommandHelp {
        name: "hosts",
        usage: "hosts",
        description: "list remembered hosts",
    },
    CommandHelp {
        name: "quit",
        usage: "quit",
        description: "quit the program",
    },
];

pub fn command_names() -> Vec<&'static str> {
    COMMANDS.iter().map(|command| command.name).collect()
}

pub fn help_lines() -> Vec<String> {
    let mut lines = vec!["Available commands:".to_string()];
    for command in COMMANDS {
        lines.push(format!(
            "  {:<28} - {}",
            command.usage, command.description
        ));
    }
    lines
}

/// Wrap a completion list to `width` columns, one space between names.
pub fn completion_lines(commands: &[&str], width: usize) -> Vec<String> {
    let mut lines = vec!["Possible completions:".to_string()];
    let mut current = String::new();

    for command in commands {
        let needed = if current.is_empty() {
            command.len()
        } else {
            current.len() + 1 + command.len()
        };
        if !current.is_empty() && needed > width {
            lines.push(current.clone());
            current.clear();
        }
        if !current.is_empty() {
            current.push(' ');
        }
        current.push_str(command);
    }

    if !current.is_empty() {
        lines.push(current);
    }
    lines
}

/// Add a command to the history, skipping blanks and consecutive duplicates.
pub fn record_history(history: &mut Vec<String>, line: &str) {
    let line = line.trim();
    if line.is_empty() {
        return;
    }
    if history.last().is_some_and(|last| last == line) {
        return;
    }
    history.push(line.to_string());
}

/// Parse the optional bet argument of `play <game> [bet]`.
pub fn parse_bet_arg(raw: &str) -> Result<u64, String> {
    let bet = raw
        .trim()
        .parse::<u64>()
        .map_err(|_| format!("Invalid bet: {raw}"))?;
    if bet < 10 {
        return Err("Bet must be at least 10 PLN".to_string());
    }
    Ok(bet)
}

/// Lines to move the cursor up to reach the first line of a region that ends
/// on the current line. `render()` has no trailing newline, so a 7-line board
/// needs `MoveUp(6)`.
pub fn lines_to_move_up(previous_lines: usize) -> u16 {
    previous_lines
        .saturating_sub(1)
        .min(u16::MAX as usize) as u16
}

/// Column (0-based) to move to after the given prompt and input text.
pub fn input_cursor_column(prompt_len: usize, input_len: usize) -> u16 {
    prompt_len
        .saturating_add(input_len)
        .min(u16::MAX as usize) as u16
}