Skip to content

Commit

Permalink
chore: better format (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
sundy-li authored Apr 26, 2023
1 parent a19fff1 commit e9fd35d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bendsql"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
license = "Apache-2.0"
description = "Databend Native Command Line Tool"
Expand Down
6 changes: 5 additions & 1 deletion cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ pub struct Settings {
pub display_pretty_sql: bool,
pub prompt: String,
pub progress_color: String,
pub output_format: OutputFormat,

/// Show progress [bar] when executing queries.
/// Only works in non-interactive mode.
pub show_progress: bool,

/// Output format is set by the flag.
#[serde(skip)]
pub output_format: OutputFormat,
}

#[derive(ValueEnum, Clone, Debug, PartialEq, Deserialize)]
Expand Down
14 changes: 10 additions & 4 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ struct Args {
#[clap(long, value_parser = parse_key_val::<String, String>, help = "Data format options")]
format_opt: Vec<(String, String)>,

#[clap(short = 'o', long, default_value = "table", help = "Output format")]
output: OutputFormat,
#[clap(short = 'o', long, help = "Output format")]
output: Option<OutputFormat>,

#[clap(long, help = "Show progress for data loading in stderr")]
progress: bool,
Expand Down Expand Up @@ -233,10 +233,16 @@ pub async fn main() -> Result<()> {
conn_args.get_dsn()?
}
};
config.settings.output_format = args.output;
let is_repl = atty::is(atty::Stream::Stdin) && !args.non_interactive && args.query.is_none();
config.settings.show_progress = args.progress;
if let Some(output) = args.output {
config.settings.output_format = output;
} else if is_repl {
config.settings.output_format = OutputFormat::Table;
} else {
config.settings.output_format = OutputFormat::TSV;
}

let is_repl = atty::is(atty::Stream::Stdin) && !args.non_interactive && args.query.is_none();
let mut session = session::Session::try_new(dsn, config.settings, is_repl).await?;

if is_repl {
Expand Down
7 changes: 7 additions & 0 deletions cli/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ impl Session {
}
}
}

// if the last query is not finished with `;`, we need to execute it.
if let Some(query) = self.query.take() {
if let Err(e) = self.handle_query(false, &query).await {
eprintln!("{}", e);
}
}
}

fn append_query(&mut self, line: &str) -> Option<String> {
Expand Down

0 comments on commit e9fd35d

Please sign in to comment.