0
# Command Line Editing
1
2
Core line editing functionality for building interactive command-line applications with full GNU Readline support. These functions provide the foundation for input handling, buffer manipulation, and display control.
3
4
## Capabilities
5
6
### Configuration and Initialization
7
8
Parse readline configuration and initialize the readline library with custom settings.
9
10
```python { .api }
11
def parse_and_bind(string: str):
12
"""
13
Execute the init line provided in the string argument.
14
15
Parameters:
16
- string: Readline initialization command (e.g., "tab: complete", "set bell-style none")
17
"""
18
19
def read_init_file(filename: str = None):
20
"""
21
Execute a readline initialization file.
22
23
Parameters:
24
- filename: Path to init file (default: ~/.inputrc or uses last filename)
25
"""
26
```
27
28
### Usage Examples
29
30
```python
31
import gnureadline
32
33
# Configure readline behavior
34
gnureadline.parse_and_bind("tab: complete")
35
gnureadline.parse_and_bind("set bell-style none")
36
gnureadline.parse_and_bind("set completion-ignore-case on")
37
38
# Load configuration from file
39
gnureadline.read_init_file("~/.inputrc")
40
gnureadline.read_init_file("/etc/inputrc")
41
```
42
43
### Buffer Manipulation
44
45
Access and modify the current line buffer contents during input processing.
46
47
```python { .api }
48
def get_line_buffer() -> str:
49
"""
50
Get the current contents of the line buffer.
51
52
Returns:
53
str: Current line buffer content
54
"""
55
56
def insert_text(string: str):
57
"""
58
Insert text into the line buffer at the cursor position.
59
60
Parameters:
61
- string: Text to insert
62
"""
63
```
64
65
### Usage Examples
66
67
```python
68
import gnureadline
69
70
# Set up a completion function that manipulates the buffer
71
def smart_completer(text: str, state: int):
72
if state == 0:
73
# Get current buffer to provide context-aware completion
74
buffer = gnureadline.get_line_buffer()
75
print(f"Current buffer: '{buffer}'")
76
77
# Insert helpful text
78
if text == "help":
79
gnureadline.insert_text(" - available commands: ls, cd, exit")
80
81
return None
82
83
gnureadline.set_completer(smart_completer)
84
```
85
86
### Display Control
87
88
Control how readline displays the current line and prompt.
89
90
```python { .api }
91
def redisplay():
92
"""Force redisplay of the current line."""
93
```
94
95
### Usage Examples
96
97
```python
98
import gnureadline
99
import signal
100
101
# Redisplay after signal handling
102
def signal_handler(signum, frame):
103
print("\nCaught signal, refreshing display...")
104
gnureadline.redisplay()
105
106
signal.signal(signal.SIGWINCH, signal_handler)
107
```
108
109
## Advanced Configuration Examples
110
111
### Custom Key Bindings
112
113
```python
114
import gnureadline
115
116
# Set up custom key bindings
117
gnureadline.parse_and_bind('"\\C-l": clear-screen')
118
gnureadline.parse_and_bind('"\\C-u": unix-line-discard')
119
gnureadline.parse_and_bind('"\\C-w": unix-word-rubout')
120
121
# Vi-style editing mode
122
gnureadline.parse_and_bind("set editing-mode vi")
123
124
# Emacs-style editing mode (default)
125
gnureadline.parse_and_bind("set editing-mode emacs")
126
```
127
128
### Multi-line Input Handling
129
130
```python
131
import gnureadline
132
133
# Configure for multi-line input
134
gnureadline.parse_and_bind("set horizontal-scroll-mode off")
135
gnureadline.parse_and_bind("set print-completions-horizontally off")
136
137
def get_multiline_input(prompt=">>> "):
138
"""Get multi-line input with proper display handling."""
139
lines = []
140
141
while True:
142
try:
143
if not lines:
144
line = input(prompt)
145
else:
146
line = input("... ")
147
148
lines.append(line)
149
150
# Check for continuation
151
if line.endswith("\\"):
152
continue
153
else:
154
break
155
156
except EOFError:
157
break
158
159
# Force redisplay after multi-line input
160
gnureadline.redisplay()
161
return "\n".join(lines)
162
```