[Odin] String Builder shenanigans[Odin] String Builder shenanigans
👉
Right-Align
Week 22, 2026
package main
import "core:fmt"
import "core:strings"
main :: proc () {
text_1 := "abcdefghijklmnopqrstuvwxyz"
text_2 := "There's poetry in nature. A symmetry - Saffron A. Kent"
text_3 := "We do not inherit the Earth from our ancestors; we borrow it from our " +
"children. Every part of this Earth is sacred, and we must care for it " +
"as we would care for our own lives - Chief Seattle"
inputs: [] struct {
text : string,
line_width : int,
starting_chars : int,
char_step : int,
} = {
{ text_1, 8, 1, 1 },
{ text_1, 20, 2, 4 },
{ text_1, 20, 10, -2 },
{ text_2, 5, 5, 0 },
{ text_2, 15, 1, 2 },
{ text_3, 20, 1, 1 },
{ text_3, 10, 1, 0 },
{ text_3, 50, 50, -6 },
}
for i in inputs {
fmt.printfln("Input: %#v", i)
fmt.println()
fmt.println("Result:")
fmt.println("|0--------|10-------|20-------|30-------|40-------|50-------|60----")
fmt.println(right_align(i.text, i.line_width, i.starting_chars, i.char_step))
fmt.println("|---------|---------|---------|---------|---------|---------|------")
fmt.println()
}
}
right_align :: proc (text: string, line_width, starting_chars, char_step: int) -> string {
sb := strings.builder_make()
written_chars := 0
max_chars := starting_chars
for c, i in text {
if written_chars == 0 {
if i > 0 do strings.write_byte(&sb, '\n')
starting_spaces := line_width - max_chars
for _ in 0..