Principle:Ollama Ollama CLI Format Utility
| Knowledge Sources | |
|---|---|
| Domains | CLI, Formatting |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Format Utilities provide human-readable formatting functions for the Ollama CLI, converting raw numeric values into user-friendly representations for byte sizes, time durations, and tabular data display.
Core Concepts
Byte Size Formatting
The byte formatter converts raw byte counts into human-readable strings using appropriate SI or binary unit suffixes (KB, MB, GB, TB). The formatter selects the most appropriate unit to keep the numeric portion readable (e.g., displaying "7.2 GB" rather than "7200000000 bytes" or "0.0072 TB"). This is used extensively in the CLI for displaying model sizes, download progress, and memory usage.
Time Duration Formatting
The time formatter converts durations into human-readable relative or absolute formats. For inference statistics, it formats nanosecond-precision timing data into seconds or milliseconds. For model listing, it may format timestamps as relative durations (e.g., "2 hours ago") to give users an intuitive sense of recency without requiring them to mentally parse absolute timestamps.
Table Rendering
The table formatter constructs aligned columnar output for the CLI's list and status commands. It handles dynamic column width calculation based on content, proper alignment (left for text, right for numbers), and truncation of long values to fit terminal width. Table rendering provides a consistent visual format across all CLI output that presents structured data.
Consistent Presentation
The formatting utilities enforce consistent presentation conventions across the entire CLI surface. All byte sizes use the same unit selection algorithm, all durations use the same formatting rules, and all tabular output uses the same alignment conventions. This consistency reduces cognitive load for users who interact with multiple CLI commands.
Implementation Notes
The formatting functions are implemented in the format/ package: byte formatting in format/bytes.go, time formatting in format/time.go, and general formatting utilities in format/format.go. These functions are consumed by the CLI command implementations in cmd/ to format output for model listing, pull/push progress, and inference statistics. Comprehensive tests in format/bytes_test.go, format/time_test.go, and format/format_test.go verify correct formatting across edge cases.