Skip to content

Center alignment for fmt #16885

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
Sep 4, 2014
Merged

Center alignment for fmt #16885

merged 1 commit into from
Sep 4, 2014

Conversation

wickerwaka
Copy link

Use '^' to specify center alignment in format strings.

fmt!( "[{:^5s}]", "Hi" ) -> "[ Hi  ]"
fmt!( "[{:^5s}]", "H" )  -> "[  H  ]"
fmt!( "[{:^5d}]", 1i )   -> "[  1  ]"
fmt!( "[{:^5d}]", -1i )  -> "[ -1  ]"
fmt!( "[{:^6d}]", 1i )   -> "[  1   ]"
fmt!( "[{:^6d}]", -1i )  -> "[  -1  ]"

If the padding is odd then the padding on the right will be one
character longer than the padding on the left.

@ftxqxd
Copy link
Contributor

ftxqxd commented Aug 30, 2014

Python uses ^ instead of |, which I prefer for consistency with < and >. Rust’s format syntax comes from Python, after all.

@wickerwaka
Copy link
Author

Good point, I wasn't aware of the python heritage. Changed it to '^'

rt::AlignLeft => ( 0u, padding ),
rt::AlignRight | rt::AlignUnknown => ( padding, 0u ),
rt::AlignCenter => ( padding / 2, ( padding + 1 ) / 2 ),
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stylistically we normally don't put spaces around the parens in tuples

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Squashed the tuples down!

@alexcrichton
Copy link
Member

This looks like a solid implementation @wickerwaka, thanks! @P1start is right in that the design was influenced by python, so I think it makes sense to go ahead and implement this as well.

@sfackler
Copy link
Member

sfackler commented Sep 4, 2014

The module docs should be updated as well: http://doc.rust-lang.org/std/fmt/#fill/alignment

Use '^' to specify center alignment in format strings.

fmt!( "[{:^5s}]", "Hi" ) -> "[ Hi  ]"
fmt!( "[{:^5s}]", "H" )  -> "[  H  ]"
fmt!( "[{:^5d}]", 1i )   -> "[  1  ]"
fmt!( "[{:^5d}]", -1i )  -> "[ -1  ]"
fmt!( "[{:^6d}]", 1i )   -> "[  1   ]"
fmt!( "[{:^6d}]", -1i )  -> "[  -1  ]"

If the padding is odd then the padding on the right will be one
character longer than the padding on the left.

Tuples squashed
bors added a commit that referenced this pull request Sep 4, 2014
Use '^' to specify center alignment in format strings.

```
fmt!( "[{:^5s}]", "Hi" ) -> "[ Hi  ]"
fmt!( "[{:^5s}]", "H" )  -> "[  H  ]"
fmt!( "[{:^5d}]", 1i )   -> "[  1  ]"
fmt!( "[{:^5d}]", -1i )  -> "[ -1  ]"
fmt!( "[{:^6d}]", 1i )   -> "[  1   ]"
fmt!( "[{:^6d}]", -1i )  -> "[  -1  ]"
```

If the padding is odd then the padding on the right will be one
character longer than the padding on the left.
@bors bors closed this Sep 4, 2014
@bors bors merged commit 2bc4a5e into rust-lang:master Sep 4, 2014
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 31, 2024
fix: Improve error recovery for match arms

This should make use of the recovery token sets, but I think it'd be better to fix that as a whole while fixing the other places for these adhoc recovery checks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants