Skip to content

Commit

Permalink
chore: Ratatui 0.24.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Dustin Blackman committed Nov 27, 2023
1 parent a123857 commit cdcdd22
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 34 deletions.
60 changes: 56 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ dirs = "=5.0.1"
futures = "=0.3.28"
once_cell = "=1.18.0"
owo-colors = { version = "3.5.0", features = ["supports-colors"] }
ratatui = "=0.23.0"
ratatui = "=0.24.0"
reqwest = { version = "=0.11.22", default-features = false, features = ["stream", "json", "gzip", "native-tls-vendored"] }
rust-embed = { version = "=8.0.0", features = ["include-exclude"] }
serde = "=1.0.193"
Expand All @@ -56,7 +56,7 @@ serde_yaml = "=0.9.27"
syntect = { version = "=5.1.0", default-features = false, features = ["parsing", "default-syntaxes", "plist-load", "yaml-load", "regex-onig"] }
tokio = { version = "=1.33.0", features = ["fs", "macros", "rt-multi-thread", "sync", "process"] }
tokio-util = "=0.7.9"
tui-textarea = { version = "=0.2.4", default-features = false, features = ["ratatui-crossterm"] }
tui-textarea = { version = "=0.4.0", default-features = false, features = ["crossterm", "ratatui"] }
uuid = { version = "=1.6.1", features = ["v4"] }

