Skip to content

Commit 8aede30

Browse files
committed
Add CompactStr for Debug Lines and Timestamp text
1 parent 2a73448 commit 8aede30

4 files changed

Lines changed: 73 additions & 48 deletions

File tree

Cargo.lock

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ arc-swap = "1.7.1"
1717
better-panic = "0.3.0"
1818
chrono = "0.4.39"
1919
color-eyre = "0.6.3"
20+
compact_str = "0.9.0"
2021
crokey = { version = "1.1.2", features = ["serde"] }
2122
# crokey = { path = "../crokey", features = ["serde"] }
2223
derivative = "2.2.0"

src/tui/buffer/buf_line.rs

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::borrow::Cow;
22

33
use ansi_to_tui::IntoText;
44
use chrono::{DateTime, Local};
5+
use compact_str::{CompactString, ToCompactString, format_compact};
56
use memchr::memmem::Finder;
67
use ratatui::{
78
layout::Size,
@@ -20,42 +21,42 @@ use crate::traits::{ByteSuffixCheck, FirstChars, LineHelpers};
2021
#[derive(Debug)]
2122
pub struct BufLine {
2223
pub timestamp: DateTime<Local>,
23-
timestamp_str: String,
24+
timestamp_str: CompactString,
2425

2526
#[cfg(debug_assertions)]
26-
debug_info: String,
27+
debug_info: CompactString,
2728

2829
value: Line<'static>,
29-
// maybe? depends on whats easier to chain bytes from, for the hex view later
30-
// raw_value: Vec<u8>,
30+
3131
/// How many vertical lines are needed in the terminal to fully show this line.
32-
rendered_line_height: usize,
33-
// Might not be exactly accurate, but would be enough to place user input lines in proper space if needing to
34-
pub(super) raw_buffer_index: usize,
32+
// Truncated from usize, since even the ratatui sizes are capped there.
33+
rendered_line_height: u16,
3534

35+
pub(super) raw_buffer_index: usize,
36+
// TODO turn into enum
3637
pub(super) is_bytes: bool,
3738
pub(super) is_macro: bool,
3839
}
3940

40-
impl PartialEq for BufLine {
41-
fn eq(&self, other: &Self) -> bool {
42-
self.timestamp == other.timestamp
43-
}
44-
}
41+
// impl PartialEq for BufLine {
42+
// fn eq(&self, other: &Self) -> bool {
43+
// self.timestamp == other.timestamp
44+
// }
45+
// }
4546

46-
impl Eq for BufLine {}
47+
// impl Eq for BufLine {}
4748

48-
impl PartialOrd for BufLine {
49-
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
50-
self.timestamp.partial_cmp(&other.timestamp)
51-
}
52-
}
49+
// impl PartialOrd for BufLine {
50+
// fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
51+
// self.timestamp.partial_cmp(&other.timestamp)
52+
// }
53+
// }
5354

54-
impl Ord for BufLine {
55-
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
56-
self.timestamp.cmp(&other.timestamp)
57-
}
58-
}
55+
// impl Ord for BufLine {
56+
// fn cmp(&self, other: &Self) -> std::cmp::Ordering {
57+
// self.timestamp.cmp(&other.timestamp)
58+
// }
59+
// }
5960

