2020-05-24 08:48:17 +00:00
|
|
|
% notcurses_metric(3)
|
|
|
|
% nick black <nickblack@linux.com>
|
2022-01-21 17:39:27 +00:00
|
|
|
% v3.0.5
|
2020-05-24 08:48:17 +00:00
|
|
|
|
|
|
|
# NAME
|
|
|
|
|
|
|
|
notcurses_metric - fixed-width numeric output with metric suffixes
|
|
|
|
|
|
|
|
# SYNOPSIS
|
|
|
|
|
|
|
|
**#include <notcurses/notcurses.h>**
|
|
|
|
|
|
|
|
```c
|
2021-11-09 01:09:28 +00:00
|
|
|
#define NCPREFIXCOLUMNS 7
|
|
|
|
#define NCIPREFIXCOLUMNS 8
|
|
|
|
#define NCBPREFIXCOLUMNS 9
|
|
|
|
#define NCPREFIXSTRLEN (NCPREFIXCOLUMNS + 1)
|
|
|
|
#define NCIPREFIXSTRLEN (NCIPREFIXCOLUMNS + 1)
|
|
|
|
#define NCBPREFIXSTRLEN (NCBPREFIXCOLUMNS + 1)
|
2020-09-15 05:39:42 +00:00
|
|
|
#define NCMETRICFWIDTH(x, cols) ((int)(strlen(x) - ncstrwidth(x) + (cols)))
|
2021-11-09 01:09:28 +00:00
|
|
|
#define NCPREFIXFMT(x) NCMETRICFWIDTH((x), NCPREFIXCOLUMNS), (x)
|
|
|
|
#define NCIPREFIXFMT(x) NCMETRIXFWIDTH((x), NCIPREFIXCOLUMNS), (x)
|
|
|
|
#define NCBPREFIXFMT(x) NCMETRICFWIDTH((x), NCBPREFIXCOLUMNS), (x)
|
2020-05-24 08:48:17 +00:00
|
|
|
```
|
|
|
|
|
2021-11-09 05:53:30 +00:00
|
|
|
**const char* ncnmetric(uintmax_t ***val***, size_t s, uintmax_t ***decimal***, char* ***buf***, int ***omitdec***, unsigned ***mult***, int ***uprefix***);**
|
2020-05-24 08:48:17 +00:00
|
|
|
|
2021-11-07 10:52:31 +00:00
|
|
|
**static inline const char* ncqprefix(uintmax_t ***val***, uintmax_t ***decimal***, char* ***buf***, int ***omitdec***);**
|
2020-11-06 21:49:35 +00:00
|
|
|
|
2021-11-07 10:52:31 +00:00
|
|
|
**static inline const char* nciprefix(uintmax_t ***val***, uintmax_t ***decimal***, char* ***buf***, int ***omitdec***);**
|
2020-11-28 22:08:45 +00:00
|
|
|
|
2021-11-07 10:52:31 +00:00
|
|
|
**static inline const char* ncbprefix(uintmax_t ***val***, uintmax_t ***decimal***, char* ***buf***, int ***omitdec***);**
|
2020-05-24 08:48:17 +00:00
|
|
|
|
|
|
|
# DESCRIPTION
|
|
|
|
|
|
|
|
**ncmetric** (and the helper wrappers **qprefix** and **bprefix**) accept
|
|
|
|
very large (or very small) non-negative numbers, and prepare formatted output
|
|
|
|
of a maximum width using metric suffixes. The suffix can represent arbitrary
|
2021-11-09 01:09:28 +00:00
|
|
|
amounts of growth, but is designed for 1000 (**NCPREFIX**) or 1024
|
|
|
|
(**NCIPREFIX**). 1024 is used for "digital units of information", i.e. kibibytes
|
2020-08-24 04:12:36 +00:00
|
|
|
and gibibits. **ncmetric** supports the large suffixes KMGTPEZY (Kilo, Mega,
|
2020-05-24 08:48:17 +00:00
|
|
|
Giga, Tera, Peta, Exa, Zetta, and Yotta) and the small suffixes mµnpfazy
|
|
|
|
(Milli, Micro, Nano, Pico, Femto, Atto, Zepto, and Yocto). This covers the
|
2020-11-28 22:08:45 +00:00
|
|
|
range 1e24 (one septillion) through 1e-24, sufficing for all possible values of
|
|
|
|
a 64-bit **uintmax_t**.
|
2020-05-24 08:48:17 +00:00
|
|
|
|
2021-11-09 05:53:30 +00:00
|
|
|
***val*** is the value being output, having been scaled by ***decimal***.
|
|
|
|
***decimal*** will typically be 1; to represent values less than 1, ***decimal***
|
|
|
|
should be larger than ***val***. The output will be written to ***buf***, which
|
2020-05-24 08:48:17 +00:00
|
|
|
must be at least:
|
|
|
|
|
2021-11-09 01:09:28 +00:00
|
|
|
* **NCPREFIXSTRLEN** + 1 bytes for a 1000-based value
|
|
|
|
* **NCIPREFIXSTRLEN** + 1 bytes for a 1024-based value
|
|
|
|
* **NCBPREFIXSTRLEN** + 1 bytes for a 1024-based value with an 'i' suffix
|
2020-05-24 08:48:17 +00:00
|
|
|
|
2021-11-09 05:53:30 +00:00
|
|
|
***s*** is the maximum output size, including '\0', used in the same
|
|
|
|
fashion as **snprintf(3)** (which **ncnmetric** calls).
|
|
|
|
|
2020-11-28 22:08:45 +00:00
|
|
|
Three helper functions are provided to simplify these common cases:
|
|
|
|
|
|
|
|
```
|
2021-11-09 01:09:28 +00:00
|
|
|
// Mega, kilo, gigafoo. Use NCPREFIXSTRLEN + 1 and NCPREFIXCOLUMNS.
|
2020-11-28 22:08:45 +00:00
|
|
|
static inline const char*
|
2021-11-07 10:52:31 +00:00
|
|
|
ncqprefix(uintmax_t val, uintmax_t decimal, char* buf, int omitdec){
|
2020-11-28 22:08:45 +00:00
|
|
|
return ncmetric(val, decimal, buf, omitdec, 1000, '\0');
|
|
|
|
}
|
|
|
|
|
2021-11-09 01:09:28 +00:00
|
|
|
// Mibi, kebi, gibibytes sans 'i' suffix. Use NCIPREFIXSTRLEN + 1.
|
2020-11-28 22:08:45 +00:00
|
|
|
static inline const char*
|
2021-11-07 10:52:31 +00:00
|
|
|
nciprefix(uintmax_t val, uintmax_t decimal, char* buf, int omitdec){
|
2020-11-28 22:08:45 +00:00
|
|
|
return ncmetric(val, decimal, buf, omitdec, 1024, '\0');
|
|
|
|
}
|
|
|
|
|
2021-11-09 01:09:28 +00:00
|
|
|
// Mibi, kebi, gibibytes. Use NCBPREFIXSTRLEN + 1 and NCBPREFIXCOLUMNS.
|
2020-11-28 22:08:45 +00:00
|
|
|
static inline const char*
|
2021-11-07 10:52:31 +00:00
|
|
|
ncbprefix(uintmax_t val, uintmax_t decimal, char* buf, int omitdec){
|
2020-11-28 22:08:45 +00:00
|
|
|
return ncmetric(val, decimal, buf, omitdec, 1024, 'i');
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-05-24 08:48:17 +00:00
|
|
|
If **omitdec** is not zero, the decimal point and mantissa will be
|
|
|
|
omitted if all digits to be displayed would be zero. The decimal point takes
|
|
|
|
the current locale into account (see **setlocale(3)** and **localeconv(3)**).
|
2021-11-09 05:53:30 +00:00
|
|
|
***mult*** is the relative multiple for each suffix. ***uprefix***, if not zero,
|
2020-05-24 08:48:17 +00:00
|
|
|
will be used as a suffix following any metric suffix.
|
|
|
|
|
2020-11-28 22:08:45 +00:00
|
|
|
The maximum number of columns is not directly related to the maximum number of
|
|
|
|
bytes, since Unicode doesn't necessarily map to single-byte characters
|
|
|
|
(including the 'µ' micro suffix). The corresponding defines for maximum column
|
|
|
|
length are:
|
2020-05-24 08:48:17 +00:00
|
|
|
|
2021-11-09 01:09:28 +00:00
|
|
|
* **NCPREFIXCOLUMNS** (7)
|
|
|
|
* **NCIPREFIXCOLUMNS** (8)
|
|
|
|
* **NCBPREFIXCOLUMNS** (9)
|
2020-05-24 08:48:17 +00:00
|
|
|
|
2020-11-28 22:08:45 +00:00
|
|
|
In general, the maximum-width output will take the form **CCC.mmMu**, where C
|
|
|
|
are digits of the characteristic (up to ceil(log10(**mult**)) digits), the
|
|
|
|
decimal point follows, m are digits of the mantissa (up to 2), M is the metric
|
2021-11-09 05:53:30 +00:00
|
|
|
suffix, and u is the ***uprefix***. The minimum-width output will take the form
|
|
|
|
**C**. This minimal form can occur if ***omitdec*** is non-zero and a
|
|
|
|
single-column value such as 5 is passed for ***val***.
|
2020-05-24 08:48:17 +00:00
|
|
|
|
2020-11-28 22:08:45 +00:00
|
|
|
Three more defines are provided to simplify formatted fixed-width output using
|
|
|
|
the results of **ncmetric**. Each of these macros accepts a character buffer
|
|
|
|
holding the result of the call, and expand to *two* arguments:
|
2020-05-24 08:48:17 +00:00
|
|
|
|
2021-11-09 01:09:28 +00:00
|
|
|
* **NCPREFIXFMT(x)**
|
|
|
|
* **NCIPREFIXFMT(x)**
|
|
|
|
* **NCBPREFIXFMT(x)**
|
2020-11-28 22:08:45 +00:00
|
|
|
|
|
|
|
These can be used in e.g. the following ungainly fashion:
|
|
|
|
|
2021-11-09 01:09:28 +00:00
|
|
|
**ncplane_printf(n, "%*s", NCPREFIXFMT(buf));**
|
2020-11-28 22:08:45 +00:00
|
|
|
|
2021-11-09 01:09:28 +00:00
|
|
|
to ensure that the output is always **NCPREFIXCOLUMNS** wide.
|
2020-05-24 08:48:17 +00:00
|
|
|
|
|
|
|
# RETURN VALUES
|
|
|
|
|
2021-11-09 05:53:30 +00:00
|
|
|
**NULL** if input parameters were invalid. Otherwise, a pointer to ***buf***,
|
2020-05-24 08:48:17 +00:00
|
|
|
filled in with the formatted output.
|
|
|
|
|
2020-11-28 22:08:45 +00:00
|
|
|
# EXAMPLES
|
|
|
|
|
2021-11-09 05:53:30 +00:00
|
|
|
**ncnmetric(0, INT_MAX, buf, 0, 1000, '\0')**: "0.00".
|
2020-11-28 22:08:45 +00:00
|
|
|
|
2021-11-09 05:53:30 +00:00
|
|
|
**ncnmetric(0, INT_MAX, buf, 1, 1000, '\0')**: "0".
|
2020-11-28 22:08:45 +00:00
|
|
|
|
2021-11-09 05:53:30 +00:00
|
|
|
**ncnmetric(1023, INT_MAX, buf, 1, 1000, '\0')**: "1.02K".
|
2020-11-28 22:08:45 +00:00
|
|
|
|
2021-11-09 05:53:30 +00:00
|
|
|
**ncnmetric(1023, INT_MAX, buf, 1, 1024, 'i')**: "1023".
|
2020-11-28 22:08:45 +00:00
|
|
|
|
2021-11-09 05:53:30 +00:00
|
|
|
**ncnmetric(1024, INT_MAX, buf, 1, 1024, 'i')**: "1Ki".
|
2020-11-28 22:08:45 +00:00
|
|
|
|
2021-11-09 05:53:30 +00:00
|
|
|
**ncnmetric(4096, INT_MAX, buf, 0, 1024, 'i')**: "4.00Ki".
|
2020-11-28 22:08:45 +00:00
|
|
|
|
2021-11-09 05:53:30 +00:00
|
|
|
**ncnmetric(0x7fffffffffffffff, INT_MAX, buf, 0, 1000, '\0')**: "9.22E".
|
2020-11-28 22:08:45 +00:00
|
|
|
|
2021-11-09 05:53:30 +00:00
|
|
|
**ncnmetric(0xffffffffffffffff, INT_MAX, buf, 0, 1000, '\0')**: "18.45E".
|
2020-11-28 22:08:45 +00:00
|
|
|
|
|
|
|
# BUGS
|
|
|
|
|
|
|
|
This function is difficult to understand.
|
|
|
|
|
2020-05-24 08:48:17 +00:00
|
|
|
# SEE ALSO
|
|
|
|
|
|
|
|
**localeconv(3)**,
|
|
|
|
**notcurses(3)**,
|
2020-11-28 22:22:05 +00:00
|
|
|
**notcurses_output(3)**,
|
2021-11-09 05:53:30 +00:00
|
|
|
**setlocale(3)**,
|
|
|
|
**snprintf(3)**
|