package term

import "core:fmt"
import "core:strings"

Style :: struct {
	fg:   Maybe(int),
	bg:   Maybe(int),
	bold: bool,
}

current_style: Style // what the terminal currently has active

reset_style :: proc(sb:^strings.Builder) {
	apply_style(sb, {})
}

apply_style :: proc(sb: ^strings.Builder, target: Style) {
	if target == current_style {
		return
	}

	codes: [dynamic; 8]string

	if target.bold != current_style.bold {
		append(&codes, target.bold ? "1" : "22")
	}

	if target.fg != current_style.fg {
		if fg, ok := target.fg.?; ok {
			append(&codes, fmt.tprintf("%d", fg))
		} else {
			append(&codes, "39")
		}
	}

	if target.bg != current_style.bg {
		if bg, ok := target.bg.?; ok {
			append(&codes, fmt.tprintf("%d", bg))
		} else {
			append(&codes, "49")
		}
	}

	if len(codes) > 0 {
		strings.write_string(sb, fmt.tprintf("\x1b[%sm", strings.join(codes[:], ";")))
	}

	current_style = target
}