Skip to content

Commit b4b5e82

Browse files
committed
feat: Add table-arrangement output option
Add `--table-arrangement` cli option to allow user choose between 3 table arrangement strategies: - dynamic (default) - content is distributed evenly trying to use as much space as necessary - dynamic-full-width - spread the output to the full available width - disabled - use as much space as necessary to render cells (table row may continue on the next row). Also fix accidentially ignored `--pretty` modifier when there is configuration for the requested resource.
1 parent ea87e0b commit b4b5e82

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

openstack_cli/src/cli.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,12 @@ pub struct GlobalOpts {
182182
#[arg(short, long, global=true, action = clap::ArgAction::Count, display_order = 920)]
183183
pub verbose: u8,
184184

185+
/// Output Table arrangement
186+
#[arg(long, global=true, default_value_t = TableArrangement::Dynamic, value_enum, display_order = 930)]
187+
pub table_arrangement: TableArrangement,
188+
185189
/// Record HTTP request timings
186-
#[arg(long, global=true, action = clap::ArgAction::SetTrue, display_order = 920)]
190+
#[arg(long, global=true, action = clap::ArgAction::SetTrue, display_order = 950)]
187191
pub timing: bool,
188192
}
189193

@@ -197,6 +201,22 @@ pub enum OutputFormat {
197201
Wide,
198202
}
199203

204+
/// Table arrangement
205+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
206+
pub enum TableArrangement {
207+
/// Dynamically determine the width of columns in regard to terminal width and content length.
208+
/// With this mode, the content in cells will wrap dynamically to get the the best column
209+
/// layout for the given content.
210+
#[default]
211+
Dynamic,
212+
/// This is mode is the same as the `Dynamic` arrangement, but it will always use as much space
213+
/// as it’s given. Any surplus space will be distributed between all columns.
214+
DynamicFullWidth,
215+
/// Don’t do any content arrangement. Tables with this mode might become wider than your output
216+
/// and look ugly.
217+
Disabled,
218+
}
219+
200220
/// Supported Top Level commands (services)
201221
#[allow(missing_docs)]
202222
#[derive(Parser)]

openstack_cli/src/output.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::collections::BTreeSet;
2121
use std::io::{self, Write};
2222

2323
use crate::OpenStackCliError;
24-
use crate::cli::{Cli, OutputFormat};
24+
use crate::cli::{Cli, OutputFormat, TableArrangement};
2525
use structable::{OutputConfig, StructTable};
2626

2727
/// Output Processor
@@ -31,6 +31,8 @@ pub(crate) struct OutputProcessor {
3131
pub(crate) config: OutputConfig,
3232
/// Whether output is for human or for machine
3333
pub(crate) target: OutputFor,
34+
/// Table arrangement
35+
pub(crate) table_arrangement: TableArrangement,
3436
}
3537

3638
/// Output target (human or machine) enum
@@ -40,6 +42,16 @@ pub(crate) enum OutputFor {
4042
Machine,
4143
}
4244

45+
impl From<TableArrangement> for ContentArrangement {
46+
fn from(value: TableArrangement) -> Self {
47+
match value {
48+
TableArrangement::Dynamic => Self::Dynamic,
49+
TableArrangement::DynamicFullWidth => Self::DynamicFullWidth,
50+
TableArrangement::Disabled => Self::Disabled,
51+
}
52+
}
53+
}
54+
4355
impl OutputProcessor {
4456
/// Get OutputConfig from passed arguments
4557
pub fn from_args(args: &Cli) -> Self {
@@ -55,6 +67,7 @@ impl OutputProcessor {
5567
pretty: args.global_opts.pretty,
5668
},
5769
target,
70+
table_arrangement: args.global_opts.table_arrangement,
5871
}
5972
}
6073

@@ -73,10 +86,16 @@ impl OutputProcessor {
7386
if config.fields.is_empty() && !config.wide {
7487
if let Some(cfg) = args.config.views.get(resource_key.as_ref()) {
7588
config = cfg.clone();
89+
config.wide = matches!(args.global_opts.output, Some(OutputFormat::Wide));
90+
config.pretty = args.global_opts.pretty;
7691
}
7792
}
7893

79-
Self { config, target }
94+
Self {
95+
config,
96+
target,
97+
table_arrangement: args.global_opts.table_arrangement,
98+
}
8099
}
81100

82101
/// Validate command arguments with respect to the output producing
@@ -137,7 +156,7 @@ impl OutputProcessor {
137156
let mut table = Table::new();
138157
table
139158
.load_preset(UTF8_FULL_CONDENSED)
140-
.set_content_arrangement(ContentArrangement::Dynamic)
159+
.set_content_arrangement(ContentArrangement::from(self.table_arrangement))
141160
.set_header(headers)
142161
.add_rows(rows);
143162
println!("{table}");
@@ -175,7 +194,7 @@ impl OutputProcessor {
175194
let mut table = Table::new();
176195
table
177196
.load_preset(UTF8_FULL_CONDENSED)
178-
.set_content_arrangement(ContentArrangement::Dynamic)
197+
.set_content_arrangement(ContentArrangement::from(self.table_arrangement))
179198
.set_header(headers)
180199
.add_rows(table_rows);
181200
println!("{table}");

0 commit comments

Comments
 (0)