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
//! Hinter for reedline - provides inline hints based on history
use nu_ansi_term::{Color, Style};
use reedline::{Hinter, History};
/// MongoDB hinter for reedline
pub struct MongoHinter {
/// Style for hints
style: Style,
/// Current hint text
current_hint: String,
}
impl MongoHinter {
/// Create a new MongoDB hinter with default style
///
/// # Returns
/// * `Self` - New hinter
pub fn new() -> Self {
Self {
style: Style::new().italic().fg(Color::DarkGray),
current_hint: String::new(),
}
}
}
impl Default for MongoHinter {
fn default() -> Self {
Self::new()
}
}
impl Hinter for MongoHinter {
/// Provide a hint for the current line
///
/// # Arguments
/// * `line` - The current input line
/// * `pos` - Cursor position
/// * `history` - Command history
/// * `use_ansi_coloring` - Whether to use ANSI colors
/// * `_cwd` - Current working directory (unused)
///
/// # Returns
/// * `String` - Hint text to display after the cursor
fn handle(
&mut self,
line: &str,
pos: usize,
history: &dyn History,
use_ansi_coloring: bool,
_cwd: &str,
) -> String {
// Clear previous hint
self.current_hint.clear();
// Only provide hints if cursor is at the end of the line
if pos != line.len() {
return String::new();
}
// Don't hint for empty lines
if line.trim().is_empty() {
return String::new();
}
// Search history for matching commands
let search_result = history
.search(reedline::SearchQuery::last_with_prefix(
line.to_string(),
None,
))
.ok()
.and_then(|results| results.into_iter().next());
if let Some(history_item) = search_result {
let history_line = history_item.command_line.as_str();
// Only show hint if history item is longer than current input
if history_line.len() > line.len() && history_line.starts_with(line) {
let hint = &history_line[line.len()..];
// Store the complete hint for later use
self.current_hint = hint.to_string();
if use_ansi_coloring {
return self.style.paint(hint).to_string();
} else {
return hint.to_string();
}
}
}
String::new()
}
/// Return the next hint token
///
/// # Returns
/// * `String` - Next hint token
fn next_hint_token(&self) -> String {
String::new()
}
/// Return the complete hint
///
/// # Returns
/// * `String` - Complete hint text
fn complete_hint(&self) -> String {
self.current_hint.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
use reedline::FileBackedHistory;
use std::path::PathBuf;
fn create_test_history() -> Box<dyn History> {
// Create a temporary in-memory history for testing
Box::new(
FileBackedHistory::with_file(100, PathBuf::from("/tmp/test_history.txt"))
.unwrap_or_else(|_| FileBackedHistory::new(100).expect("Failed to create history")),
)
}
#[test]
fn test_new_hinter() {
let hinter = MongoHinter::new();
assert_eq!(hinter.next_hint_token(), String::new());
}
#[test]
fn test_empty_line_no_hint() {
let mut hinter = MongoHinter::new();
let history = create_test_history();
let hint = hinter.handle("", 0, history.as_ref(), true, "/tmp");
assert_eq!(hint, "");
}
#[test]
fn test_cursor_not_at_end_no_hint() {
let mut hinter = MongoHinter::new();
let history = create_test_history();
let hint = hinter.handle("db.users", 2, history.as_ref(), true, "/tmp");
assert_eq!(hint, "");
}
#[test]
fn test_hint_token() {
let hinter = MongoHinter::new();
assert_eq!(hinter.next_hint_token(), "");
}
#[test]
fn test_default() {
let hinter = MongoHinter::default();
assert_eq!(hinter.next_hint_token(), String::new());
}
}