ohmu
·
2026-10-15
1#pragma once
2
3#include "common/FFstrbuf.h"
4#include "common/parsing.h"
5#include "common/option.h"
6
7typedef enum FFPercentageTypeFlags: uint8_t {
8 FF_PERCENTAGE_TYPE_NONE = 0,
9 FF_PERCENTAGE_TYPE_NUM_BIT = 1 << 0,
10 FF_PERCENTAGE_TYPE_BAR_BIT = 1 << 1,
11 FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT = 1 << 2,
12 FF_PERCENTAGE_TYPE_NUM_COLOR_BIT = 1 << 3,
13 FF_PERCENTAGE_TYPE_BAR_MONOCHROME_BIT = 1 << 4,
14} FFPercentageTypeFlags;
15static_assert(sizeof(FFPercentageTypeFlags) == 1, "");
16
17typedef struct FFPercentageModuleConfig {
18 uint8_t green;
19 uint8_t yellow;
20 FFPercentageTypeFlags type;
21} FFPercentageModuleConfig;
22
23// Maps a received signal strength in dBm onto the 0-100 scale the percentage formatters want.
24// -50 dBm and above counts as a perfect link, -100 dBm and below as no link at all, and the range in
25// between is linear. Both ends are clamped rather than extrapolated, so the result is always a
26// percentage -- which also keeps it usable with `{?x}` guards, unlike a raw negative RSSI.
27[[gnu::const]] static inline double ffRssiToSignalQuality(int32_t rssi) {
28 return (double) (rssi >= -50 ? 100 : rssi <= -100 ? 0 : (rssi + 100) * 2);
29}
30
31// if (green <= yellow)
32// [0, green]: print green
33// (green, yellow]: print yellow
34// (yellow, 100]: print red
35//
36// if (green > yellow)
37// [green, 100]: print green
38// [yellow, green): print yellow
39// [0, yellow): print red
40
41// `buffer` and `module` are dereferenced unconditionally; `module` carries the key/color formatting
42[[gnu::nonnull(1, 4)]] void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConfig config, const FFModuleArgs* module);
43[[gnu::nonnull(1, 5)]] void ffPercentAppendNum(FFstrbuf* buffer, double percent, FFPercentageModuleConfig config, bool parentheses, const FFModuleArgs* module);
44
45typedef struct yyjson_val yyjson_val;
46typedef struct yyjson_mut_doc yyjson_mut_doc;
47typedef struct yyjson_mut_val yyjson_mut_val;
48bool ffPercentParseCommandOptions(const char* key, const char* subkey, const char* value, FFPercentageModuleConfig* config);
49bool ffPercentParseJsonObject(yyjson_val* key, yyjson_val* value, FFPercentageModuleConfig* config);
50[[gnu::nonnull(1, 2)]] void ffPercentGenerateJsonConfig(yyjson_mut_doc* doc, yyjson_mut_val* module, FFPercentageModuleConfig config);
51const char* ffPercentParseTypeJsonConfig(yyjson_val* value, FFPercentageTypeFlags* result);