[ Widgets ]
Gauge
koto::Gauge renders a horizontal progress bar across an area with 1/8th sub-cell fractional block characters and an optional centered text label (Header: koto/gauge.hpp).
Quick example
Section titled “Quick example”Gauge() .percent(65) .label("65%") .filled_fg(Color::Emerald) .empty_fg(Color::DarkGray) .label_fg(Color::White) .draw(frame, gauge_area);█████████████65%███▌Behavior
Section titled “Behavior”- Ratio clamping:
ratio(float r)clampsrto[0.0f, 1.0f]viastd::clamp(r, 0.0f, 1.0f).percent(int p)callsratio(static_cast<float>(p) / 100.0f). - Sub-cell calculation:
Gauge::draw()computestotal_subcells = area.width * 8andfilled_subcells = std::clamp(static_cast<int>(std::lround(m_ratio * static_cast<float>(total_subcells))), 0, total_subcells). Each row inarearenders:full_cells = filled_subcells / 8columns using{m_filled_ch, m_filled_fg, m_filled_bg}.- Up to 1 fractional transition column at
col_idx == full_cellswhenremainder = filled_subcells % 8 > 0andm_fractional == true, usingFRACTIONAL_BLOCKS[remainder](U'▏',U'▎',U'▍',U'▌',U'▋',U'▊',U'▉') with foregroundm_filled_fgand backgroundm_empty_bg. - Remaining columns using
{m_empty_ch, m_empty_fg, m_empty_bg}.
- Centered label overlay: When
!m_label.empty()andtext_width(m_label) <= area.width, the label is drawn on rowarea.row + (area.height - 1) / 2starting atarea.col + (area.width - label_w) / 2. For eachGraphemeinm_label,GaugereadsCell existing = frame.get_cell(label_row, c)and writesCell{g, m_label_fg, existing.bg}, preserving the underlying bar cell’s background color.
Methods
Section titled “Methods”| Method | Description | Default |
|---|---|---|
Gauge() | Default-constructs a Gauge at 0.0f ratio with fractional blocks enabled. | m_ratio = 0.0f |
ratio(float r) | Sets progress ratio clamped to [0.0f, 1.0f]. | 0.0f |
percent(int p) | Calls ratio(static_cast<float>(p) / 100.0f). | 0 |
label(std::string_view label) | Sets the centered overlay label string. | "" |
filled_char(char32_t ch) | Sets the full-cell fill character and sets m_fractional = false. | U'█' |
empty_char(char32_t ch) | Sets the unfilled cell character. | U' ' |
use_fractional_blocks(bool enable) | Enables or disables 1/8th sub-block rendering on the boundary cell. | true |
fg(Color fg) | Alias for filled_fg(fg). | Color::Emerald |
bg(Color bg) | Alias for empty_bg(bg). | COLOR_DEFAULT |
filled_fg(Color fg) | Sets the foreground color of filled and fractional cells. | Color::Emerald |
filled_bg(Color bg) | Sets the background color of full filled cells. | COLOR_DEFAULT |
empty_fg(Color fg) | Sets the foreground color of empty cells. | Color::DarkGray |
empty_bg(Color bg) | Sets the background color of empty cells and the fractional transition cell. | COLOR_DEFAULT |
label_fg(Color fg) | Sets the foreground color of the centered label. | Color::White |
draw(Frame& frame, Area area) | Draws the gauge into frame within area. | None |
Gotchas
Section titled “Gotchas”- Calling
.filled_char()disables fractional blocks:Gauge::filled_char(ch)setsm_fractional = false(src/gauge.cpp:36). If you want both a customfilled_charand fractional blocks, call.use_fractional_blocks(true)after.filled_char(ch). - Label visibility over solid blocks: By default, filled cells use
m_filled_ch = U'█'(foregroundColor::Emerald, backgroundCOLOR_DEFAULT). When the label overwrites a filled cell, it replacesU'█'with the label character and preservesexisting.bg(COLOR_DEFAULT). Set.filled_bg(...)and.empty_bg(...)if you want a continuous colored background behind the label text. - No constructor taking
doubleorfloat: Always constructGauge()and chain.ratio(0.5f)or.percent(50).