Skip to content

Commit

Permalink
Merge pull request #6 from stigoleg/help-text
Browse files Browse the repository at this point in the history
added version to help section
  • Loading branch information
stigoleg authored Dec 26, 2024
2 parents d4ead90 + 048236f commit 45ebe06
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'
go-version: '1.23'
- name: Build
run: go build ./...
run: go build -o keepalive ./cmd/keepalive
- name: Test
run: go test -v ./...
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ changelog:
release:
github:
owner: stigoleg
name: keep-alive
name: keepalive

3 changes: 2 additions & 1 deletion cmd/keepalive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
)

const appVersion = "1.2.0"
const appVersion = "1.2.1"

func main() {
cfg, err := config.ParseFlags(appVersion)
Expand All @@ -36,6 +36,7 @@ func main() {
} else {
model = ui.InitialModel()
}
model.SetVersion(appVersion)

// Create program with signal handling
p := tea.NewProgram(
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/stigoleg/keep-alive

go 1.21.0
go 1.23

require (
github.com/charmbracelet/bubbletea v1.2.4
Expand Down
1 change: 1 addition & 0 deletions internal/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func ParseFlags(version string) (*Config, error) {
flags.Usage = func() {
model := ui.InitialModel()
model.ShowHelp = true
model.SetVersion(version)
fmt.Print(model.View())
}

Expand Down
2 changes: 1 addition & 1 deletion internal/keepalive/keepalive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestKeepAlive(t *testing.T) {
t.Run("Timed Operation", func(t *testing.T) {
k := &Keeper{}
defer k.Stop() // Ensure cleanup even if test fails

// Start timed
err := k.StartTimed(2 * time.Second)
if err != nil && err.Error() == "unsupported platform" {
Expand Down
8 changes: 4 additions & 4 deletions internal/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (k *darwinKeepAlive) Start(ctx context.Context) error {
Setpgid: true,
Pgid: 0,
}

if err := k.cmd.Start(); err != nil {
k.cancel()
return err
Expand Down Expand Up @@ -90,7 +90,7 @@ func (k *darwinKeepAlive) killProcess() {
k.cmd.Process.Wait()
close(done)
}()

select {
case <-done:
return
Expand All @@ -101,10 +101,10 @@ func (k *darwinKeepAlive) killProcess() {

// Process didn't terminate with SIGTERM, try SIGKILL
k.cmd.Process.Kill()

// Kill the process group as well
syscall.Kill(-pid, syscall.SIGKILL)

// Use pkill as a last resort
exec.Command("pkill", "-9", "caffeinate").Run()
}
Expand Down
2 changes: 1 addition & 1 deletion internal/platform/platform_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type linuxKeepAlive struct {

// trySystemdInhibit attempts to use systemd-inhibit
func trySystemdInhibit(ctx context.Context) (*exec.Cmd, error) {
cmd := exec.CommandContext(ctx, "systemd-inhibit", "--what=idle:sleep:handle-lid-switch",
cmd := exec.CommandContext(ctx, "systemd-inhibit", "--what=idle:sleep:handle-lid-switch",
"--who=keep-alive", "--why=Prevent system sleep", "--mode=block",
"sleep", "infinity")
err := cmd.Start()
Expand Down
10 changes: 5 additions & 5 deletions internal/platform/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ func getCaffeinateProcesses() ([]int, error) {
if runtime.GOOS != "darwin" {
return nil, nil
}

cmd := exec.Command("pgrep", "caffeinate")
output, err := cmd.Output()
if err != nil {
// No processes found is not an error for our purposes
return nil, nil
}

// Parse PIDs from output
var pids []int
for _, line := range strings.Split(strings.TrimSpace(string(output)), "\n") {
Expand Down Expand Up @@ -50,7 +50,7 @@ func killCaffeinate() error {
}
}
time.Sleep(100 * time.Millisecond)

// Check if any processes remain that we started
if pids, _ := getCaffeinateProcesses(); len(pids) > 0 {
// Try SIGKILL for remaining processes
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestKeepAlive(t *testing.T) {

// Give caffeinate time to start
time.Sleep(200 * time.Millisecond)

// Check if we have exactly one more caffeinate process than we started with
pids, err := getCaffeinateProcesses()
if err != nil {
Expand All @@ -131,7 +131,7 @@ func TestKeepAlive(t *testing.T) {

// Final check with debug info
if pids, _ := getCaffeinateProcesses(); len(pids) > initialCount {
t.Errorf("Found %d extra caffeinate processes still running after stop: %v (initial count was %d)",
t.Errorf("Found %d extra caffeinate processes still running after stop: %v (initial count was %d)",
len(pids)-initialCount, pids, initialCount)
}
}
2 changes: 1 addition & 1 deletion internal/platform/platform_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
const (
esSystemRequired = 0x00000001
esDisplayRequired = 0x00000002
esContinuous = 0x80000000
esContinuous = 0x80000000
)

var (
Expand Down
11 changes: 11 additions & 0 deletions internal/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Model struct {
StartTime time.Time
Duration time.Duration
ShowHelp bool
version string
}

// InitialModel returns the initial model for the TUI.
Expand Down Expand Up @@ -92,3 +93,13 @@ func (m Model) TimeRemaining() time.Duration {
}
return remaining
}

// SetVersion sets the version for the help text
func (m *Model) SetVersion(version string) {
m.version = version
}

// Version returns the current version
func (m Model) Version() string {
return m.version
}
4 changes: 2 additions & 2 deletions internal/ui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,14 @@ func cleanup(m Model) (Model, error) {
if err := m.KeepAlive.Stop(); err != nil {
return m, err
}

// Reset all state
m.State = stateMenu
m.Input = ""
m.Duration = 0
m.StartTime = time.Time{}
m.ErrorMessage = ""

return m, nil
}

Expand Down
7 changes: 4 additions & 3 deletions internal/ui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// View renders the current state of the model to a string.
func View(m Model) string {
if m.ShowHelp {
return helpView()
return helpView(m)
}

switch m.State {
Expand Down Expand Up @@ -163,8 +163,9 @@ func runningView(m Model) string {
return b.String()
}

func helpView() string {
func helpView(m Model) string {
help := `Keep-Alive Help
Version: %s
Usage:
keepalive [flags]
Expand All @@ -188,5 +189,5 @@ Navigation:
Press 'q' or 'Esc' to close help`

return Current.Help.Render(help)
return Current.Help.Render(fmt.Sprintf(help, m.Version()))
}

0 comments on commit 45ebe06

Please sign in to comment.