<story><title>ps aux written in bash without forking</title><url>https://github.com/izabera/ps</url></story><parent_chain><item><author>bheadmaster</author><text>&amp;gt; # turns out that the most difficult problem in computer science is aligning things&lt;p&gt;&amp;gt; # this one function looks simple but it took so fucking long&lt;p&gt;Heh. I can&amp;#x27;t count how many times I&amp;#x27;ve written a column-aligning functions in various programming languages, and each time it is a pain. And it sounds simple in my head - just get max-length of each column, and add spaces up to the next multiple of tab-size.&lt;p&gt;But even in Python with f-strings and all the fancy padding stuff it has, it is ends up a convoluted, unreadable mess:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    # randomwordgenerator.com
    table = [
        [&amp;#x27;agony&amp;#x27;, &amp;#x27;kick&amp;#x27;, &amp;#x27;pump&amp;#x27;],
        [&amp;#x27;frown&amp;#x27;, &amp;#x27;lonely&amp;#x27;, &amp;#x27;mutation&amp;#x27;],
        [&amp;#x27;sail&amp;#x27;, &amp;#x27;tasty&amp;#x27;, &amp;#x27;want&amp;#x27;],
    ]

    tab_width = 4
    n_rows, n_cols = len(table), len(table[0])
    max_width = [
        max(len(table[r][c]) for r in range(n_rows))
        for c in range(n_cols)
    ]

    for r in range(n_rows):
        for c in range(n_cols):
            item = table[r][c]
            if c == n_cols - 1:
                # do not print space after last item
                print(item)
            else:
                # only print newline after last item
                # EDIT: found a bug here after commenting...
                # width = int(((max_width[c] + 1) &amp;#x2F; tab_width) * tab_width)
                width = ((max_width[c] &amp;#x2F;&amp;#x2F; tab_width) + 1) * tab_width

                print(f&amp;#x27;{item:&amp;lt;{width}}&amp;#x27;, end=&amp;#x27;&amp;#x27;)
&lt;/code&gt;&lt;/pre&gt;
Even while writing this code for the comment for the hundredth time, I&amp;#x27;ve had to fix at least 5 bugs! Truly horrible.</text></item></parent_chain><comment><author>Mister_Snuggles</author><text>I&amp;#x27;ve literally added Pandas to some of my projects just so I could use a DataFrame to print a nicely formatted table instead of writing code like this.&lt;p&gt;Surely there&amp;#x27;s a library out there to do this job, it seems like such a common use-case.  I&amp;#x27;m surprised it&amp;#x27;s not in the standard library to be honest!</text></comment>
<story><title>ps aux written in bash without forking</title><url>https://github.com/izabera/ps</url></story><parent_chain><item><author>bheadmaster</author><text>&amp;gt; # turns out that the most difficult problem in computer science is aligning things&lt;p&gt;&amp;gt; # this one function looks simple but it took so fucking long&lt;p&gt;Heh. I can&amp;#x27;t count how many times I&amp;#x27;ve written a column-aligning functions in various programming languages, and each time it is a pain. And it sounds simple in my head - just get max-length of each column, and add spaces up to the next multiple of tab-size.&lt;p&gt;But even in Python with f-strings and all the fancy padding stuff it has, it is ends up a convoluted, unreadable mess:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    # randomwordgenerator.com
    table = [
        [&amp;#x27;agony&amp;#x27;, &amp;#x27;kick&amp;#x27;, &amp;#x27;pump&amp;#x27;],
        [&amp;#x27;frown&amp;#x27;, &amp;#x27;lonely&amp;#x27;, &amp;#x27;mutation&amp;#x27;],
        [&amp;#x27;sail&amp;#x27;, &amp;#x27;tasty&amp;#x27;, &amp;#x27;want&amp;#x27;],
    ]

    tab_width = 4
    n_rows, n_cols = len(table), len(table[0])
    max_width = [
        max(len(table[r][c]) for r in range(n_rows))
        for c in range(n_cols)
    ]

    for r in range(n_rows):
        for c in range(n_cols):
            item = table[r][c]
            if c == n_cols - 1:
                # do not print space after last item
                print(item)
            else:
                # only print newline after last item
                # EDIT: found a bug here after commenting...
                # width = int(((max_width[c] + 1) &amp;#x2F; tab_width) * tab_width)
                width = ((max_width[c] &amp;#x2F;&amp;#x2F; tab_width) + 1) * tab_width

                print(f&amp;#x27;{item:&amp;lt;{width}}&amp;#x27;, end=&amp;#x27;&amp;#x27;)
&lt;/code&gt;&lt;/pre&gt;
Even while writing this code for the comment for the hundredth time, I&amp;#x27;ve had to fix at least 5 bugs! Truly horrible.</text></item></parent_chain><comment><author>liveoneggs</author><text>&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;perldoc.perl.org&amp;#x2F;perlform&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;perldoc.perl.org&amp;#x2F;perlform&lt;/a&gt;</text></comment>