From 1459a9cdcdad53ee412b7a4b6b41a66eef832058 Mon Sep 17 00:00:00 2001 From: Nawaf Date: Sun, 20 Aug 2023 16:43:53 -0400 Subject: [PATCH] fix bufferline overflow --- helix-term/src/ui/editor.rs | 57 +++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index aa159d40d..c43775eec 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -539,31 +539,58 @@ impl EditorView { let mut x = viewport.x; let current_doc = view!(editor).doc; + let mut current_doc_idx = None; + let width = viewport.right(); + + let mut needed_width = 0; + let entries: Vec = editor + .documents() + .enumerate() + .map(|(idx, doc)| { + let fname = doc + .path() + .unwrap_or(&scratch) + .file_name() + .unwrap_or_default() + .to_str() + .unwrap_or_default(); + + let text = format!(" {}{} ", fname, if doc.is_modified() { "[+]" } else { "" }); + + if doc.id() == current_doc { + current_doc_idx = Some(idx); + needed_width += text.len(); + } else if current_doc_idx.is_none() { + needed_width += text.len(); + } + text + }) + .collect(); - for doc in editor.documents() { - let fname = doc - .path() - .unwrap_or(&scratch) - .file_name() - .unwrap_or_default() - .to_str() - .unwrap_or_default(); - - let style = if current_doc == doc.id() { + let mut to_trim = needed_width.saturating_sub(width as usize); + for (idx, filename) in entries.iter().enumerate() { + let mut text = filename.as_str(); + if to_trim > 0 { + if text.len() > to_trim { + text = &filename[to_trim..]; + to_trim = 0; + } else if to_trim > text.len() { + to_trim -= text.len(); + continue; + } + } + let style = if idx == current_doc_idx.unwrap() { bufferline_active } else { bufferline_inactive }; - let text = format!(" {}{} ", fname, if doc.is_modified() { "[+]" } else { "" }); - let used_width = viewport.x.saturating_sub(x); - let rem_width = surface.area.width.saturating_sub(used_width); - + let rem_width = width.saturating_sub(x); x = surface .set_stringn(x, viewport.y, text, rem_width as usize, style) .0; - if x >= surface.area.right() { + if x >= width { break; } }