Created
February 15, 2026 15:09
-
-
Save meysampg/9921fc51a41f3199b01b2e40177a9a04 to your computer and use it in GitHub Desktop.
creating center padded box on terminal output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func padCenter(s string, w int) string { | |
| if len(s) >= w { | |
| return s[:w] | |
| } | |
| left := (w - len(s)) / 2 | |
| right := w - len(s) - left | |
| return strings.Repeat(" ", left) + s + strings.Repeat(" ", right) | |
| } | |
| func box(left string, leftn int, right string, rightn int) string { | |
| return "|" + padCenter(left, leftn) + "|" + padCenter(right, rightn) + "|" | |
| } | |
| func box1(all string) string { // 32 bits | |
| return "|" + padCenter(all, 63) + "|" | |
| } | |
| func box2(left, right string) string { // 16 bits | 16 bits | |
| return box(left, 31, right, 31) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment