miller icon indicating copy to clipboard operation
miller copied to clipboard

How to align both header and data?

Open agguser opened this issue 3 years ago • 9 comments

For example:

$ M() { echo "s,i,f\na,1,1.1\nbb,22,22.22" | mlr --c2p "$@"; }

$ M cat
s  i  f
a  1  1.1
bb 22 22.22

$ M format-values -i '%2lld' -f '%5.2lf'
s  i  f
a   1  1.10
bb 22 22.22

$ M put '$i=fmtnum($i,"%2lld"); $f=fmtnum($f,"%5.2lf")'
s  i  f
a   1  1.10
bb 22 22.22

But what I want is that both header and data has same alignment:

s   i     f
a   1  1.10
bb 22 22.22

It would be great if there is ralign verb to right-align specific fields, e.g. mlr --c2p ralign -f i,f.

agguser avatar Oct 24 '20 19:10 agguser

looking ...

johnkerl avatar Oct 24 '20 22:10 johnkerl

I think you just want mlr --opprint --right ... ?

$ mlr --c2p cat sif.csv
s  i  f
a  1  1.1
bb 22 22.22

$ mlr --c2p --right cat sif.csv
 s  i     f
 a  1   1.1
bb 22 22.22

johnkerl avatar Oct 26 '20 03:10 johnkerl

I want to right-align only "number" columns (i, f), not "text" columns (s).

agguser avatar Oct 26 '20 03:10 agguser

D'oh.

johnkerl avatar Oct 26 '20 03:10 johnkerl

Yes I had imagined this some while back but wasn't sure if there were sufficient demand. Now that there are two of us who have thought of this ... probably rises to the level of being worth coding up. ;)

johnkerl avatar Oct 26 '20 03:10 johnkerl

Thanks. By the way, on second thought, instead of ralign verb, it should be --ralign {a,b,c} option for pprint.

agguser avatar Oct 26 '20 03:10 agguser

Workaround:

$ echo "s,i,f\na,1,1.1\nbb,22,22.22" | mlr --c2t format-values -f '%.2lf' | column -t -s $'\t' -R 3
s   i       f
a   1    1.10
bb  22  22.22

agguser avatar Oct 28 '20 02:10 agguser

One more interested!

I got here also looking on how to right-align only some of the columns in a pprint output. I'm generating a small report tool that will list a set of data consisting on text, numbers (integers and floats) and times (in the format [[dd-]hh:]mm:ss). Humans need numbers to be right aligned to be easy to skim over them (comma aligned, to be more precise, which is equivalent to right aligned in case of integers and floats formatted with a fixed number of decimals). On the other hand, text needs to be left aligned for human comfort. In the middle is the time data: it is handled as a string, but it should be right aligned as well so the hours, minutes and seconds are always aligned.

Some kind of "alignment specification" would be good. The "ralign" proposal seems good, but I want to add something:

  • Given that left alignment is the default, per column right alignment is missing... and it would be good to have "center" ability in the same go. In fact, I would go for a parameter to specify default alignment (left, right, center) and a verb to specify column alignments.
  • Giving the columns as an option to pprint instead of a verb could make it difficult to apply this option to newly created fields on the fly through a put command.

Poshi avatar Jul 28 '21 11:07 Poshi

+1 that --ralign {a,b,c} option would be great or some kind of alignment "spec"

nikbucher avatar Jun 09 '23 06:06 nikbucher