Menu

Format String System

Relevant source files

Purpose and Scope

The Format String System is a template engine that controls how module output is rendered in fastfetch. It provides a sophisticated syntax for variable substitution, conditional rendering, color formatting, and text manipulation. Each module defines its available placeholders, and users customize output by writing format strings that reference these placeholders.

For general display options (colors, separators, key widths), see Output Formatting and Display Options. For the module execution framework that invokes format strings, see Module System Architecture.

Sources: src/common/format.c1-414


System Architecture

The format string system consists of three main components: argument registration by modules, parsing and evaluation by the format engine, and rendering utilities for special data types.

Sources: src/common/format.c123-413 src/common/format.c9-59 src/common/format.c69-96


Format Argument Types

Modules register format arguments using the FFformatarg structure. Each argument has a name (optional), a type, and a pointer to the data value. The format engine knows how to convert each type to a string representation.

Supported Argument Types

TypeEnum ValueC TypeDescription
NullFF_FORMAT_ARG_TYPE_NULLN/ANo value, skipped during rendering
IntegerFF_FORMAT_ARG_TYPE_INTint32_t*Signed 32-bit integer
Unsigned IntegerFF_FORMAT_ARG_TYPE_UINTuint32_t*Unsigned 32-bit integer
64-bit UnsignedFF_FORMAT_ARG_TYPE_UINT64uint64_t*Unsigned 64-bit integer
16-bit UnsignedFF_FORMAT_ARG_TYPE_UINT16uint16_t*Unsigned 16-bit integer
8-bit UnsignedFF_FORMAT_ARG_TYPE_UINT8uint8_t*Unsigned 8-bit integer
C StringFF_FORMAT_ARG_TYPE_STRINGconst char*Null-terminated string
String BufferFF_FORMAT_ARG_TYPE_STRBUFFFstrbuf*Dynamic string buffer
FloatFF_FORMAT_ARG_TYPE_FLOATfloat*Single-precision floating point
DoubleFF_FORMAT_ARG_TYPE_DOUBLEdouble*Double-precision floating point
BooleanFF_FORMAT_ARG_TYPE_BOOLbool*Boolean (rendered as "true"/"false")
ListFF_FORMAT_ARG_TYPE_LISTFFlist*List of FFstrbuf (comma-separated)

Sources: src/common/format.c9-59


Placeholder Syntax

Basic Variable Substitution

Format strings use curly braces {} to denote placeholders. The format engine supports three placeholder addressing modes:

SyntaxDescriptionExample
{}Auto-increment counter (1, 2, 3, ...){} @ {} → "CPU @ 3.5 GHz"
{1}Positional index (1-based){2} ({1}) → "3.5 GHz (CPU)"
{name}Named placeholder{cpu-name} @ {freq} → "CPU @ 3.5 GHz"

The argument index resolution logic is implemented in getArgumentIndex():

Sources: src/common/format.c69-96 src/common/format.c307-318

Escape Sequences

