arf-console 0.6.0

A cross-platform R console written in Rust
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! Meta command processing.

use crate::completion::path::expand_tilde;
use crate::config::{RSourceStatus, ReprexMode};
use crate::external::formatter;
use crate::history::{HistoryRuntime, HistoryStore};
use crate::pager::HistoryDbMode;
use std::path::PathBuf;

use super::reprex::ReprexRuntime;
use super::shell::confirm_action;
use super::state::PromptRuntimeConfig;
use super::{ARF_PREFIX, arf_println};

/// Result of processing a meta command.
pub enum MetaCommandResult {
    /// Command was handled, continue with new prompt
    Handled,
    /// User wants to exit
    Exit,
    /// Unknown command
    Unknown(String),
    /// Shell command was executed inline (for :system)
    ShellExecuted,
    /// Restart the process with optional R version
    Restart(Option<String>),
    /// Open the help browser with the given query (caller runs pager)
    ShowHelpBrowser(String),
    /// Display session info (caller runs pager)
    ShowSessionInfo,
    /// Display changelog (caller runs pager)
    ShowChangelog,
    /// Open the history browser (caller runs pager)
    ShowHistoryBrowser {
        store: HistoryStore,
        mode: HistoryDbMode,
    },
    /// Clear stores after the caller has finalized the command provenance.
    ClearHistory {
        stores: Vec<(&'static str, HistoryStore)>,
    },
    /// Display history schema (caller runs pager)
    ShowHistorySchema,
}

/// Process a meta command (starting with `:`) and return the result.
#[allow(clippy::too_many_arguments)]
pub fn process_meta_command(
    input: &str,
    prompt_config: &mut PromptRuntimeConfig,
    reprex: &mut ReprexRuntime,
    r_history: &HistoryRuntime,
    shell_history: &HistoryRuntime,
    r_source_status: &RSourceStatus,
    dir_stack: &mut Vec<PathBuf>,
    history_session_id: Option<i64>,
    r_home: Option<&std::path::Path>,
) -> Option<MetaCommandResult> {
    let trimmed = input.trim();
    if !trimmed.starts_with(':') {
        return None;
    }

    let parts: Vec<&str> = trimmed[1..].split_whitespace().collect();
    let cmd = parts.first().copied().unwrap_or("");

    match cmd {
        "reprex" if parts.len() == 2 => match parts[1] {
            "on" => {
                reprex.set_mode(ReprexMode::On);
                arf_println!("Reprex: on");
                Some(MetaCommandResult::Handled)
            }
            "off" => {
                reprex.set_mode(ReprexMode::Off);
                arf_println!("Reprex: off");
                Some(MetaCommandResult::Handled)
            }
            "format" => {
                if reprex.mode != ReprexMode::Format {
                    let resolved = formatter::resolve_formatter(reprex.formatter_selector);
                    if let Some(backend) = resolved {
                        reprex.formatter = Some(backend);
                        reprex.set_mode(ReprexMode::Format);
                        arf_println!("Reprex: format");
                    } else {
                        arf_println!(
                            "{}",
                            formatter::unavailable_message(
                                reprex.formatter_selector,
                                formatter::FormatterUnavailableContext::MetaCommand
                            )
                        );
                    }
                } else {
                    // Repeating the command is idempotent and does not probe
                    // the formatter again.
                    reprex.set_mode(ReprexMode::Format);
                    arf_println!("Reprex: format");
                }
                Some(MetaCommandResult::Handled)
            }
            _ => {
                arf_println!("Usage: :reprex on|off|format");
                Some(MetaCommandResult::Handled)
            }
        },
        "reprex" => {
            arf_println!("Usage: :reprex on|off|format");
            Some(MetaCommandResult::Handled)
        }
        "shell" => {
            prompt_config.set_shell(true);
            arf_println!("Shell mode enabled. Type :r to return to R.");
            Some(MetaCommandResult::Handled)
        }
        "r" | "R" => {
            if prompt_config.is_shell_enabled() {
                prompt_config.set_shell(false);
                arf_println!("Returned to R mode.");
            } else {
                arf_println!("Already in R mode.");
            }
            Some(MetaCommandResult::Handled)
        }
        "system" => {
            // Execute the rest of the input as a shell command
            let shell_cmd = trimmed[1..].strip_prefix("system").unwrap_or("").trim();
            if shell_cmd.is_empty() {
                arf_println!("Usage: :system <command>");
            } else {
                if let Some(hint) = dir_command_hint(shell_cmd) {
                    arf_println!("{}", hint);
                }
                super::shell::execute_shell_command(shell_cmd);
            }
            Some(MetaCommandResult::ShellExecuted)
        }
        "restart" | "restart!" => {
            let force = cmd == "restart!";
            if force
                || confirm_action(&format!(
                    "{} Restart R session? Current session will be lost.",
                    ARF_PREFIX
                ))
            {
                arf_println!("Restarting R session...");
                Some(MetaCommandResult::Restart(None))
            } else {
                arf_println!("Restart cancelled.");
                Some(MetaCommandResult::Handled)
            }
        }
        "switch" | "switch!" => {
            // :switch requires rig to be enabled at startup
            if !r_source_status.rig_enabled() {
                arf_println!("Error: :switch requires rig to be available at startup.");
                arf_println!(
                    r#"Start arf with r_source = "auto" (with rig installed) or r_source = "rig"."#
                );
                return Some(MetaCommandResult::Handled);
            }

            // Extract the complete version argument, since version ranges may
            // contain spaces (for example, ">=4.3, <5.0").
            let version = trimmed[1..]
                .trim_start()
                .strip_prefix(cmd)
                .map(str::trim)
                .filter(|version| !version.is_empty())
                .map(str::to_owned);
            if version.is_none() {
                arf_println!("Usage: :{cmd} <version>");
                arf_println!("Example: :{cmd} 4.4 or :{cmd} release");
                return Some(MetaCommandResult::Handled);
            }
            let force = cmd == "switch!";
            let ver = version.as_ref().unwrap();
            if force || confirm_action(&format!("Restart with R {}?", ver)) {
                arf_println!("Restarting with R {}...", ver);
                Some(MetaCommandResult::Restart(version))
            } else {
                arf_println!("Switch cancelled.");
                Some(MetaCommandResult::Handled)
            }
        }
        "history" => {
            let subcmd = parts.get(1).copied().unwrap_or("");
            match subcmd {
                "browse" => {
                    let target = parts.get(2).copied().unwrap_or("");
                    process_history_browse(
                        r_history,
                        shell_history,
                        target,
                        prompt_config.is_shell_enabled(),
                    )
                }
                "clear" => {
                    let target = parts.get(2).copied().unwrap_or("");
                    process_history_clear(
                        r_history,
                        shell_history,
                        target,
                        prompt_config.is_shell_enabled(),
                    )
                }
                "schema" => Some(MetaCommandResult::ShowHistorySchema),
                "" => {
                    arf_println!("Usage: :history <subcommand>");
                    println!("#   browse - Browse and manage command history");
                    println!("#   clear  - Clear command history");
                    println!("#   schema - Display database schema and R examples");
                    Some(MetaCommandResult::Handled)
                }
                _ => {
                    arf_println!(
                        "Unknown history subcommand: {}. Use :history for help",
                        subcmd
                    );
                    Some(MetaCommandResult::Handled)
                }
            }
        }
        "help" | "h" => {
            // Fuzzy help search for R documentation
            // Inspired by the felp package: https://github.com/atusy/felp
            let query = parts.get(1..).map(|p| p.join(" ")).unwrap_or_default();
            Some(MetaCommandResult::ShowHelpBrowser(query))
        }
        "info" | "session" => Some(MetaCommandResult::ShowSessionInfo),
        "changelog" => Some(MetaCommandResult::ShowChangelog),
        "cd" => {
            let path_arg = trimmed[1..].strip_prefix("cd").unwrap_or("").trim();
            match meta_cd(path_arg) {
                Ok(cwd) => arf_println!("{}", cwd.display()),
                Err(e) => arf_println!("cd: {}", e),
            }
            Some(MetaCommandResult::Handled)
        }
        "pushd" => {
            let path_arg = trimmed[1..].strip_prefix("pushd").unwrap_or("").trim();
            match meta_pushd(dir_stack, path_arg) {
                Ok(cwd) => arf_println!("{}", cwd.display()),
                Err(e) => arf_println!("pushd: {}", e),
            }
            Some(MetaCommandResult::Handled)
        }
        "popd" => {
            match meta_popd(dir_stack) {
                Ok(cwd) => arf_println!("{}", cwd.display()),
                Err(e) => arf_println!("popd: {}", e),
            }
            Some(MetaCommandResult::Handled)
        }
        "ipc" => {
            let subcmd = parts.get(1).copied().unwrap_or("status");
            match subcmd {
                "start" => match crate::ipc::start_server(
                    None,
                    r_home.map(|path| path.display().to_string()),
                    None,
                    history_session_id,
                    crate::ipc::session::SessionType::Interactive,
                ) {
                    Ok(session) => {
                        arf_println!("IPC server started: {}", session.socket_path)
                    }
                    Err(e) => arf_println!("Failed to start IPC server: {}", e),
                },
                "stop" => {
                    crate::ipc::stop_server();
                    arf_println!("IPC server stopped.");
                }
                "status" => {
                    let sessions = crate::ipc::session::list_sessions();
                    let my_pid = std::process::id();
                    let my_session = sessions.iter().find(|s| s.pid == my_pid);
                    if let Some(session) = my_session {
                        arf_println!("IPC server is running.");
                        println!("#   Socket: {}", session.socket_path);
                        println!("#   PID:    {}", session.pid);
                    } else {
                        arf_println!(
                            "IPC server is not running. Use :ipc start or --with-ipc flag."
                        );
                    }
                }
                "send-policy" => match parts.get(2).copied() {
                    Some("allow") => {
                        crate::ipc::set_send_policy_allow(true);
                        arf_println!("IPC send policy: allow");
                    }
                    Some("prompt") => {
                        crate::ipc::set_send_policy_allow(false);
                        arf_println!("IPC send policy: prompt");
                    }
                    _ => arf_println!("Usage: :ipc send-policy prompt|allow"),
                },
                _ => {
                    arf_println!(
                        "Unknown :ipc subcommand. Available: start, stop, status, send-policy"
                    );
                }
            }
            Some(MetaCommandResult::Handled)
        }
        "commands" | "cmds" => {
            arf_println!("Available commands:");
            println!("#   :help          - Search R help");
            println!("#   :info          - Show session information");
            println!("#   :shell         - Enter shell mode (input goes to system shell)");
            println!("#   :r             - Return to R mode (from shell mode)");
            println!("#   :system <cmd>  - Execute a single system command");
            println!("#   :cd <path>     - Change working directory");
            println!("#   :pushd <path>  - Push directory and change to it");
            println!("#   :popd          - Pop directory from stack");
            println!("#   :reprex <on|off|format> - Set reprex mode");
            println!("#   :history       - History management (browse, clear, schema)");
            println!("#   :restart       - Restart R session");
            println!("#   :restart!      - Restart without confirmation");
            println!("#   :switch <ver>  - Restart with different R version (requires rig)");
            println!("#   :switch! <ver> - Switch without confirmation");
            println!(
                "#   :ipc           - IPC server management (start, stop, status, send-policy)"
            );
            println!("#   :changelog     - Show arf changelog");
            println!("#   :commands      - Show this list");
            println!("#   :quit          - Exit arf");
            Some(MetaCommandResult::Handled)
        }
        "quit" | "exit" => Some(MetaCommandResult::Exit),
        "" => {
            // Just ":" with nothing after - show help hint
            arf_println!("Type :commands for available commands");
            Some(MetaCommandResult::Handled)
        }
        _ => Some(MetaCommandResult::Unknown(cmd.to_string())),
    }
}

/// Process :history browse command.
fn process_history_browse(
    r_history: &HistoryRuntime,
    shell_history: &HistoryRuntime,
    target: &str,
    is_shell_mode: bool,
) -> Option<MetaCommandResult> {
    // Determine which database to browse
    let (mode, runtime) = match target {
        "" => {
            // Default: browse based on current mode
            if is_shell_mode {
                (HistoryDbMode::Shell, shell_history)
            } else {
                (HistoryDbMode::R, r_history)
            }
        }
        "r" | "R" => (HistoryDbMode::R, r_history),
        "shell" => (HistoryDbMode::Shell, shell_history),
        _ => {
            arf_println!("Unknown target: {}. Use r or shell.", target);
            return Some(MetaCommandResult::Handled);
        }
    };

    let Some(store) = runtime.store() else {
        arf_println!("History is unavailable for {} mode.", mode.display_name());
        return Some(MetaCommandResult::Handled);
    };

    Some(MetaCommandResult::ShowHistoryBrowser { store, mode })
}

/// Process :history clear command.
fn process_history_clear(
    r_history: &HistoryRuntime,
    shell_history: &HistoryRuntime,
    target: &str,
    is_shell_mode: bool,
) -> Option<MetaCommandResult> {
    // Determine what to clear based on target
    let clear_target = match target {
        "" => {
            // Default: clear based on current mode
            if is_shell_mode { "shell" } else { "r" }
        }
        "r" | "R" => "r",
        "shell" => "shell",
        "all" => "all",
        _ => {
            arf_println!("Unknown target: {}. Use r, shell, or all.", target);
            return Some(MetaCommandResult::Handled);
        }
    };

    // Collect paths to clear based on target
    let runtimes: Vec<(&str, &HistoryRuntime)> = match clear_target {
        "r" => vec![("R", r_history)],
        "shell" => vec![("Shell", shell_history)],
        "all" => {
            vec![("R", r_history), ("Shell", shell_history)]
        }
        _ => unreachable!(),
    };

    let stores = dedup_history_stores(
        runtimes
            .into_iter()
            .filter_map(|(name, runtime)| runtime.store().map(|store| (name, store)))
            .collect(),
    );

    if stores.is_empty() {
        arf_println!("History is unavailable.");
        return Some(MetaCommandResult::Handled);
    }

    // Count total entries across all targeted databases
    let mut total_count = 0i64;
    let mut counts: Vec<(&str, i64)> = Vec::new();

    for (name, store) in &stores {
        match store.count_all() {
            Ok(count) => {
                counts.push((name, count));
                total_count += count;
            }
            Err(error) => arf_println!("Failed to read {} history: {}", name, error),
        }
    }

    if total_count == 0 {
        arf_println!("History is already empty.");
        return Some(MetaCommandResult::Handled);
    }

    // Show what will be cleared
    if counts.len() == 1 {
        arf_println!("{} history: {} entries", counts[0].0, counts[0].1);
    } else {
        for (name, count) in &counts {
            arf_println!("{} history: {} entries", name, count);
        }
        arf_println!("Total: {} entries", total_count);
    }

    // Confirm before clearing
    let prompt = format!("{} Clear {} history entries?", ARF_PREFIX, total_count);
    if !confirm_action(&prompt) {
        arf_println!("Cancelled.");
        return Some(MetaCommandResult::Handled);
    }

    Some(MetaCommandResult::ClearHistory { stores })
}

fn dedup_history_stores(stores: Vec<(&str, HistoryStore)>) -> Vec<(&str, HistoryStore)> {
    let mut unique: Vec<(&str, HistoryStore)> = Vec::new();
    for (name, store) in stores {
        if unique
            .iter()
            .any(|(_, existing)| existing.same_owner(&store))
        {
            continue;
        }
        unique.push((name, store));
    }
    unique
}

/// Change the current working directory.
///
/// If `path_arg` is empty, changes to the home directory.
/// Tilde (`~`) is expanded to the home directory.
pub(crate) fn meta_cd(path_arg: &str) -> Result<PathBuf, String> {
    let target = if path_arg.is_empty() {
        dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?
    } else {
        PathBuf::from(expand_tilde(path_arg))
    };
    std::env::set_current_dir(&target).map_err(|e| format!("{}: {}", target.display(), e))?;
    std::env::current_dir().map_err(|e| e.to_string())
}

/// Push the current directory onto the stack and change to a new directory.
///
/// Requires a path argument. Unlike bash's `pushd` (which swaps the top two
/// stack entries when called without arguments), this always requires an
/// explicit destination.
pub(crate) fn meta_pushd(dir_stack: &mut Vec<PathBuf>, path_arg: &str) -> Result<PathBuf, String> {
    if path_arg.is_empty() {
        return Err("Usage: :pushd <path>".to_string());
    }
    let current = std::env::current_dir().map_err(|e| e.to_string())?;
    let new_dir = meta_cd(path_arg)?;
    dir_stack.push(current);
    Ok(new_dir)
}

/// Pop the top directory from the stack and change to it.
pub(crate) fn meta_popd(dir_stack: &mut Vec<PathBuf>) -> Result<PathBuf, String> {
    let target = dir_stack
        .last()
        .cloned()
        .ok_or_else(|| "Directory stack is empty".to_string())?;
    std::env::set_current_dir(&target).map_err(|e| format!("{}", e))?;
    dir_stack.pop();
    std::env::current_dir().map_err(|e| e.to_string())
}

/// Return a hint message if the shell command is a directory navigation command
/// that won't work as expected in a subprocess.
pub(crate) fn dir_command_hint(shell_cmd: &str) -> Option<&'static str> {
    match shell_cmd.split_whitespace().next()? {
        "cd" => Some("Hint: Use the :cd meta command instead to change directory."),
        "pushd" => Some("Hint: Use the :pushd meta command instead to change directory."),
        "popd" => Some("Hint: Use the :popd meta command instead to restore directory."),
        _ => None,
    }
}

#[cfg(test)]
#[path = "meta_command/tests.rs"]
mod tests;