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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
// This file is part of Sydney <https://github.com/gemrest/sydney>.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
use std::time::{Duration, Instant};
use crossterm::event;
use germ::{ast::Node, request::Status};
use url::Url;
use crate::{input::Mode as InputMode, stateful_list::StatefulList};
pub struct App {
pub items: StatefulList<(Vec<Node>, Option<String>, bool)>,
pub input: String,
pub input_mode: InputMode,
pub command_stroke_history: Vec<event::KeyCode>,
pub command_history: Vec<String>,
pub command_history_cursor: usize,
pub error: Option<String>,
pub url: Url,
pub capsule_history: Vec<Url>,
pub previous_capsule: Option<Url>,
pub response_input: String,
pub accept_response_input: bool,
pub response_input_text: String,
pub wrap_at: u16,
}
impl App {
pub fn new() -> Self {
let url = Url::parse("gemini://fuwn.me/blog/technology/gemini?referrer=sydney").unwrap();
let mut app = Self {
response_input: String::new(),
error: None,
command_stroke_history: Vec::new(),
input: String::new(),
input_mode: InputMode::Normal,
items: StatefulList::with_items(Vec::new()),
command_history: vec![],
command_history_cursor: 0,
url,
capsule_history: vec![],
previous_capsule: None,
accept_response_input: false,
response_input_text: "".to_string(),
wrap_at: crossterm::terminal::size().unwrap_or((80, 24)).0,
};
app.make_request();
app
}
pub fn set_url(&mut self, url: Url) {
self.previous_capsule = Some(self.url.clone());
self.url = url;
}
pub fn make_request(&mut self) {
self.items = StatefulList::with_items({
let mut items: Vec<(Vec<Node>, Option<String>, bool)> = vec![];
match germ::request::request(&self.url) {
Ok(mut response) => {
if response.status() == &Status::TemporaryRedirect
|| response.status() == &Status::PermanentRedirect
{
self.url = Url::parse(&if response.meta().starts_with('/') {
format!(
"gemini://{}{}",
self.url.host_str().unwrap(),
response.meta()
)
} else if response.meta().starts_with("gemini://") {
response.meta().to_string()
} else if !response.meta().starts_with('/')
&& !response.meta().starts_with("gemini://")
{
format!(
"{}/{}",
self.url.to_string().trim_end_matches('/'),
response.meta()
)
} else {
self.url.to_string()
})
.unwrap();
response = germ::request::request(&self.url).unwrap();
}
if response.status() == &Status::Input
|| response.status() == &Status::SensitiveInput
{
self.accept_response_input = true;
self.response_input_text = response.meta().to_string();
items = self.items.items.clone();
}
// items.push((
// vec![Node::Text(response.meta().to_string())],
// None,
// false,
// ));
// items.push((vec![Node::Text("".to_string())], None, false));
let mut pre = false;
if let Some(content) = response.content().clone() {
let real_lines = content.lines();
for line in real_lines {
let line = line.replace('\t', " ");
let pre_like = if line.starts_with("```") {
pre = !pre;
true
} else {
false
};
let ast = germ::ast::Ast::from_string(&line);
let ast_node = ast.inner().first().map_or_else(
|| {
if pre_like || pre {
if line == "```" {
Node::Text("sydney_abc_123".to_string())
} else {
Node::Text(line.get(3..).unwrap_or("").to_string())
}
} else {
Node::Whitespace
}
},
Clone::clone,
);
let mut parts = line.split_whitespace();
if let (Some("=>"), Some(to)) = (parts.next(), parts.next()) {
items.push((vec![ast_node], Some(to.to_string()), false));
} else {
items.push((vec![ast_node], None, pre));
}
}
} else if response.status() != &Status::Input
&& response.status() != &Status::SensitiveInput
{
self.error = Some(response.meta().to_string());
}
if let Some(last_url) = self.capsule_history.last() {
if last_url.to_string() != self.url.to_string() {
self.capsule_history.push(self.url.clone());
}
} else {
self.capsule_history.push(self.url.clone());
}
}
Err(error) => {
self.error = Some(error.to_string());
return;
}
}
items
});
}
pub fn run<B: ratatui::backend::Backend>(
terminal: &mut ratatui::Terminal<B>,
mut app: Self,
tick_rate: Duration,
) -> std::io::Result<()> {
let mut last_tick = Instant::now();
loop {
terminal.draw(|f| crate::ui::ui(f, &mut app))?;
let timeout = tick_rate
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
if event::poll(timeout)? {
if let event::Event::Key(key) = event::read()? {
if crate::input::handle_key_strokes(&mut app, key) {
return Ok(());
}
}
}
if last_tick.elapsed() >= tick_rate {
last_tick = Instant::now();
}
}
}
pub fn go_back(&mut self) {
if let Some(url) = self.capsule_history.pop() {
if url == self.url {
if let Some(url) = self.capsule_history.pop() {
self.set_url(url);
}
} else {
self.set_url(url);
}
self.make_request();
}
}
}
|