Implementation:Ggml org Llama cpp Console
| Knowledge Sources | |
|---|---|
| Domains | Terminal, IO |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Implements cross-platform terminal I/O with colored output, line editing, history, and a loading spinner for the CLI chat interface.
Description
This file provides the interactive terminal experience for the llama-cli chat tool. It manages terminal state (raw mode on POSIX via termios, console mode on Windows) with ANSI color code support for different display types (prompt, reasoning, user input, error). It implements a readline function with cursor movement, word-by-word navigation (Ctrl+arrows), history (up/down arrows), multi-line input, and proper UTF-8 handling. The spinner namespace provides an animated loading indicator running on a separate thread. Platform-specific code handles Windows console API and POSIX terminal ioctls. The init and cleanup functions save and restore the terminal state.
Usage
Used by the llama-cli interactive chat tool to provide a user-friendly terminal interface with input editing, history navigation, and colored output.
Code Reference
Source Location
- Repository: Ggml_org_Llama_cpp
- File: common/console.cpp
- Lines: 1-1137
Signature
namespace console {
// Display types for colored output
// DISPLAY_TYPE_RESET, DISPLAY_TYPE_PROMPT, DISPLAY_TYPE_USER_INPUT, etc.
void init(bool use_simple_io, bool use_advanced_display);
void cleanup();
void set_display(display_type type);
// Line editing with history
std::string readline(FILE * in, const std::string & prompt);
// Loading spinner (runs on separate thread)
namespace spinner {
void start();
void stop();
}
}
// ANSI color constants
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define ANSI_BOLD "\x1b[1m"
Import
#include "console.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| use_simple_io | bool | Yes | If true, uses basic I/O without raw terminal mode (for non-interactive use) |
| use_advanced_display | bool | Yes | If true, enables ANSI color codes and advanced terminal features |
| type | display_type | Yes | The display type to set (prompt, user input, reasoning, error, etc.) |
| prompt | std::string | Yes | Prompt string displayed before user input in readline |
Outputs
| Name | Type | Description |
|---|---|---|
| readline result | std::string | User-entered text line with full editing support (history, cursor movement) |
| colored output | terminal output | ANSI-colored text written to stdout for different content types |
Usage Examples
#include "console.h"
// Initialize console with advanced features
console::init(false /* not simple */, true /* advanced display */);
// Set color for model output
console::set_display(console::DISPLAY_TYPE_PROMPT);
printf("Assistant: Hello!\n");
// Read user input with editing and history
console::set_display(console::DISPLAY_TYPE_USER_INPUT);
std::string input = console::readline(stdin, "> ");
// Show loading spinner while processing
console::spinner::start();
// ... process ...
console::spinner::stop();
// Restore terminal state
console::cleanup();