SequenceResultPurpose
{{{Literal opening brace
{-}(stop parsing)Truncate output at this point

Sources: src/common/format.c144-149 src/common/format.c164-165


Conditional Rendering

The format system supports conditional blocks that render content only when a specified argument is set (non-zero, non-empty, non-null).

If Conditional: {?name}...{?}

Renders the enclosed content only if the argument name is "set" (has a meaningful value).

Syntax: {?placeholder}content{?}

Example: {name}{?temp} @ {temp}{?} → "CPU @ 45°C" (if temp is set) or "CPU" (if temp is not set)

Not-If Conditional: {/name}...{/}

Renders the enclosed content only if the argument name is NOT set.

Syntax: {/placeholder}content{/}

Example: {name}{/temp} (no temp data){/} → "CPU (no temp data)" (if temp is not set) or "CPU" (if temp is set)

Value Detection Logic

The formatArgSet() function determines whether an argument is "set":

Argument TypeCondition for "Set"
DOUBLEvalue > 0.0
FLOATvalue > 0.0
INTvalue > 0
STRBUFlength > 0
STRINGNon-NULL and not empty
UINT8/UINT16/UINTvalue > 0
BOOLvalue == true
LISTlength > 0

Sources: src/common/format.c199-223 src/common/format.c225-249 src/common/format.c107-121


Color Codes

Color codes allow inline color changes within format strings. The system integrates with fastfetch's color configuration.

Color Syntax

SyntaxDescriptionExample
{#rrggbb}RGB hex color{#ff0000}red text{#}
{#color-name}Named color{#red}red text{#}
{#}Reset to defaultCloses color block

ANSI Escape Generation

When not in pipe mode (!instance.config.display.pipe), the parser generates ANSI escape sequences:

  1. {#rrggbb}\e[<parsed-color>m
  2. {#}\e[0m (FASTFETCH_TEXT_MODIFIER_RESET)

The color parsing is delegated to ffOptionParseColorNoClear(), which translates color specifications into ANSI SGR codes.

Sources: src/common/format.c252-261 src/common/format.c189-196


Text Manipulation

The format system provides several operators for text manipulation: truncation, padding, and substring extraction.

Truncation: {placeholder:length}

Truncates the value to length characters, trimming trailing whitespace.

Example: {name:10} with "Very Long CPU Name" → "Very Long"

Padding

SyntaxAlignmentPadding
{placeholder<length}Left-alignedPad right with spaces
{placeholder>length}Right-alignedPad left with spaces

Example: {name<15} with "CPU" → "CPU "

Ellipsis Truncation: {placeholder:-length}

Truncates to length - 1 characters and appends an ellipsis character (…).

Example: {name:-10} with "Very Long CPU Name" → "Very Long…"

Substring Extraction: {placeholder~start,end}

Extracts characters from index start to end (0-based). Negative indices count from the end.

Syntax:

  • {placeholder~start} → Extract from start to end
  • {placeholder~start,end} → Extract from start to end

Examples:

  • {name~5} with "Hello World" → "World"
  • {name~0,5} with "Hello World" → "Hello"
  • {name~-5} with "Hello World" → "World"

Sources: src/common/format.c320-408


Constants and Environment Variables

The format system supports referencing constants and environment variables through the {$} syntax.

Constants: {$1}, {$-1}

Constants are user-defined values stored in instance.config.display.constants (an FFlist of FFstrbuf). They are indexed 1-based (or negative for counting from the end).

Configuration: Constants are defined in the display.constants array in the configuration file.

Example:

  • Config: "constants": ["My Custom Value", "Another Value"]
  • Format: {$1} → "My Custom Value"
  • Format: {$-1} → "Another Value"

Environment Variables: {$VAR_NAME}

If the placeholder cannot be parsed as a number, it's treated as an environment variable name.

Example: {$HOME} → "/home/username"

Sources: src/common/format.c263-291 src/options/display.c472-480


Module Format String Definitions

Each module defines its available placeholders in the JSON schema. The schema documents the placeholder names, indices, and descriptions for every module.

Example: CPU Module Format Arguments

From the JSON schema, the CPU module supports the following placeholders:

IndexPlaceholderDescription
1{name}CPU name
2{vendor}CPU vendor
3{cores-physical}Physical core count
4{cores-logical}Logical core count
5{cores-online}Online core count
6{freq-base}Base frequency (formatted)
7{freq-max}Max frequency (formatted)
8{temperature}Temperature (formatted)
9{core-types}Logical core count grouped by frequency
10{packages}Processor package count
11{march}X86-64 CPU microarchitecture

Format String Registration Pattern

Modules follow this pattern to register format arguments:

  1. Define FFformatarg array with named placeholders
  2. Pass array to ffPrintFormat() or custom printing function
  3. Format engine resolves placeholders against the array
  4. Output is appended to result buffer

Sources: doc/json_schema.json320-323 doc/json_schema.json276-517


Parsing State Machine

The core parsing loop in ffParseFormatString() implements a state machine that processes the format string character by character, handling nested conditionals and various placeholder types.

Parsing Algorithm

Key State Variables

VariableTypePurpose
argCounteruint32_tAuto-increment counter for {} placeholders
numOpenIfsuint32_tNesting depth of {?} conditionals
numOpenNotIfsuint32_tNesting depth of {/} conditionals
placeholderValueFFstrbufTemporary buffer for placeholder content

Sources: src/common/format.c123-413


Integration with Display Options

The format system reads display configuration from instance.config.display (type FFOptionsDisplay) to control rendering behavior.

Key Display Options Used by Format System

OptionTypeUsage in Format System
pipeboolDisables all ANSI color codes when true
constantsFFlistSource for {$N} constant placeholders
colorOutputFFstrbufDefault output color after color resets
fractionNdigitsint8_tDecimal places for float/double formatting
fractionTrailingZerosenumWhether to keep trailing zeros in floats

Display Option Access Pattern

Sources: src/common/format.c35-38 src/common/format.c192-193 src/common/format.c280-287 src/common/format.c411-412 src/options/display.h35-94


Special Formatting: Percentages and Temperatures

While the core format system handles general text substitution, specialized formatting utilities handle percentages and temperatures with color-coding and visual indicators.

Percentage Formatting

Percentage values can be rendered as:

  • Numeric: 45.3% with optional color-coding
  • Bar: [■■■■■-----] with multi-color sections
  • Combined: 45.3% [■■■■■-----]

These are rendered by ffPercentAppendBar() and ffPercentAppendNum() in src/common/percent.c

Temperature Formatting

Temperature values are rendered with color-coding based on thresholds by ffTempsAppendNum() in src/common/temps.c The format system treats formatted percentages and temperatures as opaque strings once rendered.

Integration Pattern:

  1. Module calls specialized formatter (e.g., ffPercentAppendBar())
  2. Formatter appends to a temporary FFstrbuf
  3. Temporary buffer becomes a format argument of type FF_FORMAT_ARG_TYPE_STRBUF
  4. Format engine embeds the pre-formatted string via {placeholder}

Sources: src/common/percent.c56-167 src/common/temps.c6-65


Common Format String Patterns

Pattern 1: Simple Substitution with Fallback

Format: {name}{?version} {version}{?} Result: "Bash 5.1" (if version available) or "Bash" (if not) 

Pattern 2: Conditional Details

Format: {name}{?temp} @ {temp}{?}{?freq} @ {freq}{?} Result: "CPU @ 45°C @ 3.5 GHz" (both available) "CPU @ 45°C" (only temp available) "CPU @ 3.5 GHz" (only freq available) "CPU" (neither available) 

Pattern 3: Color-Coded Output

Format: {#ff0000}{name}{#} {#00ff00}{value}{#} Result: Red name, green value 

Pattern 4: Aligned Columns

Format: {name<20} {value>10} Result: "CPU 3.5 GHz" 

Pattern 5: Environment Integration

Format: {name} on {$HOSTNAME} Result: "CPU on mycomputer" 

Sources: doc/json_schema.json276-517