Skip to content

Commit a922b83

Browse files
committed
Add format option to resource and extension list to not truncate table
1 parent d3a817b commit a922b83

File tree

6 files changed

+65
-14
lines changed

6 files changed

+65
-14
lines changed

dsc/src/args.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ pub enum OutputFormat {
1515
Yaml,
1616
}
1717

18+
#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
19+
pub enum ListOutputFormat {
20+
Json,
21+
PrettyJson,
22+
Yaml,
23+
TableNoTruncate,
24+
}
25+
1826
#[derive(Debug, Clone, PartialEq, Eq, ValueEnum, Deserialize)]
1927
pub enum TraceFormat {
2028
Default,
@@ -156,7 +164,7 @@ pub enum ExtensionSubCommand {
156164
/// Optional filter to apply to the list of extensions
157165
extension_name: Option<String>,
158166
#[clap(short = 'o', long, help = t!("args.outputFormat").to_string())]
159-
output_format: Option<OutputFormat>,
167+
output_format: Option<ListOutputFormat>,
160168
},
161169
}
162170

@@ -174,7 +182,7 @@ pub enum ResourceSubCommand {
174182
#[clap(short, long, help = t!("args.tags").to_string())]
175183
tags: Option<Vec<String>>,
176184
#[clap(short = 'o', long, help = t!("args.outputFormat").to_string())]
177-
output_format: Option<OutputFormat>,
185+
output_format: Option<ListOutputFormat>,
178186
},
179187
#[clap(name = "get", about = t!("args.resourceGet").to_string(), arg_required_else_help = true)]
180188
Get {

dsc/src/subcommand.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
use crate::args::{ConfigSubCommand, DscType, ExtensionSubCommand, OutputFormat, ResourceSubCommand};
4+
use crate::args::{ConfigSubCommand, DscType, ExtensionSubCommand, ListOutputFormat, OutputFormat, ResourceSubCommand};
55
use crate::resolve::{get_contents, Include};
66
use crate::resource_command::{get_resource, self};
77
use crate::tablewriter::Table;
@@ -611,7 +611,7 @@ pub fn resource(subcommand: &ResourceSubCommand, progress_format: ProgressFormat
611611
}
612612
}
613613

