Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,27 @@ import (
"flag"
"go/ast"
"go/types"
"os"
"reflect"
"strings"

"go.uber.org/nilaway/util/asthelper"
"golang.org/x/tools/go/analysis"
)

// defaultPrettyPrint returns the default for the PrettyPrint config option.
// It respects the NO_COLOR and TERM=dumb conventions when no -pretty-print flag is provided.
// https://no-color.org/ and POSIX terminal-capability conventions.
func defaultPrettyPrint() bool {
if os.Getenv("NO_COLOR") != "" {
return false
}
if os.Getenv("TERM") == "dumb" {
return false
}
return true
}

// Config is the struct that stores the user-configurable options for NilAway.
type Config struct {
// PrettyPrint indicates whether the error messages should be pretty printed.
Expand Down Expand Up @@ -148,7 +162,7 @@ func newFlagSet() flag.FlagSet {

// We do not keep the returned pointer to the flags because we will not use them directly here.
// Instead, we will use the flags through the analyzer's Flags field later.
_ = fs.Bool(PrettyPrintFlag, true, "Pretty print the error messages")
_ = fs.Bool(PrettyPrintFlag, defaultPrettyPrint(), "Pretty print the error messages")
_ = fs.Bool(GroupErrorMessagesFlag, true, "Group similar error messages")
_ = fs.String(IncludePkgsFlag, "", "Comma-separated list of packages to analyze")
_ = fs.String(ExcludePkgsFlag, "", "Comma-separated list of packages to exclude from analysis")
Expand All @@ -164,7 +178,7 @@ func newFlagSet() flag.FlagSet {
func run(pass *analysis.Pass) (any, error) {
// Set up default values for the config.
conf := &Config{
PrettyPrint: true,
PrettyPrint: defaultPrettyPrint(),
GroupErrorMessages: true,
// If the user does not provide an include list, we give an empty package prefix to catch
// all packages.
Expand Down
2 changes: 1 addition & 1 deletion nilaway.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var codeReferencePattern = regexp.MustCompile("\\`(.*?)\\`")
var pathPattern = regexp.MustCompile(`"(.*?)"`)
var nilabilityPattern = regexp.MustCompile(`([\(|^\t](?i)(found\s|must\sbe\s)(nilable|nonnil)[\)]?)`)

// PrettyPrintErrorMessage is used in error reporting to post process and pretty print the output with colors
// PrettyPrintErrorMessage is used in error reporting to post process and pretty print the output with colors.
func PrettyPrintErrorMessage(msg string) string {
// TODO: below string parsing should not be required after is implemented
errorStr := fmt.Sprintf("\x1b[%dm%s\x1b[0m", 31, "error: ") // red
Expand Down
Loading