-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmod.rs
More file actions
341 lines (288 loc) · 9.2 KB
/
mod.rs
File metadata and controls
341 lines (288 loc) · 9.2 KB
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
use serde::{Deserialize, Serialize};
use ratatui::widgets::{ListItem, Block, Borders, List, ListState};
use ratatui::text::{Line, Span};
use ratatui::style::{Style, Color};
use ratatui::{
layout::{Constraint, Direction, Layout},
Frame,
};
use crossterm::event::{KeyCode, KeyEvent};
use crate::utils;
#[derive(Clone, Debug)]
pub struct LeaderboardItem {
pub title_text: String,
pub task_description: String,
}
impl LeaderboardItem {
pub fn new(title_text: String, task_description: String) -> Self {
Self {
title_text,
task_description,
}
}
}
#[derive(Clone, Debug)]
pub struct GpuItem {
pub title_text: String,
}
impl GpuItem {
pub fn new(title_text: String) -> Self {
Self { title_text }
}
}
#[derive(Clone, Debug)]
pub struct SubmissionModeItem {
pub title_text: String,
pub description_text: String,
pub value: String,
}
impl SubmissionModeItem {
pub fn new(title_text: String, description_text: String, value: String) -> Self {
Self {
title_text,
description_text,
value,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub enum AppState {
#[default]
Welcome,
FileSelection,
LeaderboardSelection,
GpuSelection,
SubmissionModeSelection,
WaitingForResult,
ShowingResult,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SubmissionResultMsg(pub String);
pub trait SelectionItem {
fn title(&self) -> &str;
#[allow(dead_code)]
fn description(&self) -> Option<&str> {
None
}
fn to_list_item(&self, available_width: usize) -> ListItem;
}
pub trait SelectionView<T: SelectionItem + std::clone::Clone> {
fn title(&self) -> String;
fn items(&self) -> &[T];
fn state(&self) -> &ListState;
fn state_mut(&mut self) -> &mut ListState;
fn handle_key_event(&mut self, key: KeyEvent) -> SelectionAction {
match key.code {
KeyCode::Up | KeyCode::Char('k') => {
if let Some(selected) = self.state().selected() {
if selected > 0 {
self.state_mut().select(Some(selected - 1));
}
}
SelectionAction::Handled
}
KeyCode::Down | KeyCode::Char('j') => {
if let Some(selected) = self.state().selected() {
if selected < self.items().len().saturating_sub(1) {
self.state_mut().select(Some(selected + 1));
}
}
SelectionAction::Handled
}
KeyCode::Enter => {
if let Some(selected) = self.state().selected() {
if selected < self.items().len() {
SelectionAction::Selected(selected)
} else {
SelectionAction::Handled
}
} else {
SelectionAction::Handled
}
}
_ => SelectionAction::NotHandled,
}
}
fn render(&mut self, frame: &mut Frame) {
let main_layout = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(0)].as_ref())
.split(frame.size());
let list_area = main_layout[0];
let available_width = list_area.width.saturating_sub(4) as usize;
// Get all the data we need first to avoid borrowing conflicts
let title = self.title().to_string();
let layout_area = main_layout[0];
let items = self.items().to_vec();
let list_items: Vec<ListItem> = items
.iter()
.map(|item| item.to_list_item(available_width))
.collect();
// Create the list widget with orange theme colors
let list = List::new(list_items)
.block(Block::default()
.borders(Borders::ALL)
.title(title.clone())
.border_style(Style::default().fg(Color::Rgb(218, 119, 86))))
.style(Style::default().fg(Color::White))
.highlight_style(Style::default()
.bg(Color::Rgb(218, 119, 86))
.fg(Color::White))
.highlight_symbol("► ");
frame.render_stateful_widget(list, layout_area, self.state_mut());
}
}
#[derive(Debug, PartialEq)]
pub enum SelectionAction {
Handled,
NotHandled,
Selected(usize),
}
impl SelectionItem for LeaderboardItem {
fn title(&self) -> &str {
&self.title_text
}
fn description(&self) -> Option<&str> {
Some(&self.task_description)
}
fn to_list_item(&self, available_width: usize) -> ListItem {
let title_line = Line::from(vec![
Span::styled(self.title_text.clone(), Style::default().fg(Color::White)),
]);
// Wrap long descriptions to fit available width
let max_desc_width = available_width.saturating_sub(2); // Leave some padding
let wrapped_lines = utils::wrap_text(&self.task_description, max_desc_width);
let mut lines = vec![title_line];
for wrapped_line in wrapped_lines {
lines.push(Line::from(vec![
Span::styled(wrapped_line, Style::default().fg(Color::Gray)),
]));
}
ListItem::new(lines)
}
}
impl SelectionItem for GpuItem {
fn title(&self) -> &str {
&self.title_text
}
fn to_list_item(&self, _available_width: usize) -> ListItem {
let title_line = Line::from(vec![
Span::styled(self.title_text.clone(), Style::default().fg(Color::White)),
]);
ListItem::new(vec![title_line])
}
}
impl SelectionItem for SubmissionModeItem {
fn title(&self) -> &str {
&self.title_text
}
fn description(&self) -> Option<&str> {
Some(&self.description_text)
}
fn to_list_item(&self, available_width: usize) -> ListItem {
let title_line = Line::from(vec![
Span::styled(self.title_text.clone(), Style::default().fg(Color::White)),
]);
// Wrap long descriptions to fit available width
let max_desc_width = available_width.saturating_sub(2); // Leave some padding
let wrapped_lines = utils::wrap_text(&self.description_text, max_desc_width);
let mut lines = vec![title_line];
for wrapped_line in wrapped_lines {
lines.push(Line::from(vec![
Span::styled(wrapped_line, Style::default().fg(Color::Gray)),
]));
}
ListItem::new(lines)
}
}
// Selection view implementations for different types
pub struct LeaderboardSelectionView {
leaderboards: Vec<LeaderboardItem>,
leaderboards_state: ListState,
}
impl LeaderboardSelectionView {
pub fn new(leaderboards: Vec<LeaderboardItem>) -> Self {
let mut state = ListState::default();
state.select(Some(0));
Self {
leaderboards,
leaderboards_state: state,
}
}
}
impl SelectionView<LeaderboardItem> for LeaderboardSelectionView {
fn title(&self) -> String {
"Select Leaderboard".to_string()
}
fn items(&self) -> &[LeaderboardItem] {
&self.leaderboards
}
fn state(&self) -> &ListState {
&self.leaderboards_state
}
fn state_mut(&mut self) -> &mut ListState {
&mut self.leaderboards_state
}
}
pub struct GpuSelectionView {
gpus: Vec<GpuItem>,
gpus_state: ListState,
leaderboard_name: String,
}
impl GpuSelectionView {
pub fn new(gpus: Vec<GpuItem>, leaderboard_name: String) -> Self {
let mut state = ListState::default();
state.select(Some(0));
Self {
gpus,
gpus_state: state,
leaderboard_name,
}
}
}
impl SelectionView<GpuItem> for GpuSelectionView {
fn title(&self) -> String {
format!("Select GPU for '{}'", self.leaderboard_name)
}
fn items(&self) -> &[GpuItem] {
&self.gpus
}
fn state(&self) -> &ListState {
&self.gpus_state
}
fn state_mut(&mut self) -> &mut ListState {
&mut self.gpus_state
}
}
pub struct SubmissionModeSelectionView {
submission_modes: Vec<SubmissionModeItem>,
submission_modes_state: ListState,
leaderboard_name: String,
gpu_name: String,
}
impl SubmissionModeSelectionView {
pub fn new(submission_modes: Vec<SubmissionModeItem>, leaderboard_name: String, gpu_name: String) -> Self {
let mut state = ListState::default();
state.select(Some(0));
Self {
submission_modes,
submission_modes_state: state,
leaderboard_name,
gpu_name,
}
}
}
impl SelectionView<SubmissionModeItem> for SubmissionModeSelectionView {
fn title(&self) -> String {
format!("Select Submission Mode for '{}' on '{}'", self.leaderboard_name, self.gpu_name)
}
fn items(&self) -> &[SubmissionModeItem] {
&self.submission_modes
}
fn state(&self) -> &ListState {
&self.submission_modes_state
}
fn state_mut(&mut self) -> &mut ListState {
&mut self.submission_modes_state
}
}