status: improve the status system

parent e86cb735
package config
import "github.com/fatih/color"
type ItemStatus struct {
Symbol string
Color func(format string, a ...interface{}) string
Label string
}
var unknown ItemStatus = ItemStatus{
Symbol: "?",
Color: color.YellowString,
Label: "unknown",
}
func (s ItemStatus) IsEqual(other ItemStatus) bool {
return s.Label == other.Label
}
var ModuleStatus = struct {
Unknown ItemStatus
Enabled ItemStatus
Disabled ItemStatus
Missing ItemStatus
Unused ItemStatus
}{
Unknown: unknown,
Enabled: ItemStatus{
Symbol: "●",
Color: color.GreenString,
Label: "enabled",
},
Disabled: ItemStatus{
Symbol: "○",
Color: color.RedString,
Label: "disabled",
},
Missing: ItemStatus{
Symbol: "!",
Color: color.YellowString,
Label: "missing",
},
Unused: ItemStatus{
Symbol: "-",
Color: color.HiBlackString,
Label: "unused",
},
}
var PluginStatus = struct {
Unknown ItemStatus
Loaded ItemStatus
Unloaded ItemStatus
}{
Unknown: unknown,
Loaded: ItemStatus{
Symbol: "●",
Color: color.GreenString,
Label: "loaded",
},
Unloaded: ItemStatus{
Symbol: "○",
Color: color.RedString,
Label: "unloaded",
},
}
var ProfileStatus = struct {
Unknown ItemStatus
Available ItemStatus
Unavailable ItemStatus
}{
Unknown: unknown,
Available: ItemStatus{
Symbol: "●",
Color: color.GreenString,
Label: "available",
},
Unavailable: ItemStatus{
Symbol: "○",
Color: color.RedString,
Label: "unavailable",
},
}
package hyprland package hyprland
import ( import (
"ximperconf/config"
"ximperconf/ui" "ximperconf/ui"
"ximperconf/utils" "ximperconf/utils"
...@@ -35,22 +36,6 @@ func HyprlandCheckCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -35,22 +36,6 @@ func HyprlandCheckCommand(ctx context.Context, cmd *cli.Command) error {
return nil return nil
} }
func hyprlandModuleStatusStruct(manager *HyprlandManager, module string, user bool) ui.ItemStatus {
info := manager.GetModuleInfo(module, user)
switch info.Status {
case "enabled":
return ui.StatusEnabled
case "disabled":
return ui.StatusDisabled
case "missing":
return ui.StatusMissing
case "unused":
return ui.StatusUnused
}
return ui.StatusUnknown
}
func HyprlandModuleStatusCommand(ctx context.Context, cmd *cli.Command) error { func HyprlandModuleStatusCommand(ctx context.Context, cmd *cli.Command) error {
manager, err := GetHyprlandManager(ctx) manager, err := GetHyprlandManager(ctx)
...@@ -63,7 +48,7 @@ func HyprlandModuleStatusCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -63,7 +48,7 @@ func HyprlandModuleStatusCommand(ctx context.Context, cmd *cli.Command) error {
color.Red("недопустимое название для модуля.") color.Red("недопустимое название для модуля.")
return nil return nil
} }
fmt.Println(info.Status) fmt.Println(info.Status.Label)
return nil return nil
} }
...@@ -153,10 +138,14 @@ func HyprlandToggleModuleCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -153,10 +138,14 @@ func HyprlandToggleModuleCommand(ctx context.Context, cmd *cli.Command) error {
var msg string var msg string
switch info.Status { switch info.Status.Label {
case "enabled": case config.ModuleStatus.Enabled.Label:
msg, err = manager.SetModule("disable", module, user, false)
case config.ModuleStatus.Disabled.Label, config.ModuleStatus.Unused.Label:
msg, err = manager.SetModule("enable", module, user, false)
case config.ModuleStatus.Missing.Label:
msg, err = manager.SetModule("disable", module, user, false) msg, err = manager.SetModule("disable", module, user, false)
case "disabled", "unused": default:
msg, err = manager.SetModule("enable", module, user, false) msg, err = manager.SetModule("enable", module, user, false)
} }
...@@ -185,7 +174,7 @@ func HyprlandInfoModulesCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -185,7 +174,7 @@ func HyprlandInfoModulesCommand(ctx context.Context, cmd *cli.Command) error {
items := make([]ui.TreeItem, 0, len(modules)) items := make([]ui.TreeItem, 0, len(modules))
for _, module := range modules { for _, module := range modules {
status := hyprlandModuleStatusStruct(manager, module, user) info := manager.GetModuleInfo(module, user)
errors, err := manager.CheckModule(module, user) errors, err := manager.CheckModule(module, user)
...@@ -199,17 +188,17 @@ func HyprlandInfoModulesCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -199,17 +188,17 @@ func HyprlandInfoModulesCommand(ctx context.Context, cmd *cli.Command) error {
} }
items = append(items, ui.TreeItem{ items = append(items, ui.TreeItem{
Name: module, Name: module,
Status: status, Status: info.Status,
Description: desc, Description: desc,
}) })
} }
ui.RenderTree(ui.RenderTreeOptions{ ui.RenderTree(ui.RenderTreeOptions{
Title: "Modules", Title: "Modules",
Items: items, Items: items,
Style: ui.DefaultTreeStyle, Style: ui.DefaultTreeStyle,
StatusMap: ui.ColorStatusMap, Color: true,
Sort: true, Sort: true,
}) })
return nil return nil
......
...@@ -28,7 +28,7 @@ type HyprlandManager struct { ...@@ -28,7 +28,7 @@ type HyprlandManager struct {
type HyprModule struct { type HyprModule struct {
Name string Name string
Status string Status config.ItemStatus
Path string Path string
ConfPath string ConfPath string
LineNumber int LineNumber int
...@@ -182,7 +182,7 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule { ...@@ -182,7 +182,7 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule {
if FileExists { if FileExists {
return HyprModule{ return HyprModule{
Name: module, Name: module,
Status: "unused", Status: config.ModuleStatus.Unused,
Path: sysFile, Path: sysFile,
ConfPath: ConfPath, ConfPath: ConfPath,
Available: Available, Available: Available,
...@@ -190,7 +190,7 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule { ...@@ -190,7 +190,7 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule {
} }
return HyprModule{ return HyprModule{
Name: module, Name: module,
Status: "missing", Status: config.ModuleStatus.Missing,
Available: Available, Available: Available,
} }
} }
...@@ -222,7 +222,7 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule { ...@@ -222,7 +222,7 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule {
if !FileExists { if !FileExists {
return HyprModule{ return HyprModule{
Name: module, Name: module,
Status: "missing", Status: config.ModuleStatus.Missing,
LineNumber: LineNumber, LineNumber: LineNumber,
Available: Available, Available: Available,
} }
...@@ -230,7 +230,7 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule { ...@@ -230,7 +230,7 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule {
// файл есть // файл есть
return HyprModule{ return HyprModule{
Name: module, Name: module,
Status: "enabled", Status: config.ModuleStatus.Enabled,
Path: sysFile, Path: sysFile,
ConfPath: ConfPath, ConfPath: ConfPath,
LineNumber: LineNumber, LineNumber: LineNumber,
...@@ -248,7 +248,7 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule { ...@@ -248,7 +248,7 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule {
// файл есть // файл есть
return HyprModule{ return HyprModule{
Name: module, Name: module,
Status: "disabled", Status: config.ModuleStatus.Disabled,
Path: Path, Path: Path,
ConfPath: ConfPath, ConfPath: ConfPath,
LineNumber: LineNumber, LineNumber: LineNumber,
...@@ -260,7 +260,7 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule { ...@@ -260,7 +260,7 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule {
if FileExists { if FileExists {
return HyprModule{ return HyprModule{
Name: module, Name: module,
Status: "unused", Status: config.ModuleStatus.Unused,
Path: sysFile, Path: sysFile,
ConfPath: ConfPath, ConfPath: ConfPath,
Available: Available, Available: Available,
...@@ -270,7 +270,7 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule { ...@@ -270,7 +270,7 @@ func (m *HyprlandManager) GetModuleInfo(module string, user bool) HyprModule {
// Файла нет и упоминаний нет // Файла нет и упоминаний нет
return HyprModule{ return HyprModule{
Name: module, Name: module,
Status: "unknown", Status: config.ModuleStatus.Unknown,
Available: Available, Available: Available,
} }
} }
...@@ -305,7 +305,7 @@ func (m *HyprlandManager) GetModulesList(user bool, filter string) []string { ...@@ -305,7 +305,7 @@ func (m *HyprlandManager) GetModulesList(user bool, filter string) []string {
continue continue
} }
if filter == "" || filter == "all" || info.Status == filter { if filter == "" || filter == "all" || info.Status.Label == filter {
res = append(res, module) res = append(res, module)
} }
} }
...@@ -319,7 +319,7 @@ func (m *HyprlandManager) SetModule(action, module string, user, onlyNew bool) ( ...@@ -319,7 +319,7 @@ func (m *HyprlandManager) SetModule(action, module string, user, onlyNew bool) (
return "", fmt.Errorf("недопустимое название для модуля") return "", fmt.Errorf("недопустимое название для модуля")
} }
if info.Status == "unknown" { if info.Status.IsEqual(config.ModuleStatus.Unknown) {
return "", fmt.Errorf("модуль '%s' не найден: %s", module, info.Path) return "", fmt.Errorf("модуль '%s' не найден: %s", module, info.Path)
} }
...@@ -327,17 +327,17 @@ func (m *HyprlandManager) SetModule(action, module string, user, onlyNew bool) ( ...@@ -327,17 +327,17 @@ func (m *HyprlandManager) SetModule(action, module string, user, onlyNew bool) (
case "enable": case "enable":
// нет файла // нет файла
if info.Path == "" || info.Status == "missing" { if info.Path == "" || info.Status.IsEqual(config.ModuleStatus.Unknown) {
return "", fmt.Errorf("нельзя включить данный модуль") return "", fmt.Errorf("нельзя включить данный модуль")
} }
// уже включён // уже включён
if info.Status == "enabled" { if info.Status.IsEqual(config.ModuleStatus.Enabled) {
return "", fmt.Errorf("модуль '%s' уже включён", module) return "", fmt.Errorf("модуль '%s' уже включён", module)
} }
// был закомментирован // был закомментирован
if info.Status == "disabled" { if info.Status.IsEqual(config.ModuleStatus.Disabled) {
if onlyNew { if onlyNew {
return "", fmt.Errorf("модуль '%s' уже присутствует в конфиге (закомментирован) — пропущено", module) return "", fmt.Errorf("модуль '%s' уже присутствует в конфиге (закомментирован) — пропущено", module)
} }
...@@ -347,7 +347,7 @@ func (m *HyprlandManager) SetModule(action, module string, user, onlyNew bool) ( ...@@ -347,7 +347,7 @@ func (m *HyprlandManager) SetModule(action, module string, user, onlyNew bool) (
} }
// не использовался // не использовался
if info.Status == "unused" { if info.Status.IsEqual(config.ModuleStatus.Unused) {
line := "source = " + info.ConfPath line := "source = " + info.ConfPath
m.Lines = append(m.Lines, line) m.Lines = append(m.Lines, line)
m.Changed = true m.Changed = true
...@@ -359,12 +359,14 @@ func (m *HyprlandManager) SetModule(action, module string, user, onlyNew bool) ( ...@@ -359,12 +359,14 @@ func (m *HyprlandManager) SetModule(action, module string, user, onlyNew bool) (
case "disable": case "disable":
// Уже выключен // Уже выключен
if info.Status == "disabled" || info.Status == "unused" { if info.Status.IsEqual(config.ModuleStatus.Disabled) ||
info.Status.IsEqual(config.ModuleStatus.Unused) {
return "", fmt.Errorf("модуль '%s' уже отключён", module) return "", fmt.Errorf("модуль '%s' уже отключён", module)
} }
// Включён // Включён
if info.Status == "enabled" || info.Status == "missing" { if info.Status.IsEqual(config.ModuleStatus.Enabled) ||
info.Status.IsEqual(config.ModuleStatus.Missing) {
m.Lines[info.LineNumber] = "#" + m.Lines[info.LineNumber] m.Lines[info.LineNumber] = "#" + m.Lines[info.LineNumber]
m.Changed = true m.Changed = true
return fmt.Sprintf("Модуль '%s' отключён", module), nil return fmt.Sprintf("Модуль '%s' отключён", module), nil
...@@ -397,14 +399,14 @@ func (m *HyprlandManager) GetLoadedPlugins() []HyprPlugin { ...@@ -397,14 +399,14 @@ func (m *HyprlandManager) GetLoadedPlugins() []HyprPlugin {
return plugins return plugins
} }
func (m *HyprlandManager) GetPluginStatus(name string) string { func (m *HyprlandManager) GetPluginStatus(name string) config.ItemStatus {
for _, p := range m.LoadedPlugins { for _, p := range m.LoadedPlugins {
if p.Name == name { if p.Name == name {
return "loaded" return config.PluginStatus.Loaded
} }
} }
return "unloaded" return config.PluginStatus.Unloaded
} }
func (m *HyprlandManager) GetPluginFile(name string) (string, error) { func (m *HyprlandManager) GetPluginFile(name string) (string, error) {
...@@ -427,7 +429,7 @@ func (m *HyprlandManager) GetPluginsList(filter string) []string { ...@@ -427,7 +429,7 @@ func (m *HyprlandManager) GetPluginsList(filter string) []string {
for _, plugin := range plugins { for _, plugin := range plugins {
status := m.GetPluginStatus(plugin) status := m.GetPluginStatus(plugin)
if filter == "" || filter == "all" || filter == status { if filter == "" || filter == "all" || status.Label == filter {
list = append(list, plugin) list = append(list, plugin)
} }
} }
...@@ -449,7 +451,7 @@ func (m *HyprlandManager) SetPlugin(action string, name string) (string, error) ...@@ -449,7 +451,7 @@ func (m *HyprlandManager) SetPlugin(action string, name string) (string, error)
switch action { switch action {
case "load": case "load":
if status == "loaded" { if status.IsEqual(config.PluginStatus.Loaded) {
return "", fmt.Errorf("плагин уже загружен") return "", fmt.Errorf("плагин уже загружен")
} }
...@@ -461,7 +463,7 @@ func (m *HyprlandManager) SetPlugin(action string, name string) (string, error) ...@@ -461,7 +463,7 @@ func (m *HyprlandManager) SetPlugin(action string, name string) (string, error)
return fmt.Sprintf("Плагин '%s' загружен", name), nil return fmt.Sprintf("Плагин '%s' загружен", name), nil
case "unload": case "unload":
if status == "unloaded" { if status.IsEqual(config.PluginStatus.Unloaded) {
return "", fmt.Errorf("плагин не загружен") return "", fmt.Errorf("плагин не загружен")
} }
......
package hyprland package hyprland
import ( import (
"ximperconf/config"
"ximperconf/ui" "ximperconf/ui"
"context" "context"
...@@ -10,16 +11,6 @@ import ( ...@@ -10,16 +11,6 @@ import (
"github.com/urfave/cli/v3" "github.com/urfave/cli/v3"
) )
func hyprlandPluginStatusStruct(status string) ui.ItemStatus {
switch status {
case "loaded":
return ui.StatusLoaded
case "unloaded":
return ui.StatusUnloaded
}
return ui.StatusUnknown
}
func HyprlandPluginListCommand(ctx context.Context, cmd *cli.Command) error { func HyprlandPluginListCommand(ctx context.Context, cmd *cli.Command) error {
manager, err := GetHyprlandManager(ctx) manager, err := GetHyprlandManager(ctx)
if err != nil { if err != nil {
...@@ -35,7 +26,7 @@ func HyprlandPluginStatusCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -35,7 +26,7 @@ func HyprlandPluginStatusCommand(ctx context.Context, cmd *cli.Command) error {
if err != nil { if err != nil {
return err return err
} }
fmt.Println(manager.GetPluginStatus(cmd.Args().Get(0))) fmt.Println(manager.GetPluginStatus(cmd.Args().Get(0)).Label)
return nil return nil
} }
...@@ -52,10 +43,9 @@ func HyprlandPluginInfoCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -52,10 +43,9 @@ func HyprlandPluginInfoCommand(ctx context.Context, cmd *cli.Command) error {
items := make([]ui.TreeItem, 0, len(plugins)) items := make([]ui.TreeItem, 0, len(plugins))
for _, plugin := range plugins { for _, plugin := range plugins {
_status := manager.GetPluginStatus(plugin) status := manager.GetPluginStatus(plugin)
status := hyprlandPluginStatusStruct(_status)
desc := "" desc := ""
if _status == "loaded" { if status.IsEqual(config.PluginStatus.Loaded) {
for _, plug := range manager.LoadedPlugins { for _, plug := range manager.LoadedPlugins {
if plug.Name == plugin { if plug.Name == plugin {
desc = plug.Description desc = plug.Description
...@@ -70,11 +60,11 @@ func HyprlandPluginInfoCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -70,11 +60,11 @@ func HyprlandPluginInfoCommand(ctx context.Context, cmd *cli.Command) error {
} }
ui.RenderTree(ui.RenderTreeOptions{ ui.RenderTree(ui.RenderTreeOptions{
Title: "Plugins", Title: "Plugins",
Items: items, Items: items,
Style: ui.DefaultTreeStyle, Style: ui.DefaultTreeStyle,
StatusMap: ui.ColorStatusMap, Color: true,
Sort: true, Sort: true,
}) })
return nil return nil
} }
...@@ -120,10 +110,10 @@ func HyprlandPluginToggleCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -120,10 +110,10 @@ func HyprlandPluginToggleCommand(ctx context.Context, cmd *cli.Command) error {
path := cmd.Args().Get(0) path := cmd.Args().Get(0)
status := manager.GetPluginStatus(path) status := manager.GetPluginStatus(path)
switch status { switch status.Label {
case "loaded": case config.PluginStatus.Loaded.Label:
msg, err = manager.SetPlugin("unload", path) msg, err = manager.SetPlugin("unload", path)
case "unloaded": case config.PluginStatus.Unloaded.Label:
msg, err = manager.SetPlugin("load", path) msg, err = manager.SetPlugin("load", path)
} }
......
...@@ -49,17 +49,15 @@ func HyprlandVarInfoCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -49,17 +49,15 @@ func HyprlandVarInfoCommand(ctx context.Context, cmd *cli.Command) error {
} }
items = append(items, ui.TreeItem{ items = append(items, ui.TreeItem{
Name: v.Name, Name: v.Name,
Status: ui.StatusNo,
Description: desc, Description: desc,
}) })
} }
ui.RenderTree(ui.RenderTreeOptions{ ui.RenderTree(ui.RenderTreeOptions{
Title: "Vars", Title: "Vars",
Items: items, Items: items,
Style: ui.DefaultTreeStyle, Style: ui.DefaultTreeStyle,
StatusMap: ui.ColorStatusMap, Sort: true,
Sort: true,
}) })
return nil return nil
......
...@@ -339,11 +339,11 @@ func ShowProfilesInfo() { ...@@ -339,11 +339,11 @@ func ShowProfilesInfo() {
} }
ui.RenderTree(ui.RenderTreeOptions{ ui.RenderTree(ui.RenderTreeOptions{
Title: "Profiles", Title: "Profiles",
Items: items, Items: items,
Style: ui.DefaultTreeStyle, Style: ui.DefaultTreeStyle,
StatusMap: ui.ColorStatusMap, Color: true,
Sort: true, Sort: true,
}) })
} }
......
...@@ -27,11 +27,11 @@ func profileAvailable(p config.PresetProfile) bool { ...@@ -27,11 +27,11 @@ func profileAvailable(p config.PresetProfile) bool {
return allowed return allowed
} }
func profileStatus(p config.PresetProfile) ui.ItemStatus { func profileStatus(p config.PresetProfile) config.ItemStatus {
if profileAvailable(p) { if profileAvailable(p) {
return ui.StatusAvailable return config.ProfileStatus.Available
} }
return ui.StatusUnavailable return config.ProfileStatus.Unavailable
} }
func AppendOpResult(res *config.PresetResult, r opResult) { func AppendOpResult(res *config.PresetResult, r opResult) {
......
...@@ -3,100 +3,12 @@ package ui ...@@ -3,100 +3,12 @@ package ui
import ( import (
"fmt" "fmt"
"sort" "sort"
"ximperconf/config"
"github.com/fatih/color"
) )
type ItemStatus int
const (
StatusUnknown ItemStatus = iota
StatusNo
StatusEnabled
StatusLoaded
StatusAvailable
StatusDisabled
StatusUnavailable
StatusUnloaded
StatusMissing
StatusUnused
)
type StatusInfo struct {
Symbol string
Color func(format string, a ...interface{}) string
Label string
}
var ColorStatusMap = map[ItemStatus]StatusInfo{
StatusUnknown: {
Symbol: "?",
Color: color.YellowString,
Label: "unknown",
},
StatusNo: {
Symbol: "",
Color: color.YellowString,
Label: "no",
},
StatusEnabled: {
Symbol: "●",
Color: color.GreenString,
Label: "enabled",
},
StatusLoaded: {
Symbol: "●",
Color: color.GreenString,
Label: "loaded",
},
StatusAvailable: {
Symbol: "●",
Color: color.GreenString,
Label: "available",
},
StatusDisabled: {
Symbol: "○",
Color: color.RedString,
Label: "disabled",
},
StatusUnloaded: {
Symbol: "○",
Color: color.RedString,
Label: "unloaded",
},
StatusUnavailable: {
Symbol: "○",
Color: color.RedString,
Label: "unavailable",
},
StatusMissing: {
Symbol: "!",
Color: color.YellowString,
Label: "missing",
},
StatusUnused: {
Symbol: "-",
Color: color.HiBlackString,
Label: "unused",
},
}
var PlainStatusMap = map[ItemStatus]StatusInfo{
StatusEnabled: {Symbol: "", Color: func(format string, a ...interface{}) string { return fmt.Sprintf(format, a...) }, Label: "enabled"},
StatusLoaded: {Symbol: "", Color: func(format string, a ...interface{}) string { return fmt.Sprintf(format, a...) }, Label: "loaded"},
StatusAvailable: {Symbol: "", Color: func(format string, a ...interface{}) string { return fmt.Sprintf(format, a...) }, Label: "available"},
StatusDisabled: {Symbol: "", Color: func(format string, a ...interface{}) string { return fmt.Sprintf(format, a...) }, Label: "disabled"},
StatusUnloaded: {Symbol: "", Color: func(format string, a ...interface{}) string { return fmt.Sprintf(format, a...) }, Label: "unloaded"},
StatusUnavailable: {Symbol: "", Color: func(format string, a ...interface{}) string { return fmt.Sprintf(format, a...) }, Label: "unavailable"},
StatusMissing: {Symbol: "", Color: func(format string, a ...interface{}) string { return fmt.Sprintf(format, a...) }, Label: "missing"},
StatusUnused: {Symbol: "", Color: func(format string, a ...interface{}) string { return fmt.Sprintf(format, a...) }, Label: "unused"},
StatusUnknown: {Symbol: "", Color: func(format string, a ...interface{}) string { return fmt.Sprintf(format, a...) }, Label: "unknown"},
StatusNo: {Symbol: "", Color: func(format string, a ...interface{}) string { return fmt.Sprintf(format, a...) }, Label: "no"},
}
type TreeItem struct { type TreeItem struct {
Name string Name string
Status ItemStatus Status config.ItemStatus
Description string Description string
} }
...@@ -113,11 +25,18 @@ var DefaultTreeStyle = TreeStyle{ ...@@ -113,11 +25,18 @@ var DefaultTreeStyle = TreeStyle{
} }
type RenderTreeOptions struct { type RenderTreeOptions struct {
Title string Title string
Items []TreeItem Items []TreeItem
Style TreeStyle Style TreeStyle
StatusMap map[ItemStatus]StatusInfo Color bool
Sort bool Sort bool
}
func colorize(item TreeItem, color bool) (string, string) {
if color && item.Status.Color != nil {
return item.Status.Color(item.Status.Symbol), item.Status.Color(item.Name)
}
return item.Status.Symbol, item.Name
} }
func RenderTree(opts RenderTreeOptions) { func RenderTree(opts RenderTreeOptions) {
...@@ -125,10 +44,6 @@ func RenderTree(opts RenderTreeOptions) { ...@@ -125,10 +44,6 @@ func RenderTree(opts RenderTreeOptions) {
opts.Style = DefaultTreeStyle opts.Style = DefaultTreeStyle
} }
if opts.StatusMap == nil {
opts.StatusMap = ColorStatusMap
}
if opts.Sort { if opts.Sort {
sort.Slice(opts.Items, func(i, j int) bool { sort.Slice(opts.Items, func(i, j int) bool {
return opts.Items[i].Name < opts.Items[j].Name return opts.Items[i].Name < opts.Items[j].Name
...@@ -145,17 +60,13 @@ func RenderTree(opts RenderTreeOptions) { ...@@ -145,17 +60,13 @@ func RenderTree(opts RenderTreeOptions) {
prefix = opts.Style.PrefixLast prefix = opts.Style.PrefixLast
} }
info, ok := opts.StatusMap[it.Status] symbol, name := colorize(it, opts.Color)
if !ok {
info = opts.StatusMap[StatusUnknown]
}
line := fmt.Sprintf( line := prefix + " "
"%s %s %s", if symbol != "" {
prefix, line += symbol + " "
info.Color(info.Symbol), }
info.Color(it.Name), line += name
)
if it.Description != "" { if it.Description != "" {
line += opts.Style.Separator + it.Description line += opts.Style.Separator + it.Description
......
package ui package ui
import ( import (
"fmt"
"github.com/fatih/color" "github.com/fatih/color"
) )
...@@ -16,15 +14,13 @@ func RenderTreeLines(title string, lines []string) { ...@@ -16,15 +14,13 @@ func RenderTreeLines(title string, lines []string) {
items := make([]TreeItem, len(lines)) items := make([]TreeItem, len(lines))
for i, line := range lines { for i, line := range lines {
items[i] = TreeItem{ items[i] = TreeItem{
Name: line, Name: line,
Status: StatusUnknown,
} }
} }
RenderTree(RenderTreeOptions{ RenderTree(RenderTreeOptions{
Items: items, Items: items,
Style: DefaultTreeStyle, Style: DefaultTreeStyle,
StatusMap: PlainStatusMap, Color: false,
}) })
fmt.Println()
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment