-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Feat/table alignment #6230
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
Feat/table alignment #6230
Conversation
1b8af8e
to
98dcc44
Compare
lib/elixir/lib/io/ansi/docs.ex
Outdated
@@ -291,6 +291,8 @@ defmodule IO.ANSI.Docs do | |||
col = | |||
col | |||
|> String.replace("\\\|", "|") | |||
|> String.replace(~r/-+/, "-") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String.replace(["+", "-"], "-")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@josevalim That's not quite the same thing. My version replaces :-------
with :-
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I completely spaced out on the +
. We cannot use regexes because it breaks precompiled Elixir on OTP 20. The few cases we do we need to call Regex.recompile
. Ideally we want to find a way to write this that does not rely on regexes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a chance this code will replace foo -- bar
in a column to foo - bar
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hrm, it will. I think we need to do this squashing only after we detect the first line is a table_header?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@josevalim We can probably do it by excluding the header lines from the length count. That gets rid of the regex and the need to trim down the "-".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me.
lib/elixir/lib/io/ansi/docs.ex
Outdated
Enum.map_join(cols_and_widths, " | ", fn {{col, length}, width} -> | ||
col <> String.duplicate(" ", width - length) | ||
Enum.map_join(cols_and_widths_and_aligns, " | ", fn {{{col, length}, width}, align} -> | ||
case align do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move this case to a function as things are getting wild here!
@@ -303,6 +303,12 @@ defmodule IO.ANSI.DocsTest do | |||
"\e[7mcolumn 1 | and 2\e[0m\na | b \none | two \n\e[0m" | |||
end | |||
|
|||
test "table with heading alignment" do | |||
table = "column 1 | 2 | and three\n-----: | :------: | :-----\n a | even | c\none | odd | three" | |||
expected = "\e[7mcolumn 1 | 2 | and three\e[0m\n a | even | c \n one | odd | three \n\e[0m" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use heredocs for those strings? You just need to append <> "\e[0m" at the end I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can, I was just following the style of the other tests in the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test "table with heading alignment" do
table = """
column 1 | 2 | and three
-------: | :------: | :-----
a | even | c\none | odd | three
"""
expected = """
\e[7mcolumn 1 | 2 | and three\e[0m
a | even | c
one | odd | three
\e[0m
""" |> String.trim_trailing
The expected
doesn't look greate with heredoc because of the \e[7m
and the required trailing whitespace at the end. I can keep it if you want though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. It is ok to not do it then, your call.
@josevalim Please approve if it's ok and I'll squash the commits and rewrite the commit message to be more appropriate. |
This commit adds table alignment in markdown which allows the following for tables: right aligned | center aligned | left aligned -: | :-: | :- a | b | c This will output a table in the following format: right aligned | center aligned | left aligned a | b | c If headings are set, the header line (the line immediately below the headings is ignored when calculating the max width for the columns. All lines are trimmed of additional whitespace leading and trailing whitespace to prevent tables that look like: right aligned | center aligned | left aligned ------------: | :------------: | :- a | b | c right aligned | center aligned | left aligned a | b | c
@josevalim All ready to merge now. |
d4bdda6
to
f29796f
Compare
Thank you ❤️ |
I've done this as two commits as the second commit is technically a breaking change for the way the docs are rendered. You can see the result of of the same table being rendered with:
Commit messages below for convenience.
Add table column alignment in IO.ANSI.Docs
This commit adds table alignment in markdown which allows the following
for tables:
This will output a table in the following format:
Remove duplicate spacings for table alignment
Previously, a table in the format:
Would be displayed as:
This commit changes it so it displays as: