aboutsummaryrefslogtreecommitdiff
path: root/src/app/api_helpers.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/app/api_helpers.rs')
-rw-r--r--src/app/api_helpers.rs83
1 files changed, 55 insertions, 28 deletions
diff --git a/src/app/api_helpers.rs b/src/app/api_helpers.rs
index 6c393dac..e0091b39 100644
--- a/src/app/api_helpers.rs
+++ b/src/app/api_helpers.rs
@@ -106,50 +106,77 @@ pub(super) fn read_terminal_snapshot(
source: crate::api::schema::ReadSource,
format: crate::api::schema::ReadFormat,
lines: Option<u32>,
-) -> String {
+) -> crate::pane::TerminalReadSnapshot {
use crate::api::schema::{ReadFormat, ReadSource};
- let requested_lines = lines.unwrap_or(80).min(1000) as usize;
- let text = match (format, source) {
- (ReadFormat::Text, ReadSource::Visible) => terminal.visible_text(),
- (ReadFormat::Text, ReadSource::Recent) => terminal.recent_text(requested_lines),
+ let line_limit = lines.map(|lines| lines.min(1000) as usize);
+ let recent_lines = line_limit.unwrap_or(80);
+ match (format, source) {
+ (ReadFormat::Text, ReadSource::Visible) => {
+ limit_snapshot_lines(terminal.visible_text(), line_limit)
+ }
+ (ReadFormat::Text, ReadSource::Recent) => terminal.recent_text_snapshot(recent_lines),
(ReadFormat::Text, ReadSource::RecentUnwrapped) => {
- terminal.recent_unwrapped_text(requested_lines)
+ terminal.recent_unwrapped_text_snapshot(recent_lines)
+ }
+ (ReadFormat::Text, ReadSource::Detection) => {
+ limit_snapshot_lines(terminal.detection_text(), line_limit)
+ }
+ (ReadFormat::Ansi, ReadSource::Visible) => {
+ limit_snapshot_lines(terminal.visible_ansi(), line_limit)
}
- (ReadFormat::Text, ReadSource::Detection) => terminal.detection_text(),
- (ReadFormat::Ansi, ReadSource::Visible) => terminal.visible_ansi(),
- (ReadFormat::Ansi, ReadSource::Recent) => terminal.recent_ansi(requested_lines),
+ (ReadFormat::Ansi, ReadSource::Recent) => terminal.recent_ansi_snapshot(recent_lines),
(ReadFormat::Ansi, ReadSource::RecentUnwrapped) => {
- terminal.recent_unwrapped_ansi(requested_lines)
+ terminal.recent_unwrapped_ansi_snapshot(recent_lines)
+ }
+ (ReadFormat::Ansi, ReadSource::Detection) => {
+ limit_snapshot_lines(terminal.detection_text(), line_limit)
}
- (ReadFormat::Ansi, ReadSource::Detection) => terminal.detection_text(),
- };
-
- if lines.is_some() && matches!(source, ReadSource::Visible | ReadSource::Detection) {
- last_lines(&text, requested_lines)
- } else {
- text
}
}
-fn last_lines(text: &str, count: usize) -> String {
- if text.is_empty() || count == 0 {
- return String::new();
- }
+fn limit_snapshot_lines(text: String, limit: Option<usize>) -> crate::pane::TerminalReadSnapshot {
+ let Some(limit) = limit else {
+ return crate::pane::TerminalReadSnapshot {
+ text,
+ truncated: false,
+ };
+ };
let lines: Vec<_> = text.split_inclusive('\n').collect();
- lines[lines.len().saturating_sub(count)..].concat()
+ crate::pane::TerminalReadSnapshot {
+ text: lines[lines.len().saturating_sub(limit)..].concat(),
+ truncated: lines.len() > limit,
+ }
}
#[cfg(test)]
mod read_snapshot_tests {
- use super::last_lines;
+ use super::limit_snapshot_lines;
+
+ #[test]
+ fn line_limit_preserves_endings_and_reports_omitted_lines() {
+ let snapshot = limit_snapshot_lines("one\ntwø\n三\n".into(), Some(2));
+ assert_eq!(snapshot.text, "twø\n三\n");
+ assert!(snapshot.truncated);
+
+ let snapshot = limit_snapshot_lines("one\ntwo\nthree".into(), Some(1));
+ assert_eq!(snapshot.text, "three");
+ assert!(snapshot.truncated);
+
+ let snapshot = limit_snapshot_lines("one\ntwo".into(), Some(0));
+ assert_eq!(snapshot.text, "");
+ assert!(snapshot.truncated);
+
+ let snapshot = limit_snapshot_lines("".into(), Some(2));
+ assert_eq!(snapshot.text, "");
+ assert!(!snapshot.truncated);
+ }
#[test]
- fn last_lines_preserves_real_line_endings_and_unicode() {
- assert_eq!(last_lines("one\ntwø\n三\n", 2), "twø\n三\n");
- assert_eq!(last_lines("one\ntwo\nthree", 1), "three");
- assert_eq!(last_lines("one\ntwo", 0), "");
- assert_eq!(last_lines("", 2), "");
+ fn omitted_line_limit_returns_the_complete_snapshot() {
+ let snapshot = limit_snapshot_lines("one\ntwo\n".into(), None);
+ assert_eq!(snapshot.text, "one\ntwo\n");
+ assert!(!snapshot.truncated);
}
}