馃 Vim-like, Command-line Gemini Client
gemini
gemini-protocol
tui
smolweb
vim
1// This file is part of Sydney <https://github.com/gemrest/sydney>.
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, version 3.
6//
7// This program is distributed in the hope that it will be useful, but
8// WITHOUT ANY WARRANTY; without even the implied warranty of
9// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10// General Public License for more details.
11//
12// You should have received a copy of the GNU General Public License
13// along with this program. If not, see <http://www.gnu.org/licenses/>.
14//
15// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
16// SPDX-License-Identifier: GPL-3.0-only
17
18pub enum Command {
19 Quit,
20 Open(Option<String>),
21 Unknown,
22 Wrap(u16, Option<String>),
23 Help,
24}
25impl From<String> for Command {
26 fn from(s: String) -> Self {
27 let mut tokens = s.split(' ');
28
29 match tokens.next() {
30 Some("open" | "o") => Self::Open(tokens.next().map(ToString::to_string)),
31 Some("quit" | "q") => Self::Quit,
32 Some("wrap") =>
33 tokens.next().map_or_else(
34 || {
35 Self::Wrap(
36 80,
37 Some("Missing width argument to wrap command".to_string()),
38 )
39 },
40 |at| {
41 match at.parse() {
42 Ok(at_parsed) =>
43 Self::Wrap(
44 if at_parsed == 0 {
45 crossterm::terminal::size().unwrap_or((80, 24)).0
46 } else {
47 at_parsed
48 },
49 None,
50 ),
51 Err(error) => Self::Wrap(80, Some(error.to_string())),
52 }
53 },
54 ),
55 Some("help" | "h") => Self::Help,
56 _ => Self::Unknown,
57 }
58 }
59}