Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ impl<'a> App<'a> {

fn render<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> Result<()> {
terminal.draw(|f| {
let current_width = f.area().width;
let global_area = f.area();
let current_width = global_area.width;
if !is_line_width_sufficient(current_width) {
f.render_widget(
Paragraph::new(utils::split_to_lines(
Expand All @@ -300,7 +301,7 @@ impl<'a> App<'a> {
(current_width - 2) as usize,
))
.alignment(Alignment::Left),
f.area(),
global_area,
);
return;
}
Expand All @@ -313,7 +314,7 @@ impl<'a> App<'a> {
Constraint::Max(textarea_len),
Constraint::Length(1),
])
.split(f.area());
.split(global_area);

if layout[0].width as usize != self.app_state.last_known_width
|| layout[0].height as usize != self.app_state.last_known_height
Expand Down Expand Up @@ -346,17 +347,17 @@ impl<'a> App<'a> {
}

self.help_screen
.render(f, utils::popup_area(f.area(), 40, 30));
.render(f, utils::popup_area(global_area, 40, 30));

self.models_screen
.render(f, utils::popup_area(f.area(), 40, 60));
.render(f, utils::popup_area(global_area, 40, 60));

self.edit_screen
.render(f, utils::popup_area(f.area(), 70, 90));
.render(f, utils::popup_area(global_area, 70, 90));
self.history_screen
.render(f, utils::popup_area(f.area(), 70, 90));
.render(f, utils::popup_area(global_area, 70, 90));

self.notice.render(f, utils::notice_area(f.area(), 30));
self.notice.render(f, utils::notice_area(global_area, 30));
})?;
Ok(())
}
Expand Down
14 changes: 8 additions & 6 deletions src/app/ui/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tokio::sync::mpsc;
use tui_textarea::Key;
use unicode_width::UnicodeWidthStr;

use super::utils;
use super::{Dim, utils};

pub struct EditScreen<'a> {
action_tx: mpsc::UnboundedSender<Action>,
Expand Down Expand Up @@ -86,11 +86,13 @@ impl<'a> EditScreen<'_> {
[..10].iter().for_each(|_| self.list_state.select_next());
}

pub fn render(&mut self, frame: &mut Frame, area: Rect) {
pub fn render(&mut self, f: &mut Frame, area: Rect) {
if !self.showing {
return;
}

f.dim_bg();

let instructions = vec![
span!(" "),
span!("q").green().bold(),
Expand All @@ -113,16 +115,16 @@ impl<'a> EditScreen<'_> {
.title_bottom(Line::from(instructions))
.style(Style::default());

frame.render_widget(Clear, area);
f.render_widget(Clear, area);
let inner = block.inner(area);
frame.render_widget(block, area);
f.render_widget(block, area);

let layout = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
.split(inner);
self.render_messages_panel(frame, layout[0]);
self.render_preview_panel(frame, layout[1]);
self.render_messages_panel(f, layout[0]);
self.render_preview_panel(f, layout[1]);
}

fn render_messages_panel(&mut self, frame: &mut Frame, area: Rect) {
Expand Down
10 changes: 6 additions & 4 deletions src/app/ui/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use ratatui::{
};
use tui_textarea::Key;

use super::utils;
use super::{Dim, utils};

const ROW_HEIGHT: usize = 1;

Expand Down Expand Up @@ -88,11 +88,13 @@ impl HelpScreen<'_> {
false
}

pub fn render(&mut self, frame: &mut ratatui::Frame, area: ratatui::layout::Rect) {
pub fn render(&mut self, f: &mut ratatui::Frame, area: ratatui::layout::Rect) {
if !self.showing {
return;
}

f.dim_bg();

let block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
Expand All @@ -108,7 +110,7 @@ impl HelpScreen<'_> {
span!(" to move up/down ").white(),
]))
.style(Style::default());
frame.render_widget(Clear, area);
f.render_widget(Clear, area);

if self.last_known_width != area.width as usize
|| self.last_know_height != area.height as usize
Expand All @@ -131,7 +133,7 @@ impl HelpScreen<'_> {
.row_highlight_style(selected_row_style)
.cell_highlight_style(Style::default().bg(Color::White));

frame.render_stateful_widget(table, area, &mut self.state);
f.render_stateful_widget(table, area, &mut self.state);
}

pub fn render_help_line(&self, frame: &mut ratatui::Frame, area: Rect) {
Expand Down
32 changes: 17 additions & 15 deletions src/app/ui/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use ratatui::{
layout::{Alignment, Rect},
style::{Color, Modifier, Style, Stylize},
text::{Line, Span, Text},
widgets::{Block, BorderType, Borders, Clear, List, ListItem, ListState, Padding},
widgets::{
Block, BorderType, Borders, Clear, List, ListItem, ListState, Padding, StatefulWidget,
},
};
use ratatui_macros::span;
use std::{
Expand All @@ -15,7 +17,10 @@ use std::{
use tokio::sync::mpsc;
use tui_textarea::Key;

use super::input_box::{self, InputBox};
use super::{
Dim,
input_box::{self, InputBox},
};
use super::{question::Question, utils};

const NO_CONVERSATIONS: &str = "No conversations found";
Expand Down Expand Up @@ -480,6 +485,12 @@ impl<'a> HistoryScreen<'a> {
}

pub fn render(&mut self, f: &mut Frame, area: Rect) {
if !self.showing {
return;
}

f.dim_bg();

let instructions: Vec<Span> = vec![
" ".into(),
span!("q").green().bold(),
Expand All @@ -498,23 +509,14 @@ impl<'a> HistoryScreen<'a> {
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(Color::LightBlue))
.padding(Padding::new(1, 1, 0, 0))
.padding(Padding::symmetric(2, 0))
.title(Line::from(" Chat History ").bold())
.title_alignment(Alignment::Center)
.title_bottom(Line::from(instructions))
.style(Style::default());

let inner = block.inner(area);
if !self.showing {
if !self.conversations.is_empty() && self.items.is_empty() {
self.last_known_width = (inner.width - 2) as usize;
self.update_items();
}
return;
}
.title_bottom(Line::from(instructions));

f.render_widget(Clear, area);

let inner = block.inner(area);
if self.last_known_width != (inner.width - 2) as usize {
self.last_known_width = (inner.width - 2) as usize;
self.update_items();
Expand All @@ -523,7 +525,7 @@ impl<'a> HistoryScreen<'a> {
let list = List::new(self.items.clone())
.block(block)
.highlight_style(Style::default().add_modifier(Modifier::REVERSED));
f.render_stateful_widget(list, inner, &mut self.state);
list.render(area, f.buffer_mut(), &mut self.state);

let rename_area = input_box::build_area(inner, ((inner.width as f32 * 0.8).ceil()) as u16);
self.rename.render(f, rename_area);
Expand Down
4 changes: 4 additions & 0 deletions src/app/ui/input_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use ratatui::{
};
use tui_textarea::{CursorMove, TextArea};

use super::Dim;

pub struct InputBox<'a> {
showing: bool,
text: String,
Expand Down Expand Up @@ -64,6 +66,8 @@ impl<'a> InputBox<'a> {
return;
}

f.dim_bg();

f.render_widget(Clear, area);
self.input.render(area, f.buffer_mut());
}
Expand Down
21 changes: 21 additions & 0 deletions src/app/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,26 @@ pub use history::HistoryScreen;
pub use loading::Loading;
pub use models::ModelsScreen;
pub use notice::Notice;
use ratatui::{
style::{Color, Modifier, Style},
widgets::Block,
};
pub use scroll::Scroll;
pub use textarea::TextArea;

pub trait Dim {
fn dim_bg(&mut self);
}

impl Dim for ratatui::Frame<'_> {
fn dim_bg(&mut self) {
self.render_widget(
Block::default().style(
Style::default()
.bg(Color::Rgb(0, 0, 0))
.add_modifier(Modifier::DIM),
),
self.area(),
);
}
}
3 changes: 3 additions & 0 deletions src/app/ui/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use tokio::sync::mpsc;
use tui_textarea::Key;

use super::{
Dim,
input_box::{self, InputBox},
utils,
};
Expand Down Expand Up @@ -174,6 +175,8 @@ impl<'a> ModelsScreen<'a> {
return;
}

f.dim_bg();

let instructions = vec![
" ".into(),
span!("q").green().bold(),
Expand Down
4 changes: 3 additions & 1 deletion src/app/ui/question.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ratatui::{
};
use ratatui_macros::span;

use super::utils;
use super::{Dim, utils};

#[derive(Default)]
pub struct Question<'a> {
Expand Down Expand Up @@ -44,6 +44,8 @@ impl<'a> Question<'a> {
return;
}

f.dim_bg();

let max_width = (area.width as f32 * 0.8).ceil() as u16;
let lines = utils::split_to_lines(self.question.spans.clone(), (max_width - 2) as usize);
let area = build_area(area, max_width, lines.len() as u16 + 2);
Expand Down