~mrms/fmtum

fa5771abbf92e8807c538a0f31c798c3bf29bfb9 — Marek Ma┼íkarinec 7 months ago
Init commit
4 files changed, 101 insertions(+), 0 deletions(-)

A .build.yml
A README.md
A fmt.um
A pak.json
A  => .build.yml +27 -0
@@ 1,27 @@
image: debian/bullseye
packages:
- python3
- python3-pip
sources:
- https://git.sr.ht/~mrms/fmtum
- https://git.sr.ht/~mrms/pak
environment:
  project: fmt
secrets:
- c591317d-851e-4258-ab5e-884ca5c962c5
shell: false
tasks:
- setup: |
    cd pak
    python3 -m pip install -r requirements.txt
    ./setup.py install --user
- build: |
    cd fmtum
    python3 -m pak update
    python3 -m pak build
- deploy: |
    cd fmtum
    set +x
    python3 -m pak upload -t `cat ../.secret` pak.tar
    python3 -m pak upload -t `cat ../.secret` pak.json
    set -x

A  => README.md +7 -0
@@ 1,7 @@
# fmt

Alternative formatting function

## license

Unlicense/MIT

A  => fmt.um +54 -0
@@ 1,54 @@
import (
	"std.um"
)

//~~fn fmt
// "{}" in the string `s` will be subtituted for the arguments
fn fmt*(s: str, args: ..any): str {
//~~
	o := []char{}
	a := 0

	l := len(s)
	for i:=0; i < l; i++ {
		// handle \{
		if s[i] == '\\' && i < l-1 && s[i+1] == '{' {
			o = append(o, '{')
			i++
			continue
		}

		// if normal character, just add it to the output
		if s[i] != '{' {
			o = append(o, s[i])
			continue
		}

		if a >= len(args) {
			error("sfmt: not enough arguments")
		}

		o = append(o, []char(sprintf("%v", args[a])))
		a++

		// skip the }
		i++
	}

	if a != len(args) {
		error("sfmt: too many arguments")
	}

	return o
}

type vec2 = struct {
	x, y: int
}

fn main() {
	printf("basic usage: %s\n", fmt("a number {}, a string {}", 32, "hello"))
	printf("skip format tag: %s\n", fmt("skip me \\{}"))
	printf("complex types: %s\n", fmt("what can this print? {} {}",
		vec2{2, 4}, []int{1, 2, 3, 4}))
}

A  => pak.json +13 -0
@@ 1,13 @@
{
    "name": "fmt",
    "version": "v0.1.0",
    "author": "Marek Maškarinec <marek@mrms.cz>",
    "license": "Unlicense/MIT",
    "description": "Alternative formatting function",
    "readme": "README.md",
    "link": "",
    "dependencies": [],
    "include": ["fmt.um"],
    "run_posix": "./pak/umka/linux/umka fmt.um",
    "run_windows": ".\\pak\\umka\\windows\\umka.exe fmt.um"
}