614-
fn list_extensions(dsc: &mut DscManager, extension_name: Option<&String>, format: Option<&OutputFormat>, progress_format: ProgressFormat) {
614+
fn list_extensions(dsc: &mut DscManager, extension_name: Option<&String>, format: Option<&ListOutputFormat>, progress_format: ProgressFormat) {
615615
let mut write_table = false;
616616
let mut table = Table::new(&[
617617
t!("subcommand.tableHeader_type").to_string().as_ref(),
@@ -654,7 +654,13 @@ fn list_extensions(dsc: &mut DscManager, extension_name: Option<&String>, format
654654
exit(EXIT_JSON_ERROR);
655655
}
656656
};
657-
write_object(&json, format, include_separator);
657+
let format = match format {
658+
Some(ListOutputFormat::Json) => Some(OutputFormat::Json),
659+
Some(ListOutputFormat::PrettyJson) => Some(OutputFormat::PrettyJson),
660+
Some(ListOutputFormat::Yaml) => Some(OutputFormat::Yaml),
661+
_ => None,
662+
};
663+
write_object(&json, format.as_ref(), include_separator);
658664
include_separator = true;
659665
// insert newline separating instances if writing to console
660666
if io::stdout().is_terminal() { println!(); }
@@ -663,11 +669,12 @@ fn list_extensions(dsc: &mut DscManager, extension_name: Option<&String>, format
663669
}
664670

665671
if write_table {
666-
table.print();
672+
let truncate = format != Some(&ListOutputFormat::TableNoTruncate);
673+
table.print(truncate);
667674
}
668675
}
669676

670-
fn list_resources(dsc: &mut DscManager, resource_name: Option<&String>, adapter_name: Option<&String>, description: Option<&String>, tags: Option<&Vec<String>>, format: Option<&OutputFormat>, progress_format: ProgressFormat) {
677+
fn list_resources(dsc: &mut DscManager, resource_name: Option<&String>, adapter_name: Option<&String>, description: Option<&String>, tags: Option<&Vec<String>>, format: Option<&ListOutputFormat>, progress_format: ProgressFormat) {
671678
let mut write_table = false;
672679
let mut table = Table::new(&[
673680
t!("subcommand.tableHeader_type").to_string().as_ref(),
@@ -677,7 +684,7 @@ fn list_resources(dsc: &mut DscManager, resource_name: Option<&String>, adapter_
677684
t!("subcommand.tableHeader_adapter").to_string().as_ref(),
678685
t!("subcommand.tableHeader_description").to_string().as_ref(),
679686
]);
680-
if format.is_none() && io::stdout().is_terminal() {
687+
if format == Some(&ListOutputFormat::TableNoTruncate) || (format.is_none() && io::stdout().is_terminal()) {
681688
// write as table if format is not specified and interactive
682689
write_table = true;
683690
}
@@ -759,7 +766,13 @@ fn list_resources(dsc: &mut DscManager, resource_name: Option<&String>, adapter_
759766
exit(EXIT_JSON_ERROR);
760767
}
761768
};
762-
write_object(&json, format, include_separator);
769+
let format = match format {
770+
Some(ListOutputFormat::Json) => Some(OutputFormat::Json),
771+
Some(ListOutputFormat::PrettyJson) => Some(OutputFormat::PrettyJson),
772+
Some(ListOutputFormat::Yaml) => Some(OutputFormat::Yaml),
773+
_ => None,
774+
};
775+
write_object(&json, format.as_ref(), include_separator);
763776
include_separator = true;
764777
// insert newline separating instances if writing to console
765778
if io::stdout().is_terminal() { println!(); }
@@ -768,6 +781,7 @@ fn list_resources(dsc: &mut DscManager, resource_name: Option<&String>, adapter_
768781
}
769782

770783
if write_table {
771-
table.print();
784+
let truncate = format != Some(&ListOutputFormat::TableNoTruncate);
785+
table.print(truncate);
772786
}
773787
}

dsc/src/tablewriter.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ impl Table {
4848
}
4949

5050
/// Print the table to the console.
51+
///
52+
/// # Arguments
53+
///
54+
/// * `truncate` - Whether to truncate the table if it is too wide for the console
5155
#[allow(clippy::format_push_string)]
52-
pub fn print(&self) {
56+
pub fn print(&self, truncate: bool) {
5357
let (width, _) = size().unwrap_or((80, 25));
5458
// make header bright green
5559
println!("\x1b[1;32m");
@@ -62,7 +66,7 @@ impl Table {
6266
}
6367
}
6468
// if header row is too wide, truncate
65-
if header_row.len() > width as usize {
69+
if truncate && header_row.len() > width as usize {
6670
header_row.truncate(width as usize);
6771
}
6872

@@ -80,7 +84,7 @@ impl Table {
8084
}
8185

8286
// if row is too wide and last character is not a space, truncate and add ellipsis unicode character
83-
if row_str.len() > width as usize {
87+
if truncate && row_str.len() > width as usize {
8488
row_str.truncate(width as usize);
8589
if !row_str.ends_with(' ') {
8690
row_str.pop();

dsc/tests/dsc_extension_discover.tests.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,16 @@ Describe 'Discover extension tests' {
9090
$env:DSC_RESOURCE_PATH = $null
9191
}
9292
}
93+
94+
It 'Table can be not truncated' {
95+
$output = dsc extension list --output-format table-no-truncate
96+
$LASTEXITCODE | Should -Be 0
97+
$foundWideLine = $false
98+
foreach ($line in $output) {
99+
if ($line.Length -gt [Console]::WindowWidth) {
100+
$foundWideLine = $true
101+
}
102+
}
103+
$foundWideLine | Should -BeTrue
104+
}
93105
}

dsc/tests/dsc_resource_list.tests.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,17 @@ Describe 'Tests for listing resources' {
8888
$LASTEXITCODE | Should -Be 0
8989
$out | Should -BeLike "*ERROR*Adapter not found: foo`*"
9090
}
91+
92+
It 'Table is not truncated' {
93+
$output = dsc resource list --output-format table-no-truncate
94+
$LASTEXITCODE | Should -Be 0
95+
$foundWideLine = $false
96+
foreach ($line in $output) {
97+
if ($line.Length -gt [Console]::WindowWidth) {
98+
$foundWideLine = $true
99+
break
100+
}
101+
}
102+
$foundWideLine | Should -BeTrue
103+
}
91104
}

extensions/test/discover/testDiscover.dsc.extension.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
33
"type": "Test/Discover",
44
"version": "0.1.0",
5-
"description": "Test discover resource",
5+
"description": "Test discover resource, this is a really long description to test that the table can be rendered without truncating the description text from this extension.",
66
"discover": {
77
"executable": "pwsh",
88
"args": [

0 commit comments

Comments
 (0)