Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the --tsv option to allow tab as a separator. #113

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/common/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,48 @@ pub struct Table<H: Display, V: Display, const COLUMNS: usize> {
pub struct DisplayConfig {
pub skip_header: bool,
pub separator: String,
pub tsv: bool,
}

impl Default for DisplayConfig {
fn default() -> Self {
Self {
skip_header: false,
separator: String::from("\t"),
tsv: false,
}
}
}

pub fn write<W: Write, H: Display, V: Display, const COLUMNS: usize>(
writer: W,
mut writer: W,
table: Table<H, V, COLUMNS>,
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<T: Display, const COLUMNS: usize>(
Expand Down
2 changes: 2 additions & 0 deletions src/drives/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -48,6 +49,7 @@ fn print_drives_table(config: &Config, drives: Vec<google_drive3::api::Drive>) {
&table::DisplayConfig {
skip_header: config.skip_header,
separator: config.field_separator.clone(),
tsv: config.tsv,
},
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/files/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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,
},
);

Expand Down
18 changes: 18 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 });
Expand All @@ -486,6 +501,7 @@ async fn main() {
skip_header,
truncate_name: !full_name,
field_separator,
tsv,
})
.await
.unwrap_or_else(handle_error)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/permissions/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -59,6 +60,7 @@ fn print_permissions_table(config: &Config, permissions: Vec<google_drive3::api:
&table::DisplayConfig {
skip_header: config.skip_header,
separator: config.field_separator.clone(),
tsv: config.tsv,
},
);
}
Expand Down