Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format strings for library and xbps-query #542

Open
wants to merge 32 commits into
base: master
Choose a base branch
from

Conversation

Duncaen
Copy link
Member

@Duncaen Duncaen commented Feb 20, 2023

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

@Duncaen Duncaen force-pushed the format branch 6 times, most recently from 8b4daae to f84b587 Compare February 21, 2023 01:45
Copy link
Member

@classabbyamp classabbyamp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interested in this for #541

lib/format.c Outdated Show resolved Hide resolved
lib/format.c Outdated Show resolved Hide resolved
@Duncaen Duncaen force-pushed the format branch 2 times, most recently from 035ac3d to 89a1704 Compare February 21, 2023 16:46
lib/format.c Fixed Show fixed Hide fixed
@Duncaen

This comment was marked as outdated.

@Duncaen Duncaen force-pushed the format branch 2 times, most recently from 74f6346 to aab32fe Compare March 10, 2023 15:55
@Duncaen
Copy link
Member Author

Duncaen commented Mar 10, 2023

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.

@classabbyamp
Copy link
Member

classabbyamp commented Mar 10, 2023

<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

@Duncaen
Copy link
Member Author

Duncaen commented Mar 10, 2023

<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.

Copy link
Member

@classabbyamp classabbyamp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

bin/xbps-query/xbps-query.1 Outdated Show resolved Hide resolved
.Pp
As example a format string like:
.Bd -offset indent -literal
{pkgname:<30} {installed_size!humanize :>10}\\n
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is pkgname a property? if not, would it be possible to have it (and maybe version?) as a "pre-filtered" pkgver?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pkgname is added by xbps "dynamically" at runtime, which kinda sucks but we could probably do that for version too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah i see pkgname in the manpage now

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe have {pkgver!pkgname} and {pkgver!version} or something like that, nut sure if this really matters, maybe I should just change this in the documentation to pkgver.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well pkgname is listed in PROPERTIES, so it should be fine. adding version as a dynamic property is fairly minor imo, so could be done later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main issue with the pkgname prop is that for the pkgdb it is added when the pkgdb is loaded, for packages out of repodata it sets it when you use xbps_repo_get_pkg, so this may not be set for certain cases where it iterates of the packages in the repo.

I think xtraeme added this as a hack because the code converted pkgvers to pkgnames all over the place.

@classabbyamp
Copy link
Member

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

lib/format.c Fixed Show fixed Hide fixed
lib/format.c Fixed Show fixed Hide fixed
lib/format.c Fixed Show fixed Hide fixed
lib/format.c Fixed Show fixed Hide fixed
lib/format.c Fixed Show fixed Hide fixed
@Duncaen Duncaen force-pushed the format branch 4 times, most recently from b080bf3 to 02f7bcd Compare March 14, 2023 18:56
@Chocimier
Copy link
Member

Chocimier commented Apr 12, 2023

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 '&&amp;', --translate \"\\\" --translate \\\\\\

@Duncaen Duncaen force-pushed the format branch 2 times, most recently from e52683e to 0a47ba2 Compare September 18, 2023 21:09
Copy link
Member

@classabbyamp classabbyamp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approval for concept and (user-facing) functionality

@@ -100,6 +100,8 @@ Ignore repositories defined in configuration files.
Only repositories specified in the command line via
.Ar --repository
will be used.
.It Fl J, Fl -json
Print output as json.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be good to indicate which operating modes --json and --format work in (or get it working in all operating modes)

looks like currently it works for:

-J -F
-l x x
-L
-H x x
--list-repolock-pkgs x x
-m x x
-o
-S
-s
-f 1 1
-x
-X

Footnotes

  1. no but I think track file mode and ownership in files.plist #541 will cover it 2

@classabbyamp
Copy link
Member

$ 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants