forked from tom-doerr/path_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_handler.py
More file actions
70 lines (58 loc) · 1.93 KB
/
input_handler.py
File metadata and controls
70 lines (58 loc) · 1.93 KB
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
"""
Input handler for the agent CLI with Vim-like functionality.
"""
from typing import List, Dict, Any, Optional
from rich.console import Console
# Try to import the Vim input module, fall back to regular input if not available
try:
from src.interface.vim_input import get_vim_input
VIM_INPUT_AVAILABLE = True
except ImportError:
VIM_INPUT_AVAILABLE = False
def get_user_input(
console: Console,
prompt: str = "> ",
history: List[str] = None,
vim_mode: bool = True,
config: Dict[str, Any] = None,
) -> str:
"""
Get input from the user with optional Vim-like interface.
Args:
console: Rich console instance
prompt: Input prompt to display
history: Command history
vim_mode: Whether to use Vim-like input mode
config: Configuration dictionary
Returns:
User input string
"""
# Check config for vim_mode setting if provided
if config is not None and "vim_mode" in config:
vim_mode = config["vim_mode"]
try:
if vim_mode and VIM_INPUT_AVAILABLE:
console.print(f"[dim]{prompt}[/dim]", end="")
return get_vim_input(console, history or [])
else:
# Fall back to regular input
return console.input(prompt)
except Exception as e:
console.print(f"[bold red]Error getting input: {str(e)}[/bold red]")
# Fall back to basic input on error
return input(prompt)
def process_input_with_history(
input_text: str, history: List[str], max_history: int = 100
) -> None:
"""
Process input and update history.
Args:
input_text: The input text to process
history: The history list to update
max_history: Maximum history items to keep
"""
if input_text and (not history or input_text != history[-1]):
history.append(input_text)
# Trim history if needed
if len(history) > max_history:
history.pop(0)