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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
use super::{
article_view, async_view, comment_view, help_view::HasHelpView, text_view, traits::*, utils,
};
use crate::client::StoryNumericFilters;
use crate::prelude::*;
static STORY_TAGS: [&str; 5] = ["front_page", "story", "ask_hn", "show_hn", "job"];
/// StoryView is a View displaying a list stories corresponding
/// to a particular category (top stories, newest stories, most popular stories, etc).
pub struct StoryView {
pub stories: Vec<client::Story>,
view: ScrollView<LinearLayout>,
raw_command: String,
}
impl ViewWrapper for StoryView {
wrap_impl!(self.view: ScrollView<LinearLayout>);
}
impl StoryView {
pub fn new(stories: Vec<client::Story>, starting_id: usize) -> Self {
StoryView {
view: Self::construct_story_view(&stories, starting_id),
stories,
raw_command: String::new(),
}
}
fn construct_story_view(
stories: &[client::Story],
starting_id: usize,
) -> ScrollView<LinearLayout> {
// Determine the maximum length of a story's ID string.
// This maximum length is used to align the display of the story IDs.
let max_id_len = {
let max_id = starting_id + stories.len() + 1;
let mut width = 0;
let mut pw = 1;
while pw <= max_id {
pw *= 10;
width += 1;
}
width
};
LinearLayout::vertical()
.with(|s| {
stories.iter().enumerate().for_each(|(i, story)| {
let mut story_text = StyledString::styled(
format!("{1:>0$}. ", max_id_len, starting_id + i + 1),
config::get_config_theme().component_style.metadata,
);
story_text.append(Self::get_story_text(max_id_len, story));
s.add_child(text_view::TextView::new(story_text));
})
})
.scrollable()
}
/// Get the text summarizing basic information about a story
fn get_story_text(max_id_len: usize, story: &client::Story) -> StyledString {
let mut story_text = story.title.clone();
if let Ok(url) = url::Url::parse(&story.url) {
if let Some(domain) = url.domain() {
story_text.append_styled(
format!(" ({})", domain),
config::get_config_theme().component_style.link,
);
}
}
story_text.append_plain("\n");
story_text.append_styled(
// left-align the story's metadata by `max_id_len+2`
// which is the width of the string "{max_story_id}. "
format!(
"{:width$}{} points | by {} | {} ago | {} comments",
" ",
story.points,
story.author,
crate::utils::get_elapsed_time_as_text(story.time),
story.num_comments,
width = max_id_len + 2,
),
config::get_config_theme().component_style.metadata,
);
story_text
}
inner_getters!(self.view: ScrollView<LinearLayout>);
}
impl ListViewContainer for StoryView {
fn get_inner_list(&self) -> &LinearLayout {
self.get_inner().get_inner()
}
fn get_inner_list_mut(&mut self) -> &mut LinearLayout {
self.get_inner_mut().get_inner_mut()
}
fn on_set_focus_index(&mut self, old_id: usize, new_id: usize) {
let direction = old_id <= new_id;
// enable auto-scrolling when changing the focused index of the view
self.scroll(direction);
}
}
impl ScrollViewContainer for StoryView {
type ScrollInner = LinearLayout;
fn get_inner_scroll_view(&self) -> &ScrollView<LinearLayout> {
self.get_inner()
}
fn get_inner_scroll_view_mut(&mut self) -> &mut ScrollView<LinearLayout> {
self.get_inner_mut()
}
}
pub fn get_story_main_view(
stories: Vec<client::Story>,
client: &'static client::HNClient,
starting_id: usize,
) -> OnEventView<StoryView> {
let is_suffix_key =
|c: &Event| -> bool { config::get_story_view_keymap().goto_story.has_event(c) };
let story_view_keymap = config::get_story_view_keymap().clone();
OnEventView::new(StoryView::new(stories, starting_id))
// number parsing
.on_pre_event_inner(EventTrigger::from_fn(|_| true), move |s, e| {
match *e {
Event::Char(c) if ('0'..='9').contains(&c) => {
s.raw_command.push(c);
}
_ => {
if !is_suffix_key(e) {
s.raw_command.clear();
}
}
};
// don't allow the inner `LinearLayout` child view to handle the event
// because of its pre-defined `on_event` function
Some(EventResult::Ignored)
})
// story navigation shortcuts
.on_pre_event_inner(story_view_keymap.prev_story, |s, _| {
let id = s.get_focus_index();
if id == 0 {
None
} else {
s.set_focus_index(id - 1)
}
})
.on_pre_event_inner(story_view_keymap.next_story, |s, _| {
let id = s.get_focus_index();
s.set_focus_index(id + 1)
})
.on_pre_event_inner(story_view_keymap.goto_story_comment_view, move |s, _| {
let id = s.get_focus_index();
// the story struct hasn't had any comments inside yet,
// so it can be cloned without greatly affecting performance
let story = s.stories[id].clone();
Some(EventResult::with_cb({
move |s| comment_view::add_comment_view_layer(s, client, &story, false)
}))
})
// open external link shortcuts
.on_pre_event_inner(story_view_keymap.open_article_in_browser, move |s, _| {
let id = s.get_focus_index();
utils::open_url_in_browser(s.stories[id].get_url().as_ref());
Some(EventResult::Consumed(None))
})
.on_pre_event_inner(
story_view_keymap.open_article_in_article_view,
move |s, _| {
let id = s.get_focus_index();
let url = s.stories[id].url.clone();
if !url.is_empty() {
Some(EventResult::with_cb({
move |s| article_view::add_article_view_layer(s, &url)
}))
} else {
Some(EventResult::Consumed(None))
}
},
)
.on_pre_event_inner(story_view_keymap.open_story_in_browser, move |s, _| {
let url = s.stories[s.get_focus_index()].story_url();
utils::open_url_in_browser(&url);
Some(EventResult::Consumed(None))
})
.on_pre_event_inner(story_view_keymap.goto_story, move |s, _| {
match s.raw_command.parse::<usize>() {
Ok(number) => {
s.raw_command.clear();
if number < starting_id + 1 {
return None;
}
let number = number - 1 - starting_id;
if number < s.len() {
s.set_focus_index(number).unwrap();
Some(EventResult::Consumed(None))
} else {
None
}
}
Err(_) => None,
}
})
.on_scroll_events()
}
fn get_story_view_title_bar(tag: &'static str) -> impl View {
let style = config::get_config_theme().component_style.title_bar;
let mut title = StyledString::styled(
"[Y]",
Style::from(style).combine(ColorStyle::front(
config::get_config_theme().palette.light_white,
)),
);
title.append_styled(" Hacker News", style);
for (i, item) in
|