MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / truncate_line

Function truncate_line

crates/openshell-tui/src/ui/sandbox_logs.rs:250–284  ·  view source on GitHub ↗

Truncate a span list to fit within `max_width` characters, appending `…` if needed.

(
    spans: Vec<Span<'a>>,
    max_width: usize,
    t: &'a crate::theme::Theme,
)

Source from the content-addressed store, hash-verified

248
249/// Truncate a span list to fit within `max_width` characters, appending `…` if needed.
250fn truncate_line<'a>(
251 spans: Vec<Span<'a>>,
252 max_width: usize,
253 t: &'a crate::theme::Theme,
254) -> Line<'a> {
255 if max_width == 0 {
256 return Line::from(spans);
257 }
258
259 let mut used = 0usize;
260 let mut out: Vec<Span<'_>> = Vec::with_capacity(spans.len());
261
262 for span in spans {
263 let content_len = span.content.len();
264 if used + content_len <= max_width {
265 out.push(span);
266 used += content_len;
267 } else {
268 // Partial fit — take what we can and append ellipsis.
269 let remaining = max_width.saturating_sub(used);
270 if remaining > 1 {
271 // Find a safe UTF-8 boundary.
272 let truncated = safe_truncate(&span.content, remaining - 1);
273 let mut s = truncated.to_string();
274 s.push('…');
275 out.push(Span::styled(s, span.style));
276 } else if remaining == 1 {
277 out.push(Span::styled("…", t.muted));
278 }
279 break;
280 }
281 }
282
283 Line::from(out)
284}
285
286/// Truncate a string to at most `max_bytes` bytes on a valid UTF-8 char boundary.
287fn safe_truncate(s: &str, max_bytes: usize) -> &str {

Callers 1

render_log_lineFunction · 0.85

Calls 3

safe_truncateFunction · 0.85
lenMethod · 0.80
pushMethod · 0.80

Tested by

no test coverage detected