Implementation:Ollama Ollama Readline
| Knowledge Sources | |
|---|---|
| Domains | CLI, Terminal UI |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Main readline implementation that provides an interactive line editing interface with keyboard input handling, history navigation, and multi-line editing support.
Description
The Instance struct combines a Prompt, Terminal, and History. Readline() enters raw terminal mode and processes keystrokes character-by-character, handling control keys (Ctrl+A/E for start/end, Ctrl+W for word delete, Ctrl+K for kill line, Ctrl+C for interrupt, Ctrl+Z for suspend, Ctrl+L for clear screen), escape sequences (arrow keys, word movement, home/end), history navigation (up/down arrows), bracketed paste detection, and regular character insertion. Supports prefilling the buffer from an external editor.
Usage
The interactive input engine for ollama run, providing a polished terminal experience with readline-like editing capabilities.
Code Reference
Source Location
- Repository: Ollama
- File: readline/readline.go
- Lines: 1-392
Signature
type Prompt struct {
Prompt string
AltPrompt string
Placeholder string
AltPlaceholder string
UseAlt bool
}
type Instance struct {
Prompt *Prompt
Terminal *Terminal
History *History
Pasting bool
Prefill string
}
func New(prompt Prompt) (*Instance, error)
func (i *Instance) Readline() (string, error)
func (i *Instance) HistoryEnable()
func (i *Instance) HistoryDisable()
Import
import "github.com/ollama/ollama/readline"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| prompt | Prompt | Yes | Prompt strings and placeholders for display |
Outputs
| Name | Type | Description |
|---|---|---|
| line | string | The user's input text |
| error | error | io.EOF on Ctrl+D, ErrInterrupt on Ctrl+C |
Usage Examples
rl, err := readline.New(readline.Prompt{
Prompt: ">>> ",
AltPrompt: "... ",
Placeholder: "Send a message (/? for help)",
})
if err != nil {
log.Fatal(err)
}
for {
line, err := rl.Readline()
if err != nil {
break
}
fmt.Println("You said:", line)
}