1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-05-15 08:42:47 -04:00

doctor: refactor to use DoctorStatus struct and 'enum'

This commit is contained in:
LuckShiba
2026-01-03 20:07:27 -03:00
parent 13c352fd58
commit 6679a5f6f9
+92 -34
View File
@@ -16,16 +16,68 @@ import (
"github.com/AvengeMedia/DankMaterialShell/core/internal/tui" "github.com/AvengeMedia/DankMaterialShell/core/internal/tui"
"github.com/AvengeMedia/DankMaterialShell/core/internal/utils" "github.com/AvengeMedia/DankMaterialShell/core/internal/utils"
"github.com/AvengeMedia/DankMaterialShell/core/internal/version" "github.com/AvengeMedia/DankMaterialShell/core/internal/version"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type status string
const ( const (
statusOK = "ok" statusOK status = "ok"
statusWarn = "warn" statusWarn status = "warn"
statusError = "error" statusError status = "error"
statusInfo = "info" statusInfo status = "info"
) )
func (s status) IconStyle(styles tui.Styles) (string, lipgloss.Style) {
switch s {
case statusOK:
return "●", styles.Success
case statusWarn:
return "●", styles.Warning
case statusError:
return "●", styles.Error
default:
return "○", styles.Subtle
}
}
type DoctorStatus struct {
Errors []checkResult
Warnings []checkResult
OK []checkResult
Info []checkResult
}
func (ds *DoctorStatus) Add(r checkResult) {
switch r.status {
case statusError:
ds.Errors = append(ds.Errors, r)
case statusWarn:
ds.Warnings = append(ds.Warnings, r)
case statusOK:
ds.OK = append(ds.OK, r)
case statusInfo:
ds.Info = append(ds.Info, r)
}
}
func (ds *DoctorStatus) HasIssues() bool {
return len(ds.Errors) > 0 || len(ds.Warnings) > 0
}
func (ds *DoctorStatus) ErrorCount() int {
return len(ds.Errors)
}
func (ds *DoctorStatus) WarningCount() int {
return len(ds.Warnings)
}
func (ds *DoctorStatus) OKCount() int {
return len(ds.OK)
}
var ( var (
quickshellVersionRegex = regexp.MustCompile(`quickshell (\d+\.\d+\.\d+)`) quickshellVersionRegex = regexp.MustCompile(`quickshell (\d+\.\d+\.\d+)`)
hyprlandVersionRegex = regexp.MustCompile(`v?(\d+\.\d+\.\d+)`) hyprlandVersionRegex = regexp.MustCompile(`v?(\d+\.\d+\.\d+)`)
@@ -62,9 +114,29 @@ const (
catEnvironment catEnvironment
) )
var categoryNames = []string{ func (c category) String() string {
"System", "Versions", "Installation", "Compositor", switch c {
"Quickshell Features", "Optional Features", "Config Files", "Services", "Environment", case catSystem:
return "System"
case catVersions:
return "Versions"
case catInstallation:
return "Installation"
case catCompositor:
return "Compositor"
case catQuickshellFeatures:
return "Quickshell Features"
case catOptionalFeatures:
return "Optional Features"
case catConfigFiles:
return "Config Files"
case catServices:
return "Services"
case catEnvironment:
return "Environment"
default:
return "Unknown"
}
} }
const ( const (
@@ -74,7 +146,7 @@ const (
type checkResult struct { type checkResult struct {
category category category category
name string name string
status string status status
message string message string
details string details string
} }
@@ -256,7 +328,7 @@ func getDMSShellVersion() (version, path string) {
return "", "" return "", ""
} }
func getQuickshellVersionInfo(missingFeatures bool) (string, string, string) { func getQuickshellVersionInfo(missingFeatures bool) (string, status, string) {
if !utils.CommandExists("qs") { if !utils.CommandExists("qs") {
return "Not installed", statusError, "" return "Not installed", statusError, ""
} }
@@ -685,7 +757,7 @@ func printResults(results []checkResult) {
if currentCategory != -1 { if currentCategory != -1 {
fmt.Println() fmt.Println()
} }
fmt.Printf(" %s\n", styles.Bold.Render(categoryNames[r.category])) fmt.Printf(" %s\n", styles.Bold.Render(r.category.String()))
currentCategory = r.category currentCategory = r.category
} }
printResultLine(r, styles) printResultLine(r, styles)
@@ -693,15 +765,7 @@ func printResults(results []checkResult) {
} }
func printResultLine(r checkResult, styles tui.Styles) { func printResultLine(r checkResult, styles tui.Styles) {
icon, style := "○", styles.Subtle icon, style := r.status.IconStyle(styles)
switch r.status {
case statusOK:
icon, style = "●", styles.Success
case statusWarn:
icon, style = "●", styles.Warning
case statusError:
icon, style = "●", styles.Error
}
name := r.name name := r.name
nameLen := len(name) nameLen := len(name)
@@ -723,32 +787,26 @@ func printSummary(results []checkResult, qsMissingFeatures bool) {
theme := tui.TerminalTheme() theme := tui.TerminalTheme()
styles := tui.NewStyles(theme) styles := tui.NewStyles(theme)
errors, warnings, ok := 0, 0, 0 var ds DoctorStatus
for _, r := range results { for _, r := range results {
switch r.status { ds.Add(r)
case statusError:
errors++
case statusWarn:
warnings++
case statusOK:
ok++
}
} }
fmt.Println() fmt.Println()
fmt.Printf(" %s\n", styles.Subtle.Render("──────────────────────────────────────")) fmt.Printf(" %s\n", styles.Subtle.Render("──────────────────────────────────────"))
if errors == 0 && warnings == 0 { if !ds.HasIssues() {
fmt.Printf(" %s\n", styles.Success.Render("✓ All checks passed!")) fmt.Printf(" %s\n", styles.Success.Render("✓ All checks passed!"))
} else { } else {
var parts []string var parts []string
if errors > 0 {
parts = append(parts, styles.Error.Render(fmt.Sprintf("%d error(s)", errors))) if ds.ErrorCount() > 0 {
parts = append(parts, styles.Error.Render(fmt.Sprintf("%d error(s)", ds.ErrorCount())))
} }
if warnings > 0 { if ds.WarningCount() > 0 {
parts = append(parts, styles.Warning.Render(fmt.Sprintf("%d warning(s)", warnings))) parts = append(parts, styles.Warning.Render(fmt.Sprintf("%d warning(s)", ds.WarningCount())))
} }
parts = append(parts, styles.Success.Render(fmt.Sprintf("%d ok", ok))) parts = append(parts, styles.Success.Render(fmt.Sprintf("%d ok", ds.OKCount())))
fmt.Printf(" %s\n", strings.Join(parts, ", ")) fmt.Printf(" %s\n", strings.Join(parts, ", "))
if qsMissingFeatures { if qsMissingFeatures {