Skip to content

Commit 173d155

Browse files
authored
fix: add YAML document separators when paginating (fixes #39) (#50)
1 parent 7e73fda commit 173d155

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gws": patch
3+
---
4+
5+
fix: add YAML document separators (---) when paginating with --page-all --format yaml

src/formatter.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,16 @@ pub fn format_value(value: &Value, format: &OutputFormat) -> String {
6262
/// contains only data rows, making the combined output machine-parseable.
6363
///
6464
/// For JSON the output is compact (one JSON object per line / NDJSON).
65-
/// For YAML the page separator is preserved as-is.
65+
/// For YAML each page is prefixed with a `---` document separator so the
66+
/// combined stream is a valid YAML multi-document file.
6667
pub fn format_value_paginated(value: &Value, format: &OutputFormat, is_first_page: bool) -> String {
6768
match format {
6869
OutputFormat::Json => serde_json::to_string(value).unwrap_or_default(),
6970
OutputFormat::Csv => format_csv_page(value, is_first_page),
7071
OutputFormat::Table => format_table_page(value, is_first_page),
71-
OutputFormat::Yaml => format_yaml(value),
72+
// Prefix every page with a YAML document separator so that the
73+
// concatenated stream is parseable as a multi-document YAML file.
74+
OutputFormat::Yaml => format!("---\n{}", format_yaml(value)),
7275
}
7376
}
7477

@@ -560,13 +563,17 @@ mod tests {
560563
}
561564

562565
#[test]
563-
fn test_format_value_paginated_json_is_compact() {
564-
let val = json!({"files": [{"id": "1"}]});
565-
let output = format_value_paginated(&val, &OutputFormat::Json, true);
566-
// Compact JSON — no pretty-printed newlines inside the object
566+
fn test_format_value_paginated_yaml_has_document_separator() {
567+
let val = json!({"files": [{"id": "1", "name": "foo"}]});
568+
let first = format_value_paginated(&val, &OutputFormat::Yaml, true);
569+
let second = format_value_paginated(&val, &OutputFormat::Yaml, false);
570+
assert!(
571+
first.starts_with("---\n"),
572+
"first YAML page must start with ---"
573+
);
567574
assert!(
568-
!output.contains("\n "),
569-
"JSON must be compact in paginated mode"
575+
second.starts_with("---\n"),
576+
"continuation YAML pages must also start with ---"
570577
);
571578
}
572579
}

0 commit comments

Comments
 (0)