[dev-dependencies]
Expand Down
5 changes: 3 additions & 2 deletions src/application/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ async fn start_loop<B: Backend>(
key: Key::Char(char),
ctrl: false,
alt: false,
shift: false,
});
}
}
Expand All @@ -62,8 +63,8 @@ async fn start_loop<B: Backend>(
.constraints(vec![Constraint::Min(1), Constraint::Max(4)])
.split(frame.size());

if layout[0].width != app_state.last_known_width
|| layout[0].height != app_state.last_known_height
if layout[0].width as usize != app_state.last_known_width
|| layout[0].height as usize != app_state.last_known_height
{
app_state.set_rect(layout[0]);
}
Expand Down
3 changes: 1 addition & 2 deletions src/domain/models/loading.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use ratatui::prelude::Alignment;
use ratatui::prelude::Backend;
use ratatui::prelude::Rect;
use ratatui::widgets::Block;
use ratatui::widgets::BorderType;
Expand All @@ -12,7 +11,7 @@ use ratatui::Frame;
pub struct Loading {}

impl Loading {
pub fn render<B: Backend>(&self, frame: &mut Frame<B>, rect: Rect) {
pub fn render(&self, frame: &mut Frame, rect: Rect) {
frame.render_widget(
Paragraph::new("Loading...")
.block(
Expand Down
4 changes: 2 additions & 2 deletions src/domain/models/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Message {
self.text += &text.replace('\t', " ");
}

pub fn as_string_lines(&self, line_max_width: u16) -> Vec<String> {
pub fn as_string_lines(&self, line_max_width: usize) -> Vec<String> {
let mut lines: Vec<String> = Vec::new();

for full_line in self.text.split('\n') {
Expand All @@ -77,7 +77,7 @@ impl Message {
let mut current_lines: Vec<&str> = vec![];

for word in full_line.split(' ') {
if word.len() + char_count + 1 > line_max_width.into() {
if word.len() + char_count + 1 > line_max_width {
lines.push(current_lines.join(" ").trim_end().to_string());
current_lines = vec![word];
char_count = word.len() + 1;
Expand Down
10 changes: 5 additions & 5 deletions src/domain/services/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub struct AppState<'a> {
pub codeblocks: CodeBlocks,
pub editor_context: Option<EditorContext>,
pub exit_warning: bool,
pub last_known_height: u16,
pub last_known_width: u16,
pub last_known_height: usize,
pub last_known_width: usize,
pub messages: Vec<Message>,
pub scroll: Scroll,
pub session_id: String,
Expand Down Expand Up @@ -289,8 +289,8 @@ impl<'a> AppState<'a> {
}

pub fn set_rect(&mut self, rect: Rect) {
self.last_known_width = rect.width;
self.last_known_height = rect.height;
self.last_known_width = rect.width.into();
self.last_known_height = rect.height.into();
self.sync_dependants();
}

Expand All @@ -305,7 +305,7 @@ impl<'a> AppState<'a> {
.set_messages(&self.messages, self.last_known_width);

self.scroll
.set_state(self.bubble_list.len() as u16, self.last_known_height);
.set_state(self.bubble_list.len(), self.last_known_height);

if self.waiting_for_backend {
self.scroll.last();
Expand Down
12 changes: 5 additions & 7 deletions src/domain/services/bubble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ pub enum BubbleAlignment {
pub struct Bubble {
alignment: BubbleAlignment,
message: Message,
window_max_width: u16,
window_max_width: usize,
codeblock_counter: usize,
}

impl<'a> Bubble {
pub fn new(
message: Message,
alignment: BubbleAlignment,
window_max_width: u16,
window_max_width: usize,
codeblock_counter: usize,
) -> Bubble {
return Bubble {
Expand Down Expand Up @@ -106,9 +106,7 @@ impl<'a> Bubble {
}
}

let bubble_padding = [" "]
.repeat(self.window_max_width as usize - line_length)
.join("");
let bubble_padding = [" "].repeat(self.window_max_width - line_length).join("");

if self.alignment == BubbleAlignment::Left {
spans.push(Span::from(bubble_padding));
Expand All @@ -133,7 +131,7 @@ impl<'a> Bubble {

let message_lines = self
.message
.as_string_lines(self.window_max_width - line_border_width as u16);
.as_string_lines(self.window_max_width - line_border_width);

let mut max_line_length = message_lines
.iter()
Expand All @@ -159,7 +157,7 @@ impl<'a> Bubble {
let bottom_bar = format!("╰{inner_bar}╯");
let bar_bubble_padding = [" "]
// TODO WTF is 8?
.repeat(self.window_max_width as usize - max_line_length - 8)
.repeat(self.window_max_width - max_line_length - 8)
.join("");

let username = &self.message.author_formatted;
Expand Down
9 changes: 4 additions & 5 deletions src/domain/services/bubble_list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;

use ratatui::prelude::Backend;
use ratatui::prelude::Rect;
use ratatui::text::Line;
use ratatui::widgets::Block;
Expand All @@ -25,7 +24,7 @@ struct BubbleCacheEntry<'a> {

pub struct BubbleList<'a> {
cache: HashMap<usize, BubbleCacheEntry<'a>>,
line_width: u16,
line_width: usize,
lines: Vec<Line<'a>>,
theme: Theme,
}
Expand All @@ -40,7 +39,7 @@ impl<'a> BubbleList<'a> {
};
}

pub fn set_messages(&mut self, messages: &[Message], line_width: u16) {
pub fn set_messages(&mut self, messages: &[Message], line_width: usize) {
if self.line_width != line_width {
self.cache.clear();
self.line_width = line_width;
Expand Down Expand Up @@ -92,11 +91,11 @@ impl<'a> BubbleList<'a> {
return self.lines.len();
}

pub fn render<B: Backend>(&mut self, frame: &mut Frame<B>, rect: Rect, scroll: u16) {
pub fn render(&mut self, frame: &mut Frame, rect: Rect, scroll: usize) {
frame.render_widget(
Paragraph::new(self.lines.to_owned())
.block(Block::default())
.scroll((scroll, 0)),
.scroll((scroll.try_into().unwrap(), 0)),
rect,
);
}
Expand Down
10 changes: 5 additions & 5 deletions src/domain/services/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use ratatui::widgets::ScrollbarState;

#[derive(Default)]
pub struct Scroll {
list_length: u16,
viewport_length: u16,
pub position: u16,
list_length: usize,
viewport_length: usize,
pub position: usize,
pub scrollbar_state: ScrollbarState,
}

Expand All @@ -21,7 +21,7 @@ impl Scroll {
}

pub fn down(&mut self) {
let mut clamp: u16 = 0;
let mut clamp: usize = 0;
if self.list_length > self.viewport_length {
clamp = self.list_length - self.viewport_length + 1;
}
Expand All @@ -48,7 +48,7 @@ impl Scroll {
self.scrollbar_state.last();
}

pub fn set_state(&mut self, list_length: u16, viewport_length: u16) {
pub fn set_state(&mut self, list_length: usize, viewport_length: usize) {
self.list_length = list_length;
self.viewport_length = viewport_length;
self.scrollbar_state = self
Expand Down

0 comments on commit cdcdd22

Please sign in to comment.