Skip to content

[feat] Support justify alignment #168

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

Merged
merged 1 commit into from
Apr 27, 2021
Merged

[feat] Support justify alignment #168

merged 1 commit into from
Apr 27, 2021

Conversation

nam-hle
Copy link
Collaborator

@nam-hle nam-hle commented Apr 25, 2021

This PR adds the justify alignment feature (#167). The solution is to calculate the number array of adding spaces, which has a length that is equal to the number of continuous space sequences and has a sum is equal to the available width. To be prettier, the array should be a non-decreasing one, and the difference between the maximum and minimum value is not greater than 1.

Example with total width = 10:

alignString('a b c')     = 'a    b   c'
alignString('a b c d')   = 'a  b  c  d'
alignString('a b c d e') = 'a  b c d e'

The issue is when the string is too short (e.g., the last line of a paragraph), I propose to fallback to left align if we have to add more than 03 spaces in one place (instead of calculating more information about the surrounding lines). So:

alignString('a b c', 11, 'justify')     = 'a    b    c'
alignString('a b c', 12, 'justify')     = 'a b c       '

Here is the demo from moonMission file with justify the last column:
image

@coveralls
Copy link

coveralls commented Apr 25, 2021

Pull Request Test Coverage Report for Build 330

  • 19 of 19 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 325: 0.0%
Covered Lines: 363
Relevant Lines: 363

💛 - Coveralls

@nam-hle nam-hle marked this pull request as ready for review April 27, 2021 01:01
@gajus gajus merged commit 4ac6f00 into gajus:master Apr 27, 2021
@gajus
Copy link
Owner

gajus commented Apr 27, 2021

These PRs are great!

1 similar comment
@gajus
Copy link
Owner

gajus commented Apr 27, 2021

These PRs are great!

@gajus
Copy link
Owner

gajus commented Apr 27, 2021

🎉 This PR is included in version 6.6.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants