small refactor to log formatting

This commit is contained in:
Evan Wallace
2021-03-25 12:35:12 -07:00
parent 5832816c38
commit 6fa75759dd
5 changed files with 96 additions and 138 deletions

View File

@@ -13,16 +13,16 @@ import (
var helpText = func(colors logger.Colors) string {
return `
` + colors.Bold + `Usage:` + colors.Default + `
` + colors.Bold + `Usage:` + colors.Reset + `
esbuild [options] [entry points]
` + colors.Bold + `Documentation:` + colors.Default + `
` + colors.Underline + `https://esbuild.github.io/` + colors.Default + `
` + colors.Bold + `Documentation:` + colors.Reset + `
` + colors.Underline + `https://esbuild.github.io/` + colors.Reset + `
` + colors.Bold + `Repository:` + colors.Default + `
` + colors.Underline + `https://github.com/evanw/esbuild` + colors.Default + `
` + colors.Bold + `Repository:` + colors.Reset + `
` + colors.Underline + `https://github.com/evanw/esbuild` + colors.Reset + `
` + colors.Bold + `Simple options:` + colors.Default + `
` + colors.Bold + `Simple options:` + colors.Reset + `
--bundle Bundle all dependencies into the output files
--define:K=V Substitute K with V while parsing
--external:M Exclude module M from the bundle (can use * wildcards)
@@ -44,7 +44,7 @@ var helpText = func(colors logger.Colors) string {
safari11, edge16, node10, default esnext)
--watch Watch mode: rebuild on file system changes
` + colors.Bold + `Advanced options:` + colors.Default + `
` + colors.Bold + `Advanced options:` + colors.Reset + `
--asset-names=... Path template to use for "file" loader files
(default "[name]-[hash]")
--banner:T=... Text to be prepended to each output file of type T
@@ -92,23 +92,23 @@ var helpText = func(colors logger.Colors) string {
--tsconfig=... Use this tsconfig.json file instead of other ones
--version Print the current version (` + esbuildVersion + `) and exit
` + colors.Bold + `Examples:` + colors.Default + `
` + colors.Dim + `# Produces dist/entry_point.js and dist/entry_point.js.map` + colors.Default + `
` + colors.Bold + `Examples:` + colors.Reset + `
` + colors.Dim + `# Produces dist/entry_point.js and dist/entry_point.js.map` + colors.Reset + `
esbuild --bundle entry_point.js --outdir=dist --minify --sourcemap
` + colors.Dim + `# Allow JSX syntax in .js files` + colors.Default + `
` + colors.Dim + `# Allow JSX syntax in .js files` + colors.Reset + `
esbuild --bundle entry_point.js --outfile=out.js --loader:.js=jsx
` + colors.Dim + `# Substitute the identifier RELEASE for the literal true` + colors.Default + `
` + colors.Dim + `# Substitute the identifier RELEASE for the literal true` + colors.Reset + `
esbuild example.js --outfile=out.js --define:RELEASE=true
` + colors.Dim + `# Provide input via stdin, get output via stdout` + colors.Default + `
` + colors.Dim + `# Provide input via stdin, get output via stdout` + colors.Reset + `
esbuild --minify --loader=ts < input.ts > output.js
` + colors.Dim + `# Automatically rebuild when input files are changed` + colors.Default + `
` + colors.Dim + `# Automatically rebuild when input files are changed` + colors.Reset + `
esbuild app.ts --bundle --watch
` + colors.Dim + `# Start a local HTTP server for everything in "www"` + colors.Default + `
` + colors.Dim + `# Start a local HTTP server for everything in "www"` + colors.Reset + `
esbuild app.ts --bundle --servedir=www --outdir=www/js
`

View File

@@ -495,9 +495,10 @@ func PrintMessageToStderr(osArgs []string, msg Msg) {
}
type Colors struct {
Default string
Bold string
Dim string
Reset string
Bold string
Dim string
Underline string
Red string
Green string
@@ -506,8 +507,21 @@ type Colors struct {
Cyan string
Magenta string
Yellow string
}
Underline string
var terminalColors = Colors{
Reset: "\033[0m",
Bold: "\033[1m",
Dim: "\033[37m",
Underline: "\033[4m",
Red: "\033[31m",
Green: "\033[32m",
Blue: "\033[34m",
Cyan: "\033[36m",
Magenta: "\033[35m",
Yellow: "\033[33m",
}
func PrintText(file *os.File, level LogLevel, osArgs []string, callback func(Colors) string) {
@@ -534,19 +548,7 @@ func PrintTextWithColor(file *os.File, useColor UseColor, callback func(Colors)
var colors Colors
if useColorEscapes {
colors.Default = colorReset
colors.Bold = colorResetBold
colors.Dim = colorResetDim
colors.Red = colorRed
colors.Green = colorGreen
colors.Blue = colorBlue
colors.Cyan = colorCyan
colors.Magenta = colorMagenta
colors.Yellow = colorYellow
colors.Underline = colorResetUnderline
colors = terminalColors
}
writeStringWithColor(file, callback(colors))
}
@@ -725,12 +727,12 @@ func PrintSummary(useColor UseColor, table SummaryTable, start *time.Time) {
dir,
colors.Bold,
base,
colors.Default,
colors.Reset,
strings.Repeat(" ", spacer),
sizeColor,
entry.Size,
sizeWarning,
colors.Default,
colors.Reset,
))
}
@@ -740,7 +742,7 @@ func PrintSummary(useColor UseColor, table SummaryTable, start *time.Time) {
if length == maxLength+1 {
plural = ""
}
sb.WriteString(fmt.Sprintf("%s%s...and %d more output file%s...%s\n", margin, colors.Dim, length-maxLength, plural, colors.Default))
sb.WriteString(fmt.Sprintf("%s%s...and %d more output file%s...%s\n", margin, colors.Dim, length-maxLength, plural, colors.Reset))
}
}
sb.WriteByte('\n')
@@ -758,7 +760,7 @@ func PrintSummary(useColor UseColor, table SummaryTable, start *time.Time) {
lightningSymbol,
colors.Green,
time.Since(*start).Milliseconds(),
colors.Default,
colors.Reset,
))
}
@@ -796,21 +798,6 @@ func NewDeferLog() Log {
}
}
const colorReset = "\033[0m"
const colorRed = "\033[31m"
const colorGreen = "\033[32m"
const colorBlue = "\033[34m"
const colorCyan = "\033[36m"
const colorMagenta = "\033[35m"
const colorYellow = "\033[33m"
const colorResetDim = "\033[0;37m"
const colorBold = "\033[1m"
const colorResetBold = "\033[0;1m"
const colorResetUnderline = "\033[0;4m"
type UseColor uint8
const (
@@ -882,9 +869,14 @@ func emptyMarginText(maxMargin int, isLast bool) string {
}
func msgString(options OutputOptions, terminalInfo TerminalInfo, kind MsgKind, data MsgData, maxMargin int) string {
var colors Colors
if terminalInfo.UseColorEscapes {
colors = terminalColors
}
var kindColor string
textColor := colorBold
textResetColor := colorResetBold
prefixColor := colors.Bold
messageColor := colors.Bold
textIndent := ""
if options.IncludeSource {
@@ -893,15 +885,15 @@ func msgString(options OutputOptions, terminalInfo TerminalInfo, kind MsgKind, d
switch kind {
case Error:
kindColor = colorRed
kindColor = colors.Red
case Warning:
kindColor = colorMagenta
kindColor = colors.Magenta
case Note:
textColor = colorReset
kindColor = colorResetBold
textResetColor = colorReset
prefixColor = colors.Reset
kindColor = colors.Bold
messageColor = ""
if options.IncludeSource {
textIndent = " "
}
@@ -911,68 +903,38 @@ func msgString(options OutputOptions, terminalInfo TerminalInfo, kind MsgKind, d
}
if data.Location == nil {
if terminalInfo.UseColorEscapes {
return fmt.Sprintf("%s%s%s%s: %s%s%s\n",
textColor, textIndent, kindColor, kind.String(),
textResetColor, data.Text,
colorReset)
}
return fmt.Sprintf("%s%s: %s\n", textIndent, kind.String(), data.Text)
return fmt.Sprintf("%s%s%s%s: %s%s%s\n%s",
prefixColor, textIndent, kindColor, kind.String(),
colors.Reset, messageColor, data.Text,
colors.Reset)
}
if !options.IncludeSource {
if terminalInfo.UseColorEscapes {
return fmt.Sprintf("%s%s%s: %s%s: %s%s%s\n",
textColor, textIndent, data.Location.File,
kindColor, kind.String(),
textResetColor, data.Text,
colorReset)
}
return fmt.Sprintf("%s%s: %s: %s\n",
textIndent, data.Location.File, kind.String(), data.Text)
return fmt.Sprintf("%s%s%s: %s%s: %s%s%s\n%s",
prefixColor, textIndent, data.Location.File,
kindColor, kind.String(),
colors.Reset, messageColor, data.Text,
colors.Reset)
}
d := detailStruct(data, terminalInfo, maxMargin)
if terminalInfo.UseColorEscapes {
if d.Suggestion != "" {
return fmt.Sprintf("%s%s%s:%d:%d: %s%s: %s%s\n%s%s%s%s%s%s\n%s%s%s%s%s\n%s%s%s%s%s%s%s\n",
textColor, textIndent, d.Path, d.Line, d.Column,
kindColor, kind.String(),
textResetColor, d.Message,
colorResetDim, d.SourceBefore, colorGreen, d.SourceMarked, colorResetDim, d.SourceAfter,
emptyMarginText(maxMargin, false), d.Indent, colorGreen, d.Marker, colorResetDim,
emptyMarginText(maxMargin, true), d.Indent, colorGreen, d.Suggestion, colorResetDim,
d.ContentAfter, colorReset)
}
return fmt.Sprintf("%s%s%s:%d:%d: %s%s: %s%s\n%s%s%s%s%s%s\n%s%s%s%s%s%s%s\n",
textColor, textIndent, d.Path, d.Line, d.Column,
kindColor, kind.String(),
textResetColor, d.Message,
colorResetDim, d.SourceBefore, colorGreen, d.SourceMarked, colorResetDim, d.SourceAfter,
emptyMarginText(maxMargin, true), d.Indent, colorGreen, d.Marker, colorResetDim,
d.ContentAfter, colorReset)
}
callout := d.Marker
calloutPrefix := ""
if d.Suggestion != "" {
return fmt.Sprintf("%s%s:%d:%d: %s: %s\n%s%s%s\n%s%s%s\n%s%s%s%s\n",
textIndent, d.Path, d.Line, d.Column,
kind.String(), d.Message,
d.SourceBefore, d.SourceMarked, d.SourceAfter,
emptyMarginText(maxMargin, false), d.Indent, d.Marker,
emptyMarginText(maxMargin, true), d.Indent, d.Suggestion,
d.ContentAfter)
callout = d.Suggestion
calloutPrefix = fmt.Sprintf("%s%s%s%s%s\n",
emptyMarginText(maxMargin, false), d.Indent, colors.Green, d.Marker, colors.Dim)
}
return fmt.Sprintf("%s%s:%d:%d: %s: %s\n%s%s%s\n%s%s%s%s\n",
textIndent, d.Path, d.Line, d.Column,
kind.String(), d.Message,
d.SourceBefore, d.SourceMarked, d.SourceAfter,
emptyMarginText(maxMargin, true), d.Indent, d.Marker,
d.ContentAfter)
return fmt.Sprintf("%s%s%s:%d:%d: %s%s: %s%s%s\n%s%s%s%s%s%s%s\n%s%s%s%s%s%s%s\n%s",
prefixColor, textIndent, d.Path, d.Line, d.Column,
kindColor, kind.String(),
colors.Reset, messageColor, d.Message,
colors.Reset, colors.Dim, d.SourceBefore, colors.Green, d.SourceMarked, colors.Dim, d.SourceAfter,
calloutPrefix, emptyMarginText(maxMargin, true), d.Indent, colors.Green, callout, colors.Dim, d.ContentAfter,
colors.Reset)
}
type MsgDetail struct {

View File

@@ -67,49 +67,45 @@ func writeStringWithColor(file *os.File, text string) {
i++
continue
case strings.HasPrefix(text[i:], colorReset):
i += len(colorReset)
case strings.HasPrefix(text[i:], terminalColors.Reset):
i += len(terminalColors.Reset)
attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
case strings.HasPrefix(text[i:], colorRed):
i += len(colorRed)
case strings.HasPrefix(text[i:], terminalColors.Red):
i += len(terminalColors.Red)
attributes = FOREGROUND_RED
case strings.HasPrefix(text[i:], colorGreen):
i += len(colorGreen)
case strings.HasPrefix(text[i:], terminalColors.Green):
i += len(terminalColors.Green)
attributes = FOREGROUND_GREEN
case strings.HasPrefix(text[i:], colorBlue):
i += len(colorBlue)
case strings.HasPrefix(text[i:], terminalColors.Blue):
i += len(terminalColors.Blue)
attributes = FOREGROUND_BLUE
case strings.HasPrefix(text[i:], colorCyan):
i += len(colorCyan)
case strings.HasPrefix(text[i:], terminalColors.Cyan):
i += len(terminalColors.Cyan)
attributes = FOREGROUND_GREEN | FOREGROUND_BLUE
case strings.HasPrefix(text[i:], colorMagenta):
i += len(colorMagenta)
case strings.HasPrefix(text[i:], terminalColors.Magenta):
i += len(terminalColors.Magenta)
attributes = FOREGROUND_RED | FOREGROUND_BLUE
case strings.HasPrefix(text[i:], colorYellow):
i += len(colorYellow)
case strings.HasPrefix(text[i:], terminalColors.Yellow):
i += len(terminalColors.Yellow)
attributes = FOREGROUND_RED | FOREGROUND_GREEN
case strings.HasPrefix(text[i:], colorResetDim):
i += len(colorResetDim)
case strings.HasPrefix(text[i:], terminalColors.Dim):
i += len(terminalColors.Dim)
attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
case strings.HasPrefix(text[i:], colorBold):
i += len(colorBold)
attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY
case strings.HasPrefix(text[i:], colorResetBold):
i += len(colorResetBold)
case strings.HasPrefix(text[i:], terminalColors.Bold):
i += len(terminalColors.Bold)
attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY
// Apparently underlines only work with the CJK locale on Windows :(
case strings.HasPrefix(text[i:], colorResetUnderline):
i += len(colorResetUnderline)
case strings.HasPrefix(text[i:], terminalColors.Underline):
i += len(terminalColors.Underline)
attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
default:

View File

@@ -1041,7 +1041,7 @@ func (w *watcher) start(logLevel LogLevel, color StderrColor, mode WatchMode) {
if logLevel == LogLevelInfo {
logger.PrintTextWithColor(os.Stderr, useColor, func(colors logger.Colors) string {
return fmt.Sprintf("%s[watch] build finished, watching for changes...%s\n", colors.Dim, colors.Default)
return fmt.Sprintf("%s[watch] build finished, watching for changes...%s\n", colors.Dim, colors.Reset)
})
}
@@ -1054,7 +1054,7 @@ func (w *watcher) start(logLevel LogLevel, color StderrColor, mode WatchMode) {
if logLevel == LogLevelInfo {
logger.PrintTextWithColor(os.Stderr, useColor, func(colors logger.Colors) string {
prettyPath := w.resolver.PrettyPath(logger.Path{Text: absPath, Namespace: "file"})
return fmt.Sprintf("%s[watch] build started (change: %q)%s\n", colors.Dim, prettyPath, colors.Default)
return fmt.Sprintf("%s[watch] build started (change: %q)%s\n", colors.Dim, prettyPath, colors.Reset)
})
}
@@ -1063,7 +1063,7 @@ func (w *watcher) start(logLevel LogLevel, color StderrColor, mode WatchMode) {
if logLevel == LogLevelInfo {
logger.PrintTextWithColor(os.Stderr, useColor, func(colors logger.Colors) string {
return fmt.Sprintf("%s[watch] build finished%s\n", colors.Dim, colors.Default)
return fmt.Sprintf("%s[watch] build finished%s\n", colors.Dim, colors.Reset)
})
}
}

View File

@@ -782,7 +782,7 @@ func serveImpl(osArgs []string) error {
}
return fmt.Sprintf("%s%s - %q %s%d%s [%dms]%s\n",
colors.Dim, args.RemoteAddress, args.Method+" "+args.Path,
statusColor, args.Status, colors.Dim, args.TimeInMS, colors.Default)
statusColor, args.Status, colors.Dim, args.TimeInMS, colors.Reset)
})
}
@@ -795,7 +795,7 @@ func serveImpl(osArgs []string) error {
logger.PrintText(os.Stderr, logger.LevelInfo, filteredArgs, func(colors logger.Colors) string {
var hosts []string
sb := strings.Builder{}
sb.WriteString(colors.Default)
sb.WriteString(colors.Reset)
// If this is "0.0.0.0" or "::", list all relevant IP addresses
if ip := net.ParseIP(result.Host); ip != nil && ip.IsUnspecified() {
@@ -831,7 +831,7 @@ func serveImpl(osArgs []string) error {
for i, kind := range kinds {
sb.WriteString(fmt.Sprintf("\n > %s:%s %shttp://%s/%s",
kind, strings.Repeat(" ", maxLen-len(kind)), colors.Underline,
net.JoinHostPort(hosts[i], fmt.Sprintf("%d", result.Port)), colors.Default))
net.JoinHostPort(hosts[i], fmt.Sprintf("%d", result.Port)), colors.Reset))
}
sb.WriteString("\n\n")