Skip to content

Commit e43d153

Browse files
committed
refactor ui section
1 parent a810159 commit e43d153

5 files changed

Lines changed: 114 additions & 181 deletions

File tree

src/handler.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ pub async fn handle_key_events(
5555
FocusedBlock::Preview => {
5656
app.history.preview.scroll = app.history.preview.scroll.saturating_add(1);
5757
}
58-
FocusedBlock::Help => {
59-
app.help.scroll_down();
60-
}
6158
_ => (),
6259
},
6360

@@ -76,10 +73,6 @@ pub async fn handle_key_events(
7673
app.chat.scroll = app.chat.scroll.saturating_sub(1);
7774
}
7875

79-
FocusedBlock::Help => {
80-
app.help.scroll_up();
81-
}
82-
8376
_ => (),
8477
},
8578

@@ -172,20 +165,16 @@ pub async fn handle_key_events(
172165
app.chat
173166
.automatic_scroll
174167
.store(true, std::sync::atomic::Ordering::Relaxed);
175-
176-
app.prompt.update(&app.focused_block);
177168
}
178169
FocusedBlock::Prompt => {
179170
app.chat.move_to_bottom();
180171

181172
app.focused_block = FocusedBlock::Chat;
182173
app.prompt.mode = Mode::Normal;
183-
app.prompt.update(&app.focused_block);
184174
}
185175
FocusedBlock::History => {
186176
app.focused_block = FocusedBlock::Preview;
187177
app.history.preview.scroll = 0;
188-
app.prompt.update(&app.focused_block);
189178
}
190179
FocusedBlock::Preview => {
191180
app.focused_block = FocusedBlock::History;
@@ -199,7 +188,6 @@ pub async fn handle_key_events(
199188
if c == app.config.key_bindings.show_help && app.prompt.mode != Mode::Insert =>
200189
{
201190
app.focused_block = FocusedBlock::Help;
202-
app.prompt.update(&app.focused_block);
203191
app.chat
204192
.automatic_scroll
205193
.store(true, std::sync::atomic::Ordering::Relaxed);
@@ -212,7 +200,6 @@ pub async fn handle_key_events(
212200
&& key_event.modifiers == KeyModifiers::CONTROL =>
213201
{
214202
app.focused_block = FocusedBlock::History;
215-
app.prompt.update(&app.focused_block);
216203
app.chat
217204
.automatic_scroll
218205
.store(true, std::sync::atomic::Ordering::Relaxed);

src/help.rs

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
11
use ratatui::{
2-
layout::{Alignment, Constraint, Rect},
3-
style::{Style, Stylize},
4-
widgets::{Block, Borders, Clear, Padding, Row, Table, TableState},
2+
layout::{Alignment, Constraint, Direction, Layout},
3+
style::{Color, Style, Stylize},
4+
widgets::{Block, BorderType, Borders, Cell, Clear, Padding, Row, Table},
55
Frame,
66
};
77

88
pub struct Help {
99
block_height: usize,
10-
state: TableState,
11-
keys: &'static [(&'static str, &'static str)],
10+
keys: Vec<(Cell<'static>, &'static str)>,
1211
}
1312

1413
impl Default for Help {
1514
fn default() -> Self {
16-
let mut state = TableState::new().with_offset(0);
17-
state.select(Some(0));
18-
1915
Self {
2016
block_height: 0,
21-
state,
22-
keys: &[
23-
("Esc", "Switch to Normal mode / Dismiss pop-up"),
24-
("Tab", "Switch the focus"),
17+
keys: vec![
18+
(
19+
Cell::from("Esc").bold().yellow(),
20+
"Switch to Normal mode / Dismiss pop-up",
21+
),
22+
(Cell::from("Tab").bold().yellow(), "Switch the focus"),
2523
(
26-
"ctrl + n",
24+
Cell::from("ctrl + n").bold().yellow(),
2725
"Start new chat and save the previous one to the history",
2826
),
29-
("ctrl + s", "Save the chat to file in the current directory"),
30-
("ctrl + h", "Show history"),
31-
("ctrl + t", "Stop the stream response"),
32-
("j or Down", "Scroll down"),
33-
("k or Up", "Scroll up"),
34-
("G", "Go to the end"),
35-
("gg", "Go to the top"),
36-
("?", "Show help"),
27+
(
28+
Cell::from("ctrl + s").bold().yellow(),
29+
"Save the chat to file in the current directory",
30+
),
31+
(Cell::from("ctrl + h").bold().yellow(), "Show history"),
32+
(
33+
Cell::from("ctrl + t").bold().yellow(),
34+
"Stop the stream response",
35+
),
36+
(Cell::from("j or Down").bold().yellow(), "Scroll down"),
37+
(Cell::from("k or Up").bold().yellow(), "Scroll up"),
38+
(Cell::from("G").bold().yellow(), "Go to the end"),
39+
(Cell::from("gg").bold().yellow(), "Go to the top"),
40+
(Cell::from("?").bold().yellow(), "Show help"),
3741
],
3842
}
3943
}
@@ -44,56 +48,51 @@ impl Help {
4448
Self::default()
4549
}
4650

47-
pub fn scroll_down(&mut self) {
48-
let i = match self.state.selected() {
49-
Some(i) => {
50-
if i >= self.keys.len().saturating_sub(self.block_height - 6) {
51-
i
52-
} else {
53-
i + 1
54-
}
55-
}
56-
None => 1,
57-
};
58-
*self.state.offset_mut() = i;
59-
self.state.select(Some(i));
60-
}
61-
pub fn scroll_up(&mut self) {
62-
let i = match self.state.selected() {
63-
Some(i) => {
64-
if i > 1 {
65-
i - 1
66-
} else {
67-
0
68-
}
69-
}
70-
None => 1,
71-
};
72-
*self.state.offset_mut() = i;
73-
self.state.select(Some(i));
74-
}
51+
pub fn render(&mut self, frame: &mut Frame) {
52+
let layout = Layout::default()
53+
.direction(Direction::Vertical)
54+
.constraints([
55+
Constraint::Fill(1),
56+
Constraint::Length(15),
57+
Constraint::Fill(1),
58+
])
59+
.flex(ratatui::layout::Flex::SpaceBetween)
60+
.split(frame.area());
61+
62+
let block = Layout::default()
63+
.direction(Direction::Horizontal)
64+
.constraints([
65+
Constraint::Fill(1),
66+
Constraint::Min(75),
67+
Constraint::Fill(1),
68+
])
69+
.flex(ratatui::layout::Flex::SpaceBetween)
70+
.split(layout[1])[1];
7571

76-
pub fn render(&mut self, frame: &mut Frame, block: Rect) {
7772
self.block_height = block.height as usize;
78-
let widths = [Constraint::Length(15), Constraint::Min(60)];
73+
let widths = [Constraint::Length(12), Constraint::Fill(1)];
7974
let rows: Vec<Row> = self
8075
.keys
8176
.iter()
82-
.map(|key| Row::new(vec![key.0, key.1]))
77+
.map(|key| {
78+
Row::new(vec![key.0.to_owned(), key.1.into()])
79+
.style(Style::default().fg(Color::White))
80+
})
8381
.collect();
8482

8583
let table = Table::new(rows, widths).block(
8684
Block::default()
87-
.padding(Padding::uniform(2))
85+
.padding(Padding::uniform(1))
8886
.title(" Help ")
89-
.title_style(Style::default().bold())
87+
.title_style(Style::default().bold().fg(Color::Green))
9088
.title_alignment(Alignment::Center)
9189
.borders(Borders::ALL)
9290
.style(Style::default())
93-
.border_style(Style::default()),
91+
.border_type(BorderType::Thick)
92+
.border_style(Style::default().fg(Color::Green)),
9493
);
9594

9695
frame.render_widget(Clear, block);
97-
frame.render_stateful_widget(table, block, &mut self.state);
96+
frame.render_widget(table, block);
9897
}
9998
}

src/history.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use tokio::sync::mpsc::UnboundedSender;
22

33
use ratatui::{
4-
layout::{Alignment, Constraint, Direction, Layout, Rect},
4+
layout::{Alignment, Constraint, Direction, Layout},
55
style::{Color, Style, Stylize},
66
text::Text,
77
widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap},
@@ -22,7 +22,6 @@ pub struct Preview<'a> {
2222

2323
#[derive(Debug, Default, Clone)]
2424
pub struct History<'a> {
25-
block_height: usize,
2625
state: ListState,
2726
pub text: Vec<Vec<String>>,
2827
pub preview: Preview<'a>,
@@ -31,7 +30,6 @@ pub struct History<'a> {
3130
impl History<'_> {
3231
pub fn new() -> Self {
3332
Self {
34-
block_height: 0,
3533
state: ListState::default(),
3634
text: Vec::new(),
3735
preview: Preview::default(),
@@ -106,8 +104,24 @@ impl History<'_> {
106104
}
107105
}
108106

109-
pub fn render(&mut self, frame: &mut Frame, area: Rect, focused_block: FocusedBlock) {
110-
self.block_height = area.height as usize;
107+
pub fn render(&mut self, frame: &mut Frame, focused_block: &FocusedBlock) {
108+
let layout = Layout::default()
109+
.direction(Direction::Vertical)
110+
.constraints([
111+
Constraint::Fill(1),
112+
Constraint::Percentage(90),
113+
Constraint::Fill(1),
114+
])
115+
.split(frame.area())[1];
116+
117+
let block = Layout::default()
118+
.direction(Direction::Horizontal)
119+
.constraints([
120+
Constraint::Fill(1),
121+
Constraint::Percentage(90),
122+
Constraint::Fill(1),
123+
])
124+
.split(layout)[1];
111125

112126
if !self.text.is_empty() && self.state.selected().is_none() {
113127
*self.state.offset_mut() = 0;
@@ -117,8 +131,8 @@ impl History<'_> {
117131
let (history_block, preview_block) = {
118132
let chunks = Layout::default()
119133
.direction(Direction::Horizontal)
120-
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
121-
.split(area);
134+
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
135+
.split(block);
122136
(chunks[0], chunks[1])
123137
};
124138

@@ -171,7 +185,7 @@ impl History<'_> {
171185
}),
172186
);
173187

174-
frame.render_widget(Clear, area);
188+
frame.render_widget(Clear, block);
175189
frame.render_widget(preview, preview_block);
176190
frame.render_stateful_widget(list, history_block, &mut self.state);
177191
}

0 commit comments

Comments
 (0)