diff --git a/src/common/table.rs b/src/common/table.rs index 6345c6e..6f261e1 100644 --- a/src/common/table.rs +++ b/src/common/table.rs @@ -12,6 +12,7 @@ pub struct Table { pub struct DisplayConfig { pub skip_header: bool, pub separator: String, + pub tsv: bool, } impl Default for DisplayConfig { @@ -19,26 +20,40 @@ impl Default for DisplayConfig { Self { skip_header: false, separator: String::from("\t"), + tsv: false, } } } pub fn write( - writer: W, + mut writer: W, table: Table, config: &DisplayConfig, ) -> Result<(), io::Error> { - let mut tw = TabWriter::new(writer).padding(3); + if config.tsv { + if !config.skip_header { + writeln!(&mut writer, "{}", to_row(config, table.header))?; + } - if !config.skip_header { - writeln!(&mut tw, "{}", to_row(config, table.header))?; - } + for value in table.values { + writeln!(&mut writer, "{}", to_row(config, value))?; + } + + writer.flush() + } else { + let mut tw = TabWriter::new(writer).padding(3); + + if !config.skip_header { + writeln!(&mut tw, "{}", to_row(config, table.header))?; + } + + for value in table.values { + writeln!(&mut tw, "{}", to_row(config, value))?; + } - for value in table.values { - writeln!(&mut tw, "{}", to_row(config, value))?; + tw.flush() } - tw.flush() } fn to_row( diff --git a/src/drives/list.rs b/src/drives/list.rs index 5c303db..688ace8 100644 --- a/src/drives/list.rs +++ b/src/drives/list.rs @@ -11,6 +11,7 @@ use std::io; pub struct Config { pub skip_header: bool, pub field_separator: String, + pub tsv: bool, } pub async fn list(config: Config) -> Result<(), Error> { @@ -48,6 +49,7 @@ fn print_drives_table(config: &Config, drives: Vec) { &table::DisplayConfig { skip_header: config.skip_header, separator: config.field_separator.clone(), + tsv: config.tsv, }, ); } diff --git a/src/files/list.rs b/src/files/list.rs index aaa1c28..3e0b5a7 100644 --- a/src/files/list.rs +++ b/src/files/list.rs @@ -22,6 +22,7 @@ pub struct Config { pub skip_header: bool, pub truncate_name: bool, pub field_separator: String, + pub tsv: bool, } pub async fn list(config: Config) -> Result<(), Error> { @@ -66,6 +67,7 @@ pub async fn list(config: Config) -> Result<(), Error> { &table::DisplayConfig { skip_header: config.skip_header, separator: config.field_separator, + tsv: config.tsv, }, ); diff --git a/src/main.rs b/src/main.rs index b309066..de4ba23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -104,6 +104,10 @@ enum DriveCommand { /// Field separator #[arg(long, default_value_t = String::from("\t"))] field_separator: String, + + /// enable TSV mode. Use tab separator and disable tab writer. + #[arg(long)] + tsv: bool, }, } @@ -152,6 +156,10 @@ enum FileCommand { /// Field separator #[arg(long, default_value_t = String::from("\t"))] field_separator: String, + + /// enable TSV mode. Use tab separator and disable tab writer. + #[arg(long)] + tsv: bool, }, /// Download file @@ -359,6 +367,10 @@ enum PermissionCommand { /// Field separator #[arg(long, default_value_t = String::from("\t"))] field_separator: String, + + /// enable TSV mode. Use tab separator and disable tab writer. + #[arg(long)] + tsv: bool, }, /// Revoke permissions for a file. If no other options are specified, the 'anyone' permission will be revoked @@ -438,9 +450,11 @@ async fn main() { DriveCommand::List { skip_header, field_separator, + tsv, } => drives::list(drives::list::Config { skip_header, field_separator, + tsv, }) .await .unwrap_or_else(handle_error), @@ -471,6 +485,7 @@ async fn main() { skip_header, full_name, field_separator, + tsv, } => { let parent_query = parent.map(|folder_id| ListQuery::FilesInFolder { folder_id }); @@ -486,6 +501,7 @@ async fn main() { skip_header, truncate_name: !full_name, field_separator, + tsv, }) .await .unwrap_or_else(handle_error) @@ -687,12 +703,14 @@ async fn main() { file_id, skip_header, field_separator, + tsv, } => { // fmt permissions::list(permissions::list::Config { file_id, skip_header, field_separator, + tsv, }) .await .unwrap_or_else(handle_error) diff --git a/src/permissions/list.rs b/src/permissions/list.rs index 4e3140c..f4dcf98 100644 --- a/src/permissions/list.rs +++ b/src/permissions/list.rs @@ -15,6 +15,7 @@ pub struct Config { pub file_id: String, pub skip_header: bool, pub field_separator: String, + pub tsv: bool, } pub async fn list(config: Config) -> Result<(), Error> { @@ -59,6 +60,7 @@ fn print_permissions_table(config: &Config, permissions: Vec