mirror of
https://github.com/jesseduffield/lazynpm.git
synced 2026-01-09 06:21:11 +08:00
better message around ahead/behind for semver
This commit is contained in:
parent
0a02233a1b
commit
6134d824df
@ -1,7 +1,7 @@
|
||||
package presentation
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"fmt"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/jesseduffield/lazynpm/pkg/commands"
|
||||
@ -29,7 +29,7 @@ func getDepDisplayStrings(d *commands.Dependency) []string {
|
||||
if ok {
|
||||
localVersionCol = utils.ColoredString(d.PackageConfig.Version, color.FgGreen)
|
||||
} else {
|
||||
localVersionCol = utils.ColoredString(d.PackageConfig.Version+" "+status, color.FgYellow)
|
||||
localVersionCol = utils.ColoredString(fmt.Sprintf("%s%s", d.PackageConfig.Version, statusMap()[status]), color.FgYellow)
|
||||
}
|
||||
} else {
|
||||
localVersionCol = utils.ColoredString("missing", color.FgRed)
|
||||
@ -38,26 +38,29 @@ func getDepDisplayStrings(d *commands.Dependency) []string {
|
||||
return []string{d.Name, utils.ColoredString(d.Version, color.FgMagenta), localVersionCol}
|
||||
}
|
||||
|
||||
func semverStatus(version, constraint string) (string, bool) {
|
||||
func statusMap() map[int]string {
|
||||
return map[int]string{
|
||||
semver.BAD_AHEAD: " (ahead)",
|
||||
semver.BAD_BEHIND: " (behind)",
|
||||
semver.BAD_EQUAL: " (equal)",
|
||||
semver.BAD: "",
|
||||
}
|
||||
}
|
||||
|
||||
func semverStatus(version, constraint string) (int, bool) {
|
||||
c, err := semver.NewConstraint(constraint)
|
||||
if err != nil {
|
||||
return "error parsing constraint", false
|
||||
// could have a formatted message here but too lazy
|
||||
return semver.BAD, false
|
||||
}
|
||||
|
||||
v, err := semver.NewVersion(version)
|
||||
if err != nil {
|
||||
return "error parsing version", false
|
||||
// could have a formatted message here but too lazy
|
||||
return semver.BAD, false
|
||||
}
|
||||
|
||||
ok, errors := c.Validate(v)
|
||||
if ok {
|
||||
return "", true
|
||||
}
|
||||
status := c.Status(v)
|
||||
|
||||
messages := make([]string, len(errors))
|
||||
for i, err := range errors {
|
||||
messages[i] = err.Error()
|
||||
}
|
||||
|
||||
return strings.Join(messages, ","), false
|
||||
return status, status == semver.GOOD
|
||||
}
|
||||
|
||||
31
vendor/github.com/jesseduffield/semver/constraints.go
generated
vendored
31
vendor/github.com/jesseduffield/semver/constraints.go
generated
vendored
@ -60,6 +60,37 @@ func (cs Constraints) Check(v *Version) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
const (
|
||||
BAD = iota
|
||||
BAD_AHEAD
|
||||
BAD_BEHIND
|
||||
BAD_EQUAL
|
||||
GOOD
|
||||
)
|
||||
|
||||
// Status returns the status of the version in relation to the constraint e.g. AHEAD/BEHIND
|
||||
// This method assumes only a single constraint given
|
||||
func (cs Constraints) Status(v *Version) int {
|
||||
good := cs.Check(v)
|
||||
|
||||
if good {
|
||||
return GOOD
|
||||
}
|
||||
|
||||
if len(cs.constraints) > 1 || len(cs.constraints[0]) > 1 {
|
||||
return BAD
|
||||
}
|
||||
|
||||
val := v.Compare(cs.constraints[0][0].con)
|
||||
if val < 0 {
|
||||
return BAD_BEHIND
|
||||
} else if val > 0 {
|
||||
return BAD_AHEAD
|
||||
} else {
|
||||
return BAD_EQUAL
|
||||
}
|
||||
}
|
||||
|
||||
// Validate checks if a version satisfies a constraint. If not a slice of
|
||||
// reasons for the failure are returned in addition to a bool.
|
||||
func (cs Constraints) Validate(v *Version) (bool, []error) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user