scribe icon indicating copy to clipboard operation
scribe copied to clipboard

Feat/text alignment

Open cowile opened this issue 5 years ago • 2 comments

Hello,

I attended the DFW Elixir Hack Night on 08/20 hosted by some of the codedge team. It was my first introduction to Elixir, but I liked what I saw and read most of the getting started guide later that night.

As thanks for hosting the event, I wanted to contribute something, so I have implemented cell-level text alignment for your pretty-printed tables. It uses a global format_opt currently. A more advanced feature could change alignment based on the type of data to cover a common convention of left-aligned text and right-aligned numbers, but I have other things on my plate considering offers to change jobs.

cowile avatar Aug 21 '19 20:08 cowile

Thanks for this! Cell alignment has been on my roadmap, so this is a great first step.

I noticed a bug with center alignment. I suspect it's probably rounding errors with integer division.

iex(2)> Scribe.print(data, alignment: :center)
+---------------+---------------------------------------+------------------+--------+-------------------+
|  :__struct__  |                :email                |   :first_name   |  :id  |    :last_name    |
+---------------+---------------------------------------+------------------+--------+-------------------+
|     User     |       "[email protected]"       |   "Henriette"   |  4776  |    "Mitchell"    |
|     User     |      "[email protected]"      |     "Treva"     |  1497  |     "Ziemann"     |
|     User     |        "[email protected]"        |    "Giovanny"    |  1629  |     "Rolfson"     |
|     User     |       "[email protected]"       |     "Baron"     |  9890  |    "Powlowski"    |
|     User     |        "[email protected]"        |     "Ryder"     |  5487  |    "Gottlieb"    |
|     User     |      "[email protected]"      |     "Lukas"     |  1610  |    "Gleichner"    |
|     User     |      "[email protected]"      |      "Faye"      |  9632  |      "Huels"      |
|     User     |      "[email protected]"      |     "Angel"     |  7773  |       "Von"       |
|     User     |         "[email protected]"         |     "Roman"     |  3920  |     "Steuber"     |
|     User     |        "[email protected]"        |     "Leslie"     |  8423  |     "Wiegand"     |
|     User     |     "[email protected]"     |   "Gabrielle"   |  2252  |      "Yost"      |
|     User     |     "[email protected]"     |     "Blanca"     |  8764  |      "Stehr"      |
|     User     |     "[email protected]"     |     "Alysa"     |  3150  |     "Dibbert"     |

hpopp avatar Aug 24 '19 20:08 hpopp

You are correct. More specifically, integer division of an odd number of padding spaces will lose one space. This commit should fix by calculating any remainder spaces needed.

cowile avatar Aug 25 '19 04:08 cowile