Skip to content
Closed
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pretty_assertions = "1.4.1"
temp-dir = "0.1.16"
criterion = "0.8.1"
insta = "1.46.0"
unicode-width = "0.2.0"
temp-env = "0.3.6"
stdext = "0.3.3"
url = "2.5.7"
Expand Down Expand Up @@ -71,6 +70,7 @@ tree-sitter-elixir = "=0.3.4"
regex = "1.12.2"
strip-ansi-escapes = "0.2.1"
unicode-segmentation = "1.12.0"
unicode-width = "0.2.0"
cached = "0.56.0"
strum = { version = "0.26.3", features = ["strum_macros"] }
tinyvec = "1.10.0"
Expand Down
29 changes: 14 additions & 15 deletions src/screen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::ui::{UiTree, layout_span};
use crate::{item_data::ItemData, ui};
use ratatui::{layout::Size, style::Style, text::Line};
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;

use crate::{Res, config::Config, items::hash};

Expand Down Expand Up @@ -473,26 +474,24 @@ pub(crate) fn layout_screen<'a>(
line.display.spans.into_iter().for_each(|span| {
let style = bg.patch(line.display.style).patch(span.style);

let span_width = span.content.graphemes(true).count();
let span_width = UnicodeWidthStr::width(span.content.as_ref());

if line_end + span_width >= size.width as usize {
// Truncate the span and insert an ellipsis to indicate overflow
let overflow = line_end + span_width - size.width as usize;
let budget = size.width as usize;
let mut accumulated = 0;
let mut truncated = String::new();
for grapheme in span.content.graphemes(true) {
let w = UnicodeWidthStr::width(grapheme);
if line_end + accumulated + w + 1 > budget {
break;
}
accumulated += w;
truncated.push_str(grapheme);
}
line_end = size.width as usize;
ui::layout_span(
layout,
(
span.content
.graphemes(true)
.take(span_width.saturating_sub(overflow + 1))
.collect::<String>()
.into(),
style,
),
);
ui::layout_span(layout, (truncated.into(), style));
layout_span(layout, ("…".into(), bg));
} else {
// Insert the span as normal
line_end += span_width;
ui::layout_span(layout, (span.content, style));
}
Expand Down
3 changes: 2 additions & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ratatui::Frame;
use ratatui::prelude::*;
use tui_prompts::State as _;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;

pub(crate) mod layout;
mod menu;
Expand Down Expand Up @@ -127,7 +128,7 @@ pub(crate) fn layout_line<'a>(layout: &mut UiTree<'a>, line: Line<'a>) {
}

pub(crate) fn layout_span<'a>(layout: &mut UiTree<'a>, span: (Cow<'a, str>, Style)) {
let width = span.0.graphemes(true).count() as u16;
let width = UnicodeWidthStr::width(span.0.as_ref()) as u16;
layout.leaf_with_size(span, [width, 1]);
}

Expand Down