<commit_before>// Copyright 2017 The Serulian Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package compilerutil

import (
	"fmt"

	"github.com/serulian/compiler/compilercommon"

	"github.com/fatih/color"
	"github.com/kr/text"
	terminal "github.com/wayneashleyberry/terminal-dimensions"
)

type ConsoleLogLevel int

const (
	InfoLogLevel ConsoleLogLevel = iota
	SuccessLogLevel
	WarningLogLevel
	ErrorLogLevel
)

// LogToConsole logs a message to the console, with the given level and SourceAndLocation.
func LogToConsole(level ConsoleLogLevel, sal compilercommon.SourceAndLocation, message string, args ...interface{}) {
	width, _ := terminal.Width()
	if width <= 0 {
		width = 80
	}

	locationColor := color.New(color.FgWhite)
	messageColor := color.New(color.FgHiWhite)

	prefixColor := color.New(color.FgWhite)
	prefixText := ""

	switch level {
	case InfoLogLevel:
		prefixColor = color.New(color.FgBlue, color.Bold)
		prefixText = "INFO: "

	case SuccessLogLevel:
		prefixColor = color.New(color.FgGreen, color.Bold)
		prefixText = "SUCCESS: "

	case WarningLogLevel:
		prefixColor = color.New(color.FgYellow, color.Bold)
		prefixText = "WARNING: "

	case ErrorLogLevel:
		prefixColor = color.New(color.FgRed, color.Bold)
		prefixText = "ERROR: "
	}

	locationText := fmt.Sprintf("At %v:%v:%v:", sal.Source(), sal.Location().LineNumber()+1, sal.Location().ColumnPosition()+1)
	formattedMessage := fmt.Sprintf(message, args...)

	// If the text will go past the terminal width, then make it multiline and add extra whitespace
	// after it.
	if len(prefixText+locationText+formattedMessage) > int(width) {
		prefixColor.Print(prefixText)
		locationColor.Print(locationText)
		messageColor.Printf("\n%s\n\n", text.Indent(text.Wrap(formattedMessage, int(width)), "  "))
	} else {
		prefixColor.Print(prefixText)
		locationColor.Print(locationText)
		messageColor.Printf(" %s\n", formattedMessage)
	}
}<commit_msg>Fix console wrapping to take account indentation<commit_after>// Copyright 2017 The Serulian Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package compilerutil

import (
	"fmt"

	"github.com/serulian/compiler/compilercommon"

	"github.com/fatih/color"
	"github.com/kr/text"
	terminal "github.com/wayneashleyberry/terminal-dimensions"
)

type ConsoleLogLevel int

const INDENTATION = "  "

const (
	InfoLogLevel ConsoleLogLevel = iota
	SuccessLogLevel
	WarningLogLevel
	ErrorLogLevel
)

// LogToConsole logs a message to the console, with the given level and SourceAndLocation.
func LogToConsole(level ConsoleLogLevel, sal compilercommon.SourceAndLocation, message string, args ...interface{}) {
	width, _ := terminal.Width()
	if width <= 0 {
		width = 80
	}

	locationColor := color.New(color.FgWhite)
	messageColor := color.New(color.FgHiWhite)

	prefixColor := color.New(color.FgWhite)
	prefixText := ""

	switch level {
	case InfoLogLevel:
		prefixColor = color.New(color.FgBlue, color.Bold)
		prefixText = "INFO: "

	case SuccessLogLevel:
		prefixColor = color.New(color.FgGreen, color.Bold)
		prefixText = "SUCCESS: "

	case WarningLogLevel:
		prefixColor = color.New(color.FgYellow, color.Bold)
		prefixText = "WARNING: "

	case ErrorLogLevel:
		prefixColor = color.New(color.FgRed, color.Bold)
		prefixText = "ERROR: "
	}

	locationText := fmt.Sprintf("At %v:%v:%v:", sal.Source(), sal.Location().LineNumber()+1, sal.Location().ColumnPosition()+1)
	formattedMessage := fmt.Sprintf(message, args...)

	// If the text will go past the terminal width, then make it multiline and add extra whitespace
	// after it.
	if len(prefixText+locationText+formattedMessage) > int(width) {
		prefixColor.Print(prefixText)
		locationColor.Print(locationText)
		messageColor.Printf("\n%s\n\n", text.Indent(text.Wrap(formattedMessage, int(width)-len(INDENTATION)), INDENTATION))
	} else {
		prefixColor.Print(prefixText)
		locationColor.Print(locationText)
		messageColor.Printf(" %s\n", formattedMessage)
	}
}
{'fraction_non_alphanumeric': 0.11971658929880283, 'fraction_numerical': 0.004397752259956022, 'mean_word_length': 4.709902370990237, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5412724', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1315, 'n_words': 319}