format strings for library and xbps-query
I'm not final on the api, don't really like the function names but couldn't come up with anything better.
Format strings are inspired by pythons f"" strings:
$ xbps-query -F '{pkgname:20}\n' -m | head
Carla
ImageMagick
LuaJIT-devel
MEGAcmd
PolyMC
SDL2-32bit
SDL2-devel
SDL2_gfx
SDL2_gfx-32bit
SDL2_image
$ xbps-query -F '{pkgver:-30} {installed_size:10h}\n' -m | head
Carla-2.5.1_2 79MB
ImageMagick-7.1.0.62_1 398KB
LuaJIT-devel-2.1.0beta3_2 898KB
MEGAcmd-1.4.0_1 1300KB
PolyMC-6.3_1 0B
SDL2-32bit-2.26.3_1 1841KB
SDL2-devel-2.26.3_1 6150KB
SDL2_gfx-1.0.4_2 72KB
SDL2_gfx-32bit-1.0.4_2 83KB
SDL2_image-2.6.2_1 127KB
$ xbps-query -F '0x{installed_size:8zX}\n' -m | head
0x04F62C86
0x000636AA
0x000E08BA
0x0014512A
0x00000000
0x001CC564
0x006019F2
0x00012075
0x00014D9C
0x0001FA1D
-F, --format
Format for list output.
<substitution> ::= "{" variable ["!" conversion] [":" format] "}"
<variable> ::= [a-zA-Z90-9_-]
<conversion> ::= humanize | strmode
-- Convert inode status information into a symbolic string
<strmode> ::= "strmode"
-- Format a number into a human readable form, the default is:`humanize .8Ki`:
<humanize> ::= "humanize" [space] [decimal] [width] [scale] [i]
<space> ::= " " -- Put a space between number and the suffix.
<decimal> ::= "." -- If the final result is less than 10, display
it using one digit.
<width> ::= [0-9]+ -- Width of the output.
<scale> ::= multiplier -- Minimum scale multiplier and optionally
[multiplier] -- Maxium scale multiplier.
<multiplier> ::= "B"
| "K" -- kilo
| "M" -- mega
| "G" -- giga
| "T" -- tera
| "P" -- peta
| "E" -- exa
<i> ::= "i" -- Divide number with 1000 instead of 1024.
<format> ::= [[fill] align] [sign] [width] ["." precision] [type]
<fill> ::= <any char> -- The character to use when aligning the output.
<align> ::= "<" -- Left align.
| ">" -- Right align.
| "=" -- Left align with zero paddings after the sign.
<sign> ::= "+" -- Add sign to positive and negative numbers.
| "-" -- Add sign to negative numbers.
<width> ::= [0-9]+ -- The alignment width.
<precision> ::= [0-9]+ -- Percision for numbers.
<type> ::= "d" -- Decimal number.
| "o" -- Octal number.
| "u" -- Unsigned number.
| "x" -- Hexadecimal with lowercase letters.
| "X" -- Hexadecimal with uppercase letters.
FORMAT STRINGS
Variables are package properties if not otherwise documented. See
PROPERTIES section for a list of available properties.
As example a format string like:
{pkgname:<30} {installed_size!humanize :>10}\n
Would produce a list formatted like:
libxbps 304 KB
xbps 484 KB
Format strings are parsed by the following EBNF:
<grammar> ::= (text | escape | substitution)*
<text> ::= [^\{}]+ -- literal text chunk
<escape> ::= "\" [abfnrtv0] -- POSIX-like espace sequence
| "\{" | "\}" -- escaped "{" and "}"
<substitution> ::= "{" variable ["!" conversion] [":" format] "}"
<variable> ::= [a-zA-Z0-9_-]
<conversion> ::= humanize | strmode
-- Convert inode status information into a symbolic string
<strmode> ::= "strmode"
-- Format a number into a human readable form, the default is:`humanize .8Ki`:
<humanize> ::= "humanize" [space] [decimal] [width] [scale] [i]
<space> ::= " " -- Put a space between number and the suffix.
<decimal> ::= "." -- If the final result is less than 10, display
it using one digit.
<width> ::= [0-9]+ -- Width of the output.
<scale> ::= multiplier -- Minimum scale multiplier and optionally
[multiplier] -- Maxium scale multiplier.
<multiplier> ::= "B" -- byte
| "K" -- kilo
| "M" -- mega
| "G" -- giga
| "T" -- tera
| "P" -- peta
| "E" -- exa
<i> ::= "i" -- Use IEE/IEC (and now also SI) power of two prefixes.
<format> ::= [[fill] align] [sign] [width] ["." precision] [type]
<fill> ::= <any char> -- The character to use when aligning the output.
<align> ::= "<" -- Left align.
| ">" -- Right align.
| "=" -- Left align with zero paddings after the sign.
<sign> ::= "+" -- Add sign to positive and negative numbers.
| "-" -- Add sign to negative numbers.
<width> ::= [0-9]+ -- The alignment width.
<precision> ::= [0-9]+ -- Percision for numbers.
<type> ::= "d" -- Decimal number.
| "o" -- Octal number.
| "u" -- Unsigned number.
| "x" -- Hexadecimal with lowercase letters.
| "X" -- Hexadecimal with uppercase letters.
<i> ::= "i" -- Divide number with 1000 instead of 1024.
this seems backwards to me? MiB would be the powers of 2 Mebibytes and MB would be the 10s based megabytes
<i> ::= "i" -- Divide number with 1000 instead of 1024.this seems backwards to me? MiB would be the powers of 2 Mebibytes and MB would be the 10s based megabytes
Yes that was wrong, changed to:
<i> ::= "i" -- Use IEE/IEC (and now also SI) power of two prefixes.
feature request for this: default values. would be useful for e.g. printing the user/group. maybe the syntax could be {variable?default!conv:spec}, and would be used if the value is NULL
Some form of replacement on values is needed to produce machine-readable output.
$ xbps-query -F '{pkgname},"{short_desc}"\n' -m | grep dino # not a csv
dino,"Modern XMPP ("Jabber") Chat Client using GTK+/Vala"
$ xbps-query -F '\{"{pkgname}":"{short_desc}"\}\n' -m | grep dino # not a json
{"dino":"Modern XMPP ("Jabber") Chat Client using GTK+/Vala"}
Maybe separate argument, like --transalate '&&', --translate \"\\\" --translate \\\\\\
$ xbps-query -F '\{{pkgname!json}:{run_depends!json}\}\n' -m
...
{"android-udev-rules":}
...
an incantation like this can create invalid json (hacky I know). I think all that needs adding is a default for arrays?