Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add separate sets of hard- and soft-enforced linters #60

Merged
merged 2 commits into from Jan 31, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/lint-soft.yml
@@ -0,0 +1,23 @@
name: lint-soft
on:
push:
pull_request:

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
pull-requests: read

jobs:
golangci:
name: lint-soft
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
# Optional: golangci-lint command line arguments.
args: --config .golangci-soft.yml --issues-exit-code=0
# Optional: show only new issues if it's a pull request. The default value is `false`.
only-new-issues: true
11 changes: 6 additions & 5 deletions .github/workflows/lint.yml
Expand Up @@ -3,6 +3,11 @@ on:
push:
pull_request:

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
pull-requests: read

jobs:
golangci:
name: lint
Expand All @@ -12,11 +17,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.31
# Optional: golangci-lint command line arguments.
args: --issues-exit-code=0
# Optional: working directory, useful for monorepos
# working-directory: somedir
#args:
# Optional: show only new issues if it's a pull request. The default value is `false`.
only-new-issues: true
47 changes: 47 additions & 0 deletions .golangci-soft.yml
@@ -0,0 +1,47 @@
run:
tests: false

issues:
include:
- EXC0001
- EXC0005
- EXC0011
- EXC0012
- EXC0013

max-issues-per-linter: 0
max-same-issues: 0

linters:
enable:
# - dupl
- exhaustive
# - exhaustivestruct
- goconst
- godot
- godox
- gomnd
- gomoddirectives
- goprintffuncname
- ifshort
# - lll
- misspell
- nakedret
- nestif
- noctx
- nolintlint
- prealloc
- wrapcheck

# disable default linters, they are already enabled in .golangci.yml
disable:
- deadcode
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- structcheck
- typecheck
- unused
- varcheck
18 changes: 11 additions & 7 deletions .golangci.yml
Expand Up @@ -2,24 +2,28 @@ run:
tests: false

issues:
include:
- EXC0001
- EXC0005
- EXC0011
- EXC0012
- EXC0013

max-issues-per-linter: 0
max-same-issues: 0

linters:
enable:
- bodyclose
- dupl
- exportloopref
- goconst
- godot
- godox
- goimports
- goprintffuncname
- gosec
- misspell
- prealloc
- nilerr
- predeclared
- revive
- rowserrcheck
- sqlclosecheck
- tparallel
- unconvert
- unparam
- whitespace
1 change: 1 addition & 0 deletions ansicolors.go
@@ -1,5 +1,6 @@
package termenv

// ANSI color codes
const (
ANSIBlack ANSIColor = iota
ANSIRed
Expand Down
25 changes: 20 additions & 5 deletions color.go
Expand Up @@ -12,41 +12,48 @@ import (
)

var (
// ErrInvalidColor gets returned when a color is invalid.
ErrInvalidColor = errors.New("invalid color")
)

// Foreground and Background sequence codes
const (
Foreground = "38"
Background = "48"
)

// Color is an interface implemented by all colors that can be converted to an
// ANSI sequence.
type Color interface {
// Sequence returns the ANSI Sequence for the color.
Sequence(bg bool) string
}

// NoColor is a nop for terminals that don't support colors.
type NoColor struct{}

func (nc NoColor) String() string {
func (c NoColor) String() string {
return ""
}

// ANSIColor is a color (0-15) as defined by the ANSI Standard.
type ANSIColor int

func (a ANSIColor) String() string {
return ansiHex[a]
func (c ANSIColor) String() string {
return ansiHex[c]
}

// ANSI256Color is a color (16-255) as defined by the ANSI Standard.
type ANSI256Color int

func (a ANSI256Color) String() string {
return ansiHex[a]
func (c ANSI256Color) String() string {
return ansiHex[c]
}

// RGBColor is a hex-encoded color, e.g. "#abcdef".
type RGBColor string

// ConvertToRGB converts a Color to a colorful.Color.
func ConvertToRGB(c Color) colorful.Color {
var hex string
switch v := c.(type) {
Expand All @@ -62,6 +69,7 @@ func ConvertToRGB(c Color) colorful.Color {
return ch
}

// Convert transforms a given Color to a Color supported within the Profile.
func (p Profile) Convert(c Color) Color {
if p == Ascii {
return NoColor{}
Expand Down Expand Up @@ -95,6 +103,8 @@ func (p Profile) Convert(c Color) Color {
return c
}

// Color creates a Color from a string. Valid inputs are hex colors, as well as
// ANSI color codes (0-15, 16-255).
func (p Profile) Color(s string) Color {
if len(s) == 0 {
return nil
Expand All @@ -119,15 +129,18 @@ func (p Profile) Color(s string) Color {
return p.Convert(c)
}

// FromColor creates a Color from a color.Color.
func (p Profile) FromColor(c color.Color) Color {
col, _ := colorful.MakeColor(c)
return p.Color(col.Hex())
}

// Sequence returns the ANSI Sequence for the color.
func (c NoColor) Sequence(bg bool) string {
return ""
}

// Sequence returns the ANSI Sequence for the color.
func (c ANSIColor) Sequence(bg bool) string {
col := int(c)
bgMod := func(c int) int {
Expand All @@ -143,6 +156,7 @@ func (c ANSIColor) Sequence(bg bool) string {
return fmt.Sprintf("%d", bgMod(col-8)+90)
}

// Sequence returns the ANSI Sequence for the color.
func (c ANSI256Color) Sequence(bg bool) string {
prefix := Foreground
if bg {
Expand All @@ -151,6 +165,7 @@ func (c ANSI256Color) Sequence(bg bool) string {
return fmt.Sprintf("%s;5;%d", prefix, c)
}

// Sequence returns the ANSI Sequence for the color.
func (c RGBColor) Sequence(bg bool) string {
f, err := colorful.Hex(string(c))
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions screen.go
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
)

// Sequence definitions.
const (
// Cursor positioning.
CursorUpSeq = "%dA"
Expand Down
1 change: 1 addition & 0 deletions style.go
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/mattn/go-runewidth"
)

// Sequence definitions.
const (
ResetSeq = "0"
BoldSeq = "1"
Expand Down
10 changes: 9 additions & 1 deletion termenv.go
Expand Up @@ -8,18 +8,26 @@ import (
)

var (
// ErrStatusReport gets returned when the terminal can't be queried.
ErrStatusReport = errors.New("unable to retrieve status report")
)

// Profile is a color profile: Ascii, ANSI, ANSI256, or TrueColor.
type Profile int

const (
// Control Sequence Introducer
CSI = "\x1b["
// Operating System Command
OSC = "\x1b]"

Ascii = Profile(iota)
// Ascii, uncolored profile.
Ascii = Profile(iota) //nolint:revive
// ANSI, 4-bit color profile
ANSI
// ANSI256, 8-bit color profile
ANSI256
// TrueColor, 24-bit color profile
TrueColor
)

Expand Down