Skip to content

Commit 8381b53

Browse files
feat: Implement \set PROMPT1, PROMPT2, and PROMPT3 commands for Firebolt CLI (#4)
* Implement \set PROMPT1, PROMPT2, and PROMPT3 commands for Firebolt CLI * Refactor parse prompt functions
1 parent b2cc76a commit 8381b53

File tree

3 files changed

+358
-4
lines changed

3 files changed

+358
-4
lines changed

src/context.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,32 @@ pub struct Context {
1313
pub args: Args,
1414
pub url: String,
1515
pub sa_token: Option<ServiceAccountToken>,
16+
pub prompt1: Option<String>,
17+
pub prompt2: Option<String>,
18+
pub prompt3: Option<String>,
1619
}
1720

1821
impl Context {
1922
pub fn new(args: Args) -> Self {
2023
let url = get_url(&args);
21-
Self { args, url, sa_token: None }
24+
Self { args, url, sa_token: None, prompt1: None, prompt2: None, prompt3: None }
2225
}
2326

2427
pub fn update_url(&mut self) {
2528
self.url = get_url(&self.args);
2629
}
30+
31+
pub fn set_prompt1(&mut self, prompt: String) {
32+
self.prompt1 = Some(prompt);
33+
}
34+
35+
pub fn set_prompt2(&mut self, prompt: String) {
36+
self.prompt2 = Some(prompt);
37+
}
38+
39+
pub fn set_prompt3(&mut self, prompt: String) {
40+
self.prompt3 = Some(prompt);
41+
}
2742
}
2843

2944
#[cfg(test)]

src/main.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ use rustyline::{config::Configurer, error::ReadlineError, Cmd, DefaultEditor, Ev
33
mod args;
44
mod auth;
55
mod context;
6+
mod meta_commands;
67
mod query;
78
mod utils;
89

910
use args::get_args;
1011
use auth::maybe_authenticate;
1112
use context::Context;
13+
use meta_commands::handle_meta_command;
1214
use query::{query, try_split_queries};
1315
use utils::history_path;
1416

@@ -57,11 +59,26 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
5759
let mut buffer: String = String::new();
5860
loop {
5961
let prompt = if !buffer.trim_start().is_empty() {
60-
"~> "
62+
// Continuation prompt (PROMPT2)
63+
if let Some(custom_prompt) = &context.prompt2 {
64+
custom_prompt.as_str()
65+
} else {
66+
"~> "
67+
}
6168
} else if context.args.extra.iter().any(|arg| arg.starts_with("transaction_id=")) {
62-
"*> "
69+
// Transaction prompt (PROMPT3)
70+
if let Some(custom_prompt) = &context.prompt3 {
71+
custom_prompt.as_str()
72+
} else {
73+
"*> "
74+
}
6375
} else {
64-
"=> "
76+
// Normal prompt (PROMPT1)
77+
if let Some(custom_prompt) = &context.prompt1 {
78+
custom_prompt.as_str()
79+
} else {
80+
"=> "
81+
}
6582
};
6683
let readline = rl.readline(prompt);
6784

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

7693
buffer += "\n";
7794
if !line.is_empty() {
95+
// Check if this is a meta-command (backslash command)
96+
if line.trim().starts_with('\\') {
97+
if let Err(e) = handle_meta_command(&mut context, line.trim()) {
98+
eprintln!("Error processing meta-command: {}", e);
99+
}
100+
buffer.clear();
101+
continue;
102+
}
103+
78104
let queries = try_split_queries(&buffer).unwrap_or_default();
79105

80106
if !queries.is_empty() {

0 commit comments

Comments
 (0)