Odin CLI Best Practices

Learn to build cli apps in Odin. Read best practices. Ask away if you need any help with your app.

1 Like

Odin color

A simple ANSI color package similar to Rust’s colored crate.

3 Likes

Get the length of a string, excluding ansi codes:

count_chars :: proc(s: string) -> int {
	result := 0
	inside_ansi := false

	for i in s {
		if i == '\x1b' {
			inside_ansi = true
		}
		if inside_ansi {
			if i == 'm' {
				inside_ansi = false
			}
		} else {
			result += 1
		}
	}
	return result
}
2 Likes

When it comes to measuring string width in the terminal you’ll also want to note that some characters are more than 1 column wide. I’ve borrowed from string-width/index.js at main · sindresorhus/string-width · GitHub before for measuring string width in CLIs. package unicode - pkg.odin-lang.org will come in very handy. For emojis I’ve used GitHub - mathiasbynens/emoji-regex: A regular expression to match all Emoji-only symbols as per the Unicode Standard. as a reference.

I’ve also found ANSI Escape Codes · GitHub to be a helpful reference at times.

1 Like

I also have this note

Double-underline per ECMA-48, but instead disables bold intensity on several terminals, including in the Linux kernel’s console before version 4.17.

in my Elm ANSI package because it’s something I missed in that GitHub gist of ANSI Escape Codes. It’s a small note at the bottom of the Colors / Graphics Mode section.

@Feoramund wrote code that determines string width for terminal applications. See core:text/table for how it does this for tabular displays of unicode strings.

3 Likes

That’s excellent! Not sure I would have thought to look there, but still helpful to know it’s available.

1 Like