Skip to content

Commit

Permalink
cli: Turn on pretty mode by default
Browse files Browse the repository at this point in the history
Fixes #929
  • Loading branch information
penberg committed Feb 10, 2025
1 parent 604ca40 commit 6822864
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
28 changes: 17 additions & 11 deletions cli/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Opts {
pub database: Option<PathBuf>,
#[clap(index = 2, help = "Optional SQL command to execute")]
pub sql: Option<String>,
#[clap(short = 'm', long, default_value_t = OutputMode::Raw)]
#[clap(short = 'm', long, default_value_t = OutputMode::List)]
pub output_mode: OutputMode,
#[clap(short, long, default_value = "")]
pub output: String,
Expand Down Expand Up @@ -88,7 +88,7 @@ impl Default for Io {

#[derive(ValueEnum, Copy, Clone, Debug, PartialEq, Eq)]
pub enum OutputMode {
Raw,
List,
Pretty,
}

Expand Down Expand Up @@ -121,7 +121,7 @@ pub enum Command {
Cwd,
/// Display information about settings
ShowInfo,
/// Set the value of NULL to be printedin 'raw' mode
/// Set the value of NULL to be printedin 'list' mode
NullValue,
/// Toggle 'echo' mode to repeat commands before execution
Echo,
Expand Down Expand Up @@ -160,7 +160,7 @@ impl Command {
Self::Help => ".help",
Self::Schema => ".schema ?<table>?",
Self::Opcodes => ".opcodes",
Self::OutputMode => ".mode raw|pretty",
Self::OutputMode => ".mode list|pretty",
Self::SetOutput => ".output ?file?",
Self::Cwd => ".cd <directory>",
Self::ShowInfo => ".show",
Expand Down Expand Up @@ -220,11 +220,17 @@ pub struct Settings {

impl From<&Opts> for Settings {
fn from(opts: &Opts) -> Self {
let is_stdout = opts.output.is_empty();
let output_mode = if opts.output_mode == OutputMode::List && is_stdout {
OutputMode::Pretty
} else {
opts.output_mode
};
Self {
null_value: String::new(),
output_mode: opts.output_mode,
output_mode,
echo: false,
is_stdout: opts.output.is_empty(),
is_stdout,
output_filename: opts.output.clone(),
db_file: opts
.database
Expand Down Expand Up @@ -257,7 +263,7 @@ impl std::fmt::Display for Settings {
}

impl Limbo {
pub fn new() -> anyhow::Result<Self> {
pub fn new(interactive: bool) -> anyhow::Result<Self> {
let opts = Opts::parse();
let db_file = opts
.database
Expand Down Expand Up @@ -293,7 +299,7 @@ impl Limbo {
if opts.sql.is_some() {
app.handle_first_input(opts.sql.as_ref().unwrap());
}
if !opts.quiet {
if !opts.quiet && interactive {
app.write_fmt(format_args!("Limbo v{}", env!("CARGO_PKG_VERSION")))?;
app.writeln("Enter \".help\" for usage hints.")?;
app.display_in_memory()?;
Expand Down Expand Up @@ -391,7 +397,7 @@ impl Limbo {
Ok(file) => {
self.writer = Box::new(file);
self.opts.is_stdout = false;
self.opts.output_mode = OutputMode::Raw;
self.opts.output_mode = OutputMode::List;
self.opts.output_filename = path.to_string();
Ok(())
}
Expand Down Expand Up @@ -618,7 +624,7 @@ impl Limbo {
) -> anyhow::Result<()> {
match output {
Ok(Some(ref mut rows)) => match self.opts.output_mode {
OutputMode::Raw => loop {
OutputMode::List => loop {
if self.interrupt_count.load(Ordering::SeqCst) > 0 {
println!("Query interrupted.");
return Ok(());
Expand Down Expand Up @@ -916,7 +922,7 @@ Special Commands:
.quit Stop interpreting input stream and exit.
.show Display current settings.
.open <database_file> Open and connect to a database file.
.output <mode> Change the output mode. Available modes are 'raw' and 'pretty'.
.mode <mode> Change the output mode. Available modes are 'list' and 'pretty'.
.schema <table_name> Show the schema of the specified table.
.tables <pattern> List names of tables matching LIKE pattern TABLE
.opcodes Display all the opcodes defined by the virtual machine
Expand Down
4 changes: 3 additions & 1 deletion cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ mod import;
mod opcodes_dictionary;

use rustyline::{error::ReadlineError, DefaultEditor};
use std::io::IsTerminal;
use std::sync::atomic::Ordering;

fn main() -> anyhow::Result<()> {
env_logger::init();
let mut app = app::Limbo::new()?;
let interactive = std::io::stdin().is_terminal();
let mut app = app::Limbo::new(interactive)?;
let mut rl = DefaultEditor::new()?;
let home = dirs::home_dir().expect("Could not determine home directory");
let history_file = home.join(".limbo_history");
Expand Down
5 changes: 3 additions & 2 deletions testing/tester.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ set test_dbs [list "testing/testing.db" "testing/testing_norowidalias.db"]
set test_small_dbs [list "testing/testing_small.db" ]

proc evaluate_sql {sqlite_exec db_name sql} {
set command [list $sqlite_exec $db_name $sql]
set output [exec {*}$command]
set command [list $sqlite_exec $db_name]
set sql_with_mode ".mode list\n$sql"
set output [exec {*}$command << $sql_with_mode]
return $output
}

Expand Down

0 comments on commit 6822864

Please sign in to comment.