Skip to content

Commit

Permalink
improve display of launchd job status
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Nov 4, 2024
1 parent c80e0c8 commit 201a3f8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion schedule/handler_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/exec"
"path"
"regexp"
"slices"
"sort"
"strings"
"text/tabwriter"
Expand Down Expand Up @@ -189,12 +190,15 @@ func (h *HandlerLaunchd) DisplayJobStatus(job *Config) error {
// order keys alphabetically
keys := make([]string, 0, len(status))
for key := range status {
if slices.Contains([]string{"LimitLoadToSessionType", "OnDemand"}, key) {
continue

Check warning on line 194 in schedule/handler_darwin.go

View check run for this annotation

Codecov / codecov/patch

schedule/handler_darwin.go#L193-L194

Added lines #L193 - L194 were not covered by tests
}
keys = append(keys, key)
}
sort.Strings(keys)
writer := tabwriter.NewWriter(term.GetOutput(), 0, 0, 0, ' ', tabwriter.AlignRight)
for _, key := range keys {
fmt.Fprintf(writer, "%s:\t %s\n", key, status[key])
fmt.Fprintf(writer, "%s:\t %s\n", spacedTitle(key), status[key])

Check warning on line 201 in schedule/handler_darwin.go

View check run for this annotation

Codecov / codecov/patch

schedule/handler_darwin.go#L201

Added line #L201 was not covered by tests
}
writer.Flush()
fmt.Println("")
Expand Down
18 changes: 18 additions & 0 deletions schedule/spaced_title.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build darwin

package schedule

import "strings"

func spacedTitle(title string) string {
var previous rune
sb := strings.Builder{}
for _, char := range title {
if char >= 'A' && char <= 'Z' && previous != ' ' && sb.Len() > 0 {
sb.WriteByte(' ')
}
sb.WriteRune(char)
previous = char
}
return sb.String()
}
25 changes: 25 additions & 0 deletions schedule/spaced_title_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//go:build darwin

package schedule

import "testing"

func TestSpacedTitle(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"NoSpacesHere", "No Spaces Here"},
{"Already Spaced", "Already Spaced"},
{"", ""},
{"lowercase", "lowercase"},
{"ALLCAPS", "A L L C A P S"},
}

for _, test := range tests {
result := spacedTitle(test.input)
if result != test.expected {
t.Errorf("spacedTitle(%q) = %q; expected %q", test.input, result, test.expected)
}
}
}

0 comments on commit 201a3f8

Please sign in to comment.