package formatters

import (
	
	
	

	
)

// TTY16m is a true-colour terminal formatter.
var TTY16m = Register("terminal16m", chroma.FormatterFunc(trueColourFormatter))

var crOrCrLf = regexp.MustCompile(`\r?\n`)

// Print the text with the given formatting, resetting the formatting at the end
// of each line and resuming it on the next line.
//
// This way, a pager (like https://github.com/walles/moar for example) can show
// any line in the output by itself, and it will get the right formatting.
func writeToken( io.Writer,  string,  string) {
	if  == "" {
		fmt.Fprint(, )
		return
	}

	 := crOrCrLf.FindAllStringIndex(, -1)

	 := 0
	for ,  := range  {
		,  := [0], [1]
		fmt.Fprint(, )
		fmt.Fprint(, [:])
		fmt.Fprint(, "\033[0m")
		fmt.Fprint(, [:])
		 = 
	}

	if  < len() {
		// Print whatever is left after the last newline
		fmt.Fprint(, )
		fmt.Fprint(, [:])
		fmt.Fprint(, "\033[0m")
	}
}

func trueColourFormatter( io.Writer,  *chroma.Style,  chroma.Iterator) error {
	 = clearBackground()
	for  := ();  != chroma.EOF;  = () {
		 := .Get(.Type)
		if .IsZero() {
			fmt.Fprint(, .Value)
			continue
		}

		 := ""
		if .Bold == chroma.Yes {
			 += "\033[1m"
		}
		if .Underline == chroma.Yes {
			 += "\033[4m"
		}
		if .Italic == chroma.Yes {
			 += "\033[3m"
		}
		if .Colour.IsSet() {
			 += fmt.Sprintf("\033[38;2;%d;%d;%dm", .Colour.Red(), .Colour.Green(), .Colour.Blue())
		}
		if .Background.IsSet() {
			 += fmt.Sprintf("\033[48;2;%d;%d;%dm", .Background.Red(), .Background.Green(), .Background.Blue())
		}

		writeToken(, , .Value)
	}
	return nil
}