{"id":607,"date":"2016-02-05T11:19:39","date_gmt":"2016-02-05T16:19:39","guid":{"rendered":"http:\/\/fazigu.org\/blog\/?p=607"},"modified":"2017-02-24T06:13:09","modified_gmt":"2017-02-24T11:13:09","slug":"shell-nuggets-quickie-tabulizer","status":"publish","type":"post","link":"https:\/\/fazigu.org\/blog\/2016\/02\/05\/shell-nuggets-quickie-tabulizer\/","title":{"rendered":"Shell Nuggets: Quickie Tabulizer"},"content":{"rendered":"<p>I&#8217;m real bad about things lining up. \u00a0Used to go out of my way to ensure variable declarations were columnized by access mode, type, name, equals sign and initial value.<\/p>\n<blockquote>\n<pre style=\"font-size: 10px\">final BufferedImage mapImg = MapImageFactory.newInstance(width, height);\r\nfinal int           count  = ad.getPosition() * 2;\r\n\r\nmapImg.composite(adImg, 25               , (this.adHeight + 20) * count + 20, Over);\r\nmapImg.composite(adTxt, 25 + this.adWidth, (this.adHeight + 20) * count + 20, Over);\r\n<\/pre>\n<\/blockquote>\n<p>The <a href=\"https:\/\/github.com\/godlygeek\/tabular\" target=\"_blank\">Tabularizer plugin<\/a>\u00a0for vim was a boon for productivity. \u00a0Well, mine. \u00a0For everyone else, it meant either destroying the pre-formatted beauty or re-aligning all the columns.<\/p>\n<p>Anyway, it&#8217;s considered bad practice, so I&#8217;ve tried to get away from it&#8211; at least when writing shared code. \u00a0I still like perfect tabular output in my day-to-day. \u00a0Take the <code>df<\/code> command, for example. \u00a0It&#8217;s default &#8220;human readable sizes&#8221; output:<\/p>\n<blockquote>\n<pre style=\"font-size: 10px\">2016-02-05 11:03:57 \u00ab\u00ab\u00ab\u00ab ~\r\n rons@rons-VM$ df -h\r\nFilesystem            Size  Used Avail Use% Mounted on\r\n\/dev\/mapper\/VolGroup00-LogVol00\r\n                       59G   29G   27G  52% \/\r\n\/dev\/sda1              99M   33M   61M  36% \/boot\r\ntmpfs                 1.5G     0  1.5G   0% \/dev\/shm\r\n<\/pre>\n<\/blockquote>\n<p>Bleah. A line break. Why you mess my rows, GNU? An easy fix jumped into my noggin the other day and crept down my arms and out my fingertips:<\/p>\n<blockquote>\n<pre style=\"font-size: 10px\">2016-02-05 11:03:58 \u00ab\u00ab\u00ab\u00ab ~\r\n rons@rons-VM$ df -h | sed 's\/Mounted on$\/Mount\/;' | xargs printf '%-40.40s %8s %8s %8s %5s  %s\\n'\r\nFilesystem                                   Size     Used    Avail  Use%  Mount\r\n\/dev\/mapper\/VolGroup00-LogVol00               59G      29G      27G   52%  \/\r\n\/dev\/sda1                                     99M      33M      61M   36%  \/boot\r\ntmpfs                                        1.5G        0     1.5G    0%  \/dev\/shm\r\n<\/pre>\n<\/blockquote>\n<p>The trick is that a\u00a0<code>printf<\/code>\u00a0format with a trailing newline will repeat if it runs out of input tokens. \u00a0 That&#8217;s why I had to truncate the &#8220;Mounted on&#8221; to just &#8220;Mount&#8221; above&#8211; otherwise there would be an irregular number of columns (and thus <code>printf<\/code> tokens) on each line\/row. \u00a0Spaces in filesystems and mounts will mess it up, too, but if you use spaces in the names of your mounts, you deserve the dissonance.<\/p>\n<p>Edit: Better shell function replacement below.<\/p>\n<pre>unset -f df ; function df() {\r\n  command df -P $@ | perl -ane '\r\n    push(@rows, [@F]);\r\n    for (0..$#F) {\r\n      $ll = length($F[$_]); $cm[$_] = $ll if $ll &gt; ($cm[$_] \/\/ 0);\r\n    }\r\n    END { printf(\"%-$cm[0]s %$cm[1]s %$cm[2]s %$cm[3]s %$cm[4]s %s\\n\", @{$_})\r\n      for @rows; }';\r\n} ;<\/pre>\n<p>&nbsp;<\/p>\n<p>Uses the -P flag for POSIX one-line-per-mount format. \u00a0 Dynamically calculates column widths.<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li><a href=\"http:\/\/paydaycashamericapawnloansforbadcredit.accountant\">approved cash advance<\/a><\/li>\n<li><a href=\"http:\/\/creditcardsforbadinstantpayday.accountant\">bad credit loans guaranteed approval<\/a><\/li>\n<li><a href=\"http:\/\/paydaycashcentralloansforpeoplewithbad.accountant\">payday loan near me<\/a><\/li>\n<li><a href=\"http:\/\/paydaybadcreditloansfor.accountant\">fast cash loans<\/a><\/li>\n<li><a href=\"http:\/\/paydaysamedayloansonlineloan.accountant\">fast loan<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m real bad about things lining up. \u00a0Used to go out of my way to ensure variable declarations were columnized by access mode, type, name, equals sign and initial value. final BufferedImage mapImg = MapImageFactory.newInstance(width, height); final int count = ad.getPosition() * 2; mapImg.composite(adImg, 25 , (this.adHeight + 20) * count + 20, Over); mapImg.composite(adTxt, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[29,26],"class_list":["post-607","post","type-post","status-publish","format-standard","hentry","category-hacking","tag-bash","tag-linux"],"_links":{"self":[{"href":"https:\/\/fazigu.org\/blog\/wp-json\/wp\/v2\/posts\/607","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fazigu.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fazigu.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fazigu.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fazigu.org\/blog\/wp-json\/wp\/v2\/comments?post=607"}],"version-history":[{"count":7,"href":"https:\/\/fazigu.org\/blog\/wp-json\/wp\/v2\/posts\/607\/revisions"}],"predecessor-version":[{"id":670,"href":"https:\/\/fazigu.org\/blog\/wp-json\/wp\/v2\/posts\/607\/revisions\/670"}],"wp:attachment":[{"href":"https:\/\/fazigu.org\/blog\/wp-json\/wp\/v2\/media?parent=607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fazigu.org\/blog\/wp-json\/wp\/v2\/categories?post=607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fazigu.org\/blog\/wp-json\/wp\/v2\/tags?post=607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}