Skip to content
Merged
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
17 changes: 16 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,32 @@ pub struct Context {
pub args: Args,
pub url: String,
pub sa_token: Option<ServiceAccountToken>,
pub prompt1: Option<String>,
pub prompt2: Option<String>,
pub prompt3: Option<String>,
}

impl Context {
pub fn new(args: Args) -> Self {
let url = get_url(&args);
Self { args, url, sa_token: None }
Self { args, url, sa_token: None, prompt1: None, prompt2: None, prompt3: None }
}

pub fn update_url(&mut self) {
self.url = get_url(&self.args);
}

pub fn set_prompt1(&mut self, prompt: String) {
self.prompt1 = Some(prompt);
}

pub fn set_prompt2(&mut self, prompt: String) {
self.prompt2 = Some(prompt);
}

pub fn set_prompt3(&mut self, prompt: String) {
self.prompt3 = Some(prompt);
}
}

#[cfg(test)]
Expand Down
32 changes: 29 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ use rustyline::{config::Configurer, error::ReadlineError, Cmd, DefaultEditor, Ev
mod args;
mod auth;
mod context;
mod meta_commands;
mod query;
mod utils;

use args::get_args;
use auth::maybe_authenticate;
use context::Context;
use meta_commands::handle_meta_command;
use query::{query, try_split_queries};
use utils::history_path;

Expand Down Expand Up @@ -57,11 +59,26 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut buffer: String = String::new();
loop {
let prompt = if !buffer.trim_start().is_empty() {
"~> "
// Continuation prompt (PROMPT2)
if let Some(custom_prompt) = &context.prompt2 {
custom_prompt.as_str()
} else {
"~> "
}
} else if context.args.extra.iter().any(|arg| arg.starts_with("transaction_id=")) {
"*> "
// Transaction prompt (PROMPT3)
if let Some(custom_prompt) = &context.prompt3 {
custom_prompt.as_str()
} else {
"*> "
}
} else {
"=> "
// Normal prompt (PROMPT1)
if let Some(custom_prompt) = &context.prompt1 {
custom_prompt.as_str()
} else {
"=> "
}
};
let readline = rl.readline(prompt);

Expand All @@ -75,6 +92,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

buffer += "\n";
if !line.is_empty() {
// Check if this is a meta-command (backslash command)
if line.trim().starts_with('\\') {
if let Err(e) = handle_meta_command(&mut context, line.trim()) {
eprintln!("Error processing meta-command: {}", e);
}
buffer.clear();
continue;
}

let queries = try_split_queries(&buffer).unwrap_or_default();

if !queries.is_empty() {
Expand Down
Loading