Skip to content

Commit

Permalink
feat: option to order sections
Browse files Browse the repository at this point in the history
  • Loading branch information
jon4hz committed Feb 25, 2024
1 parent eae4b21 commit c24f6e2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions config/config_types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

type Config struct {
Order []string `mapstructure:"order"`
Hostname *Hostname `mapstructure:"hostname"`
Uptime *Uptime `mapstructure:"uptime"`
SysInfo *SysInfo `mapstructure:"sysinfo"`
Expand Down
31 changes: 28 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func main() {
// maybe make this a config option.
lipgloss.SetColorProfile(termenv.TrueColor)

var wg sync.WaitGroup
var (
sectionResults = make(map[string]string)
wg sync.WaitGroup
)
for _, section := range message.Message {
if !section.Enabled(ctx) {
continue
Expand All @@ -44,17 +47,39 @@ func main() {
log.Println(err)
return
}

if msg := section.Print(ctx); msg != "" {
sectionResults[section.String()] = msg + "\n"
}

}(section)
}
wg.Wait()

var messages []string
printed := make(map[string]struct{})

// check if specific order is set
if len(ctx.Config.Order) > 0 {
for _, section := range ctx.Config.Order {
if result, ok := sectionResults[section]; ok {
messages = append(messages, result)
printed[section] = struct{}{}
}
}
}

// print the rest
for _, section := range message.Message {
if !section.Enabled(ctx) {
continue
}
if msg := section.Print(ctx); msg != "" {
messages = append(messages, msg, "")
// skip if already printed
if _, ok := printed[section.String()]; ok {
continue
}
if msg, ok := sectionResults[section.String()]; ok {
messages = append(messages, msg)
}
}

Expand Down

0 comments on commit c24f6e2

Please sign in to comment.