1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Interactive mode for kelora
// Provides a readline-based REPL for running kelora commands
use anyhow::Result;
use rustyline::completion::{Completer, FilenameCompleter};
use rustyline::error::ReadlineError;
use rustyline::highlight::Highlighter;
use rustyline::hint::Hinter;
use rustyline::validate::Validator;
use rustyline::{CompletionType, Config, Context, Editor, Helper};
use std::path::PathBuf;
/// Helper for interactive mode with file completion
#[derive(Default)]
struct KeloraHelper {
completer: FilenameCompleter,
}
impl Completer for KeloraHelper {
type Candidate = <FilenameCompleter as Completer>::Candidate;
fn complete(
&self,
line: &str,
pos: usize,
ctx: &Context<'_>,
) -> rustyline::Result<(usize, Vec<Self::Candidate>)> {
self.completer.complete(line, pos, ctx)
}
}
impl Hinter for KeloraHelper {
type Hint = String;
}
impl Highlighter for KeloraHelper {}
impl Validator for KeloraHelper {}
impl Helper for KeloraHelper {}
/// Run interactive mode
/// This provides a readline-based prompt where users can enter kelora commands
/// without dealing with shell quoting issues (especially helpful on Windows)
pub fn run_interactive_mode() -> Result<()> {
// Configure editor with file completion
let config = Config::builder()
.completion_type(CompletionType::List)
.build();
let helper = KeloraHelper::default();
let mut rl = Editor::with_config(config)?;
rl.set_helper(Some(helper));
// Set up history file
let history_path = get_history_path();
if let Some(ref path) = history_path {
// Ignore errors when loading history (file might not exist yet)
let _ = rl.load_history(path);
}
println!("Kelora Interactive Mode — :quit to exit, :help for help\n");
let mut consecutive_interrupts = 0;
loop {
let readline = rl.readline("kelora> ");
match readline {
Ok(line) => {
// Reset interrupt counter on successful input
consecutive_interrupts = 0;
let trimmed = line.trim();
// Skip empty lines
if trimmed.is_empty() {
continue;
}
// Add to history
let _ = rl.add_history_entry(trimmed);
// Check for REPL commands (colon-prefixed)
if trimmed == ":exit" || trimmed == ":quit" || trimmed == ":q" {
break;
}
if trimmed == ":help" {
let eof_key = if cfg!(windows) { "Ctrl-Z" } else { "Ctrl-D" };
println!("Interactive mode - enter kelora commands without 'kelora' prefix");
println!();
println!(" TAB Complete files/directories");
println!(" *.log Glob patterns auto-expand");
println!(" 'foo bar' Use quotes when args contain spaces");
println!(" -h Quick help (or --help for full)");
println!();
println!(" Ctrl-C Cancel running command (press twice to exit)");
println!(" :quit Exit (or :q, :exit, {})", eof_key);
println!();
println!("Example: -j mylog.json --filter 'e.status >= 500'");
continue;
}
// Parse the command line
match parse_and_execute_command(trimmed) {
Ok(()) => {
// Command executed successfully
}
Err(e) => {
eprintln!("Error: {}", e);
}
}
}
Err(ReadlineError::Interrupted) => {
// Ctrl-C at the prompt - Node.js style two-press-to-quit
consecutive_interrupts += 1;
if consecutive_interrupts >= 2 {
println!("\nExiting...");
break;
} else {
println!("(To exit, press Ctrl-C again or type :quit)");
continue;
}
}
Err(ReadlineError::Eof) => {
// Ctrl-D - exit
break;
}
Err(err) => {
eprintln!("Error reading line: {}", err);
break;
}
}
}
// Save history
if let Some(ref path) = history_path {
let _ = rl.save_history(path);
}
Ok(())
}
/// Parse a command line and execute it
fn parse_and_execute_command(line: &str) -> Result<()> {
// Parse the line using shell-words to handle quoting
let words = shell_words::split(line)?;
if words.is_empty() {
return Ok(());
}
// Expand globs in the arguments
let expanded_args = expand_globs(&words)?;
// Build the full argument vector (prepend program name)
let mut args = vec!["kelora".to_string()];
args.extend(expanded_args);
// Execute the command by calling the main processing function
// We'll need to refactor main.rs to expose this functionality
execute_kelora_command(args)?;
Ok(())
}
/// Expand glob patterns in arguments
fn expand_globs(args: &[String]) -> Result<Vec<String>> {
let mut result = Vec::new();
for arg in args {
// Check if this looks like a glob pattern
if arg.contains('*') || arg.contains('?') || arg.contains('[') {
// Try to expand it
let mut matches: Vec<String> = glob::glob(arg)?
.filter_map(|path| path.ok())
.map(|path| path.to_string_lossy().to_string())
.collect();
if matches.is_empty() {
// No matches - keep the original pattern
result.push(arg.clone());
} else {
// Sort for consistent ordering
matches.sort();
result.extend(matches);
}
} else {
// Not a glob pattern - keep as is
result.push(arg.clone());
}
}
Ok(result)
}
/// Execute a kelora command with the given arguments
/// This spawns kelora as a subprocess with the given arguments
fn execute_kelora_command(args: Vec<String>) -> Result<()> {
use std::process::Command;
// Get the current executable path
let exe_path = std::env::current_exe()?;
// Skip the first argument (program name) since Command will add it
let cmd_args = &args[1..];
// Spawn kelora as a subprocess
let status = Command::new(&exe_path).args(cmd_args).status()?;
// Check if the command was successful
if !status.success() {
// The subprocess will have already printed error messages
// We just note that it failed
if let Some(code) = status.code() {
if code != 0 {
// Don't print anything - the error was already shown by the subprocess
}
}
}
Ok(())
}
/// Get the path to the history file
fn get_history_path() -> Option<PathBuf> {
dirs::config_dir().and_then(|mut path| {
path.push("kelora");
// Create the directory if it doesn't exist
if let Err(_e) = std::fs::create_dir_all(&path) {
return None;
}
path.push("interactive_history.txt");
Some(path)
})
}