1. package formatters
    
  2. 
    
  3. import (
    
  4. 	"fmt"
    
  5. 	"io"
    
  6. 
    
  7. 	"github.com/alecthomas/chroma/v2"
    
  8. )
    
  9. 
    
  10. // TTY16m is a true-colour terminal formatter.
    
  11. var TTY16m = Register("terminal16m", chroma.FormatterFunc(trueColourFormatter))
    
  12. 
    
  13. func trueColourFormatter(w io.Writer, style *chroma.Style, it chroma.Iterator) error {
    
  14. 	style = clearBackground(style)
    
  15. 	for token := it(); token != chroma.EOF; token = it() {
    
  16. 		entry := style.Get(token.Type)
    
  17. 		if !entry.IsZero() {
    
  18. 			out := ""
    
  19. 			if entry.Bold == chroma.Yes {
    
  20. 				out += "\033[1m"
    
  21. 			}
    
  22. 			if entry.Underline == chroma.Yes {
    
  23. 				out += "\033[4m"
    
  24. 			}
    
  25. 			if entry.Italic == chroma.Yes {
    
  26. 				out += "\033[3m"
    
  27. 			}
    
  28. 			if entry.Colour.IsSet() {
    
  29. 				out += fmt.Sprintf("\033[38;2;%d;%d;%dm", entry.Colour.Red(), entry.Colour.Green(), entry.Colour.Blue())
    
  30. 			}
    
  31. 			if entry.Background.IsSet() {
    
  32. 				out += fmt.Sprintf("\033[48;2;%d;%d;%dm", entry.Background.Red(), entry.Background.Green(), entry.Background.Blue())
    
  33. 			}
    
  34. 			fmt.Fprint(w, out)
    
  35. 		}
    
  36. 		fmt.Fprint(w, token.Value)
    
  37. 		if !entry.IsZero() {
    
  38. 			fmt.Fprint(w, "\033[0m")
    
  39. 		}
    
  40. 	}
    
  41. 	return nil
    
  42. }