Files
gastown/internal/cmd/crew_cycle.go
Steve Yegge 62f603bbe1 feat: Add C-b n/p keybindings for crew session cycling
When multiple crew sessions exist in the same rig, C-b n cycles to next
and C-b p cycles to previous. Sessions are sorted alphabetically and
wrap around.

Implementation:
- crew_cycle.go: Hidden `gt crew next/prev` commands for tmux to call
- crew_helpers.go: parseCrewSessionName and findRigCrewSessions helpers
- crew_at.go: Calls SetCrewCycleBindings on session creation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-25 11:51:48 -08:00

82 lines
1.9 KiB
Go

package cmd
import (
"fmt"
"os/exec"
"sort"
"github.com/spf13/cobra"
)
// cycleCrewSession switches to the next or previous crew session in the same rig.
// direction: 1 for next, -1 for previous
func cycleCrewSession(direction int) error {
// Get current session (uses existing function from handoff.go)
currentSession, err := getCurrentTmuxSession()
if err != nil {
return fmt.Errorf("not in a tmux session: %w", err)
}
if currentSession == "" {
return fmt.Errorf("not in a tmux session")
}
// Parse rig name from current session
rigName, _, ok := parseCrewSessionName(currentSession)
if !ok {
return fmt.Errorf("not in a crew session (expected gt-<rig>-crew-<name>)")
}
// Find all crew sessions for this rig
sessions, err := findRigCrewSessions(rigName)
if err != nil {
return fmt.Errorf("listing sessions: %w", err)
}
if len(sessions) == 0 {
return fmt.Errorf("no crew sessions found for rig %s", rigName)
}
// Sort for consistent ordering
sort.Strings(sessions)
// Find current position
currentIdx := -1
for i, s := range sessions {
if s == currentSession {
currentIdx = i
break
}
}
if currentIdx == -1 {
// Current session not in list (shouldn't happen)
return fmt.Errorf("current session not found in crew list")
}
// Calculate target index (with wrapping)
targetIdx := (currentIdx + direction + len(sessions)) % len(sessions)
if targetIdx == currentIdx {
// Only one session, nothing to switch to
return nil
}
targetSession := sessions[targetIdx]
// Switch to target session
cmd := exec.Command("tmux", "switch-client", "-t", targetSession)
if err := cmd.Run(); err != nil {
return fmt.Errorf("switching to %s: %w", targetSession, err)
}
return nil
}
func runCrewNext(cmd *cobra.Command, args []string) error {
return cycleCrewSession(1)
}
func runCrewPrev(cmd *cobra.Command, args []string) error {
return cycleCrewSession(-1)
}