#knit_string - A composable, Gleamy string formatting library!

1 messages · Page 1 of 1 (latest)

verbal matrix
#

knit

Knit your strings!


knit is a Gleam library for formatting strings.
It is a replacement for my older glormat library, which had some fundamental issues in its design.[^1]

knit doesn't try to replace the string concatenation operator <>, it focuses on formatting values and leaves concatenation to the operator that was designed for it!

Usage

Rather than writing full formatters inline- which takes up lots of space- knit returns composed formatters that can be bound to short variable names (e.g. pad, crop, align, fmt, etc.), which are then called inline with the <> operator to paste the resulting Strings together.

Formatters are composable, and can be chained together to create more complex formatters. It's recommended to use the use keyword for this:

import gleam/io
import gleam/list
import gleam/string

import knit

pub fn main() {
  let header = {
    let title_case = {
      use val <- knit.with
      string.split(val, " ") |> list.map(string.capitalise) |> string.join(" ")
    }

    use val <- knit.new
    title_case(val)
    |> knit.margin_centre(2, " ")
    |> knit.pad_centre(48, "#")
    |> knit.margin_right(1, "\n")
  }

  let body_line = {
    use val <- knit.new
    val
    |> knit.pad_left(40, " ")
    |> knit.margin_centre(6, " ")
    |> knit.margin_centre(2, "|")
    |> knit.margin_right(1, "\n")
  }

  let value = knit.new(knit.pad_right(_, 16, " "))

  io.println(
    header("gleam sponsorship receipt")
    <> body_line("NAME: " <> value("Jane Doe"))
    <> body_line("")
    <> body_line("SUBTOTAL: " <> value("$16"))
    <> body_line("EST. TAX: " <> value("1.50 hugs"))
    <> body_line("TOTAL: " <> value("$16 + 1.50 hugs")),
  )
}
########## Gleam Sponsorship Receipt ###########
|                     NAME: Jane Doe           |
|                                              |
|                 SUBTOTAL: $16                |
|                 EST. TAX: 1.50 hugs          |
|                    TOTAL: $16 + 1.50 hugs    |

Further documentation can be found at https://hexdocs.pm/knit.

Development

gleam run   # Run the project
gleam test  # Run the tests

[^1]: My older library glormat attempted to mimic the string formatting API style found in other languages such as Python or Rust- but without compile-time evaluation or some other kind of metaprogramming, the code had to be messy in order to work and didn't feel great to use. It also didn't really take advantage of Gleam's useful features.

https://hex.pm/packages/knit_string

warped leaf
#

whoa

#

That's neat

midnight vale
#

such a cool lib!

warped leaf
#

Now I wanna make loads of retro text UIs

verbal matrix
#

hahaha

#

must admit string_width would be better for that purpose, but i'm hoping knit will serve as a good formatting lib that's easy to reach for and doesn't do too much

warped leaf
midnight vale
verbal matrix
midnight vale
verbal matrix
#

i looked into it earlier and it seems very comprehensive and can do more sophisticated layout stuff

#

i'm not trying to handle layouts really, just provide an easy tool that extends as far as you want it to, without slowing things down or being cumbersome in simple cases

midnight vale
verbal matrix
#

the name is misleading 😂

jolly pewter
#

at some point I will split it up into 2 packages, I promise 😅

having a simpler thing is very nice too, very cool!!

verbal matrix
#

ty!! pink_hearts

warped leaf
#

idk why iv isn't called list_length