M
Modular3mo ago
toasty

Mist and Weave: ANSI Styling and text formatting libraries

Mist mist lets you safely use advanced styling options on the terminal. It offers you convenient methods to colorize and style your output, without you having to deal with all kinds of weird ANSI escape sequences and color conversions. https://github.com/thatstoasty/mist
import mist

fn main():
# ANSI Color Support (0-15)
var style = mist.new_style().foreground(12)
print(style.render(a))

# ANSI256 Color Support (16-255)
style = mist.new_style().foreground(55)
print(style.render(a))

# RGBColor Support (Hex Codes)
style = mist.new_style().foreground(0xc9a0dc)
print(style.render(a))
import mist

fn main():
# ANSI Color Support (0-15)
var style = mist.new_style().foreground(12)
print(style.render(a))

# ANSI256 Color Support (16-255)
style = mist.new_style().foreground(55)
print(style.render(a))

# RGBColor Support (Hex Codes)
style = mist.new_style().foreground(0xc9a0dc)
print(style.render(a))
Weave weave is a collection of (ANSI-sequence aware) text reflow operations & algorithms. Includes text padding, margins, truncation, wrapping, word wrapping, indenting, and dedenting. https://github.com/thatstoasty/weave eg
from weave import indent

fn main() raises:
print(indent("Hello\nWorld\n TEST!", 5))
from weave import indent

fn main() raises:
print(indent("Hello\nWorld\n TEST!", 5))
Hello
World
TEST!
Hello
World
TEST!
Both of these projects are used in mog, but if you need something lightweight to apply some texting coloring/styling or simple ANSI aware formatting, you can use one of these libraries instead.
1 Reply
toasty
toasty3mo ago
Just pushed a few QOL changes for mist. The TerminalStyle struct can now be an alias! It’s not much faster than just declaring it as a var, but the real win is that styles can now be a var or alias at the file scope after the refactor. To do this I had to remove any usage of str(), try, and dictionaries from the critical path of the code. If declaring it as an alias, a profile must given as an arg though. Querying the terminal for the colors it supports cannot happen at compile time. Made a fairly big change to how mist works. It no longer uses hex codes that are text eg #ffffff but instead it uses integers 0xffffff. This helped unify how ANSI and Hex color codes were being handled and lead to a pretty substantial improvement in the benchmarks! The text version is a bit nicer on the eyes, but not needing to handle variants and string conversions enabled mog styles to be defined as aliases and file scope vars. I was surprised to see the time it takes to render colored text drop by 90%. I ran a few thousand iterations on my Mac M2 and the mean dropped from roughly 0.035ms to 0.0035ms. I guess converting a String to an int then back again for the ansi sequence was eating up a lot of time. This change also lead to a 50% speedup for mog as well! I'm expecting that lib to be impacted by String operations much more, since it's splitting strings into lines and recombining it several times when it renders the content.
Want results from more Discord servers?
Add your server