6061
// Many changes needed, esp. in regards to current app-state things (index, width, color, showing timestamp)
6162
impl BufLine {
@@ -80,15 +81,15 @@ impl BufLine {
8081
}
8182

8283
#[cfg(debug_assertions)]
83-
let debug_info = format!(
84+
let debug_info = format_compact!(
8485
"({start}..{end}, {len}) ",
8586
start = raw_buffer_index,
8687
end = raw_buffer_index + raw_value.len(),
8788
len = raw_value.len(),
8889
);
8990

9091
let mut bufline = Self {
91-
timestamp_str: now.format(time_format).to_string(),
92+
timestamp_str: now.format(time_format).to_compact_string(),
9293
timestamp: now,
9394
#[cfg(debug_assertions)]
9495
debug_info,
@@ -120,7 +121,7 @@ impl BufLine {
120121
}
121122
#[cfg(debug_assertions)]
122123
{
123-
let debug_info = format!(
124+
let debug_info = format_compact!(
124125
"({start}..{end}, {len}) ",
125126
start = self.raw_buffer_index,
126127
end = self.raw_buffer_index + raw_value.len(),
@@ -182,11 +183,11 @@ impl BufLine {
182183
// Paragraph::line_count comes from an unstable ratatui feature (unstable-rendered-line-info)
183184
// which may be changed/removed in the future. If so, I'll need to roll my own wrapping/find someone's to steal.
184185
let height = para.line_count(area_width.saturating_sub(1));
185-
self.rendered_line_height = height;
186+
self.rendered_line_height = (height as u16);
186187
height
187188
}
188189

189-
pub fn get_line_height(&self) -> usize {
190+
pub fn get_line_height(&self) -> u16 {
190191
self.rendered_line_height
191192
}
192193

@@ -223,9 +224,9 @@ impl BufLine {
223224
self.raw_buffer_index
224225
}
225226

226-
pub fn timestamp(&self) -> (DateTime<Local>, &str) {
227-
(self.timestamp, &self.timestamp_str)
228-
}
227+
// pub fn timestamp(&self) -> (DateTime<Local>, &str) {
228+
// (self.timestamp, &self.timestamp_str)
229+
// }
229230

230231
// pub fn is_bytes(&self) -> bool {}
231232

src/tui/buffer/mod.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub struct Buffer {
5555
raw_buffer: Vec<u8>,
5656
// Time-tagged indexes into `raw_buffer`, from each input from the port.
5757
buffer_timestamps: Vec<(usize, DateTime<Local>)>,
58-
lines: Vec<BufLine>,
58+
port_lines: Vec<BufLine>,
5959
user_lines: Vec<BufLine>,
6060
last_line_completed: bool,
6161

@@ -106,7 +106,7 @@ impl Buffer {
106106
Self {
107107
raw_buffer: Vec::with_capacity(1024),
108108
buffer_timestamps: Vec::with_capacity(1024),
109-
lines: Vec::with_capacity(1024),
109+
port_lines: Vec::with_capacity(1024),
110110
user_lines: Vec::with_capacity(1024),
111111
last_terminal_size: Size::default(),
112112
state: BufferState {
@@ -215,7 +215,7 @@ impl Buffer {
215215
}
216216

217217
if append_to_last {
218-
let last_line = self.lines.last_mut().expect("can't append to nothing");
218+
let last_line = self.port_lines.last_mut().expect("can't append to nothing");
219219
let last_index = last_line.index_in_buffer();
220220

221221
let slice = &self.raw_buffer[last_index..start_index + trunc.len()];
@@ -267,7 +267,7 @@ impl Buffer {
267267
// .join("")
268268
// .escape_default()
269269
// );
270-
self.lines.push(BufLine::new_with_line(
270+
self.port_lines.push(BufLine::new_with_line(
271271
line,
272272
#[cfg(debug_assertions)]
273273
orig,
@@ -313,7 +313,7 @@ impl Buffer {
313313
}
314314

315315
// let _ = std::mem::take(&mut self.lines);
316-
self.lines.clear();
316+
self.port_lines.clear();
317317

318318
// Taking these variables out of `self` temporarily to allow running &mut self methods while holding
319319
// references to these.
@@ -341,8 +341,9 @@ impl Buffer {
341341
// If a user line isn't visible, ignore it when taking external new-lines into account.
342342
.filter(|b| user_echo.filter_user_line(b))
343343
.map(|b| (b.raw_buffer_index, b.timestamp, true)),
344-
|device, user| match device.0.cmp(&user.0) {
345-
Ordering::Equal => device.1 <= user.1,
344+
// Interleaving by sorting in order of raw_buffer_index, if they're equal, then whichever has a sooner timestamp.
345+
|port, user| match port.0.cmp(&user.0) {
346+
Ordering::Equal => port.1 <= user.1,
346347
Ordering::Less => true,
347348
Ordering::Greater => false,
348349
},
@@ -418,7 +419,7 @@ impl Buffer {
418419

419420
/// Updates each BufLine's render height with the new terminal width, returning the sum total at the end
420421
pub fn update_wrapped_line_heights(&mut self) -> usize {
421-
self.lines.iter_mut().fold(0, |total, l| {
422+
self.port_lines.iter_mut().fold(0, |total, l| {
422423
let new_height = l.update_line_height(
423424
self.last_terminal_size.width,
424425
self.state.timestamps_visible,
@@ -448,13 +449,18 @@ impl Buffer {
448449
}
449450
fn buflines_iter(&self) -> impl Iterator<Item = &BufLine> {
450451
if self.state.user_echo_input == UserEcho::None {
451-
Either::Left(self.lines.iter())
452+
Either::Left(self.port_lines.iter())
452453
} else {
453-
Either::Right(interleave(
454-
self.lines.iter(),
454+
Either::Right(interleave_by(
455+
self.port_lines.iter(),
455456
self.user_lines
456457
.iter()
457458
.filter(|l| self.state.user_echo_input.filter_user_line(l)),
459+
|port, user| match port.raw_buffer_index.cmp(&user.raw_buffer_index) {
460+
Ordering::Equal => port.timestamp <= user.timestamp,
461+
Ordering::Less => true,
462+
Ordering::Greater => false,
463+
},
458464
))
459465
}
460466
}
@@ -488,9 +494,9 @@ impl Buffer {
488494
.enumerate()
489495
{
490496
current_line_index = index;
491-
current_line_height = entries_lines;
497+
current_line_height = entries_lines as usize;
492498

493-
lines_from_top += entries_lines;
499+
lines_from_top += entries_lines as usize;
494500
if lines_from_top > vert_scroll {
495501
break;
496502
}
@@ -578,7 +584,7 @@ impl Buffer {
578584
}
579585
}
580586
pub fn clear(&mut self) {
581-
self.lines.clear();
587+
self.port_lines.clear();
582588
self.buffer_timestamps.clear();
583589
self.user_lines.clear();
584590
self.raw_buffer.clear();
@@ -650,14 +656,16 @@ impl Buffer {
650656
/// taking into account if text wrapping is enabled or not.
651657
pub fn combined_height(&self) -> usize {
652658
if self.state.text_wrapping {
653-
self.buflines_iter().map(|l| l.get_line_height()).sum()
659+
self.buflines_iter()
660+
.map(|l| l.get_line_height() as usize)
661+
.sum()
654662
} else {
655663
self.buflines_iter().count()
656664
}
657665
}
658666

659667
pub fn port_lines_len(&self) -> usize {
660-
self.lines.len()
668+
self.port_lines.len()
661669
}
662670

663671
pub fn update_terminal_size(

0 commit comments

Comments
 (0)