-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
proposal: text/template: add table action #45752
Comments
table
node to text/template
Could you just feed the output of text/template into text/tabwriter? |
From the perspective of providing useful template functions to users to simply format output without writing or installing another tool, that doesn't seem like a very useful approach. Both the Docker CLI and GitHub CLI (and likely many other tools accepting go templates) provide a templating. Seems making text/tabwriter available via this mechanism would be generally useful to provide tidy output to the console. |
While I realize this proposal as written may not be acceptable because it emits formatted text directly instead of facilitating formatting, perhaps it would be better to consider alternatives that still provide the core formatting to functions. For instance, if we were to try to do this in a function, something like this could work:
However, we lose any ability to pass fields to other custom functions. Perhaps we could consider something like this where parenthesis are parsed by the text/template engine:
Here, |
I see you can already use parenthesis like that: cli/cli#3488 (comment) While I'm tempted to just close this proposal out, I would like to still see if there's interest in it. While I have a solution (or workaround at least), I think this could still be useful, though I realize it might be out of scope since it would emit formatted text itself instead of merely facilitating in formatting. |
I'm not suggesting installing another tool, I'm suggesting something like (untested)
|
Ah, I see. That's clever and if a table format was all we wanted to support I agree that's very straight forward. In this case, I'm sure the CLI works want to keep the format open-ended. |
This is a very specific instance of a general request to be able to pipe a block of template output through another function. If we could do that, then "table" could be defined as a function outside the template package. But that's not possible today. |
I was able to devise a way: cli/cli#3488 Originally I used text/tabwriter, but after feedback switched over to the TablePrinter the GitHub CLI already used internally. I ended up adding just a
|
This proposal has been added to the active column of the proposals project |
I'd love to contribute my work (the original work using tabwriter, perhaps, though some refactorings for performance would be good) if approved. That said, would it be appropriate to pile on a few other related ideas or better as a separate proposal? Specifically, supporting "safe navigation operators" (a proposal to golang itself that was rejected) for templates since you have a limited grammar. For example, if I query for a nested string, there's no way short of duplicating parts of templates between if/else/end to handle the inevitable error. For example, GitHub can send back (Was also posted for discussion to https://forum.golangbridge.org/t/support-for-safe-navigation-in-text-templates/23325) |
The bar for adding new things to template is pretty high. /cc @robpike |
I agree that this does not bring enough new power to the library to be worth the change. |
Understood. I can open a separate issue for the safe navigation operator, but wondering if you think that also wouldn't right to the bar given my use case above. If it's at least worth discussing in a separate issue, I'm happy to open one. |
I believe "safe navigation" is #43608 |
No change in consensus, so declined. |
I'd like to propose adding a new node to text/template similar to
{{range pipeline}}
named{{table pipeline}}
. Inspired by Docker CLI's pseudo-function to format an array using text/tabwriter, this could be a generally useful way to format an array as columnar data using elastic tabstops.As noted in cli/cli#3488 (comment), Docker CLI checks if the template starts with "table" and does some simple string pre-parsing. The inherent problem is that the template must start with "table", so you can't first scope a pipeline with
{{with pipeline}}
, for example. You can't really do this wthin a string either because you need to know where the table columns should end, and only parse tree nodes have{{end}}
.I propose (and interested in contributing) something like
range
that works on an array, but lets you delimit columns like so:This would product output like:
The
{{end}}
node would callWriter.Flush
which should effectively set the final tabstops.Ideally, it would be great to provide some control over
tabwriter.NewWriter
's arguments. Perhaps{{table}}
could support variadic...[]string
arguments to set a few options, but columns can help support that on their own, like a user-definedpad
function that prefixes or suffixes padding characters like "..", changing colors, etc. Another option might be to emit a header using the same tabstops so the headers align with the columns, much like Docker CLI does with their pseudo-function.The text was updated successfully, but these errors were encountered: