11 "github.com/charmbracelet/crush/internal/csync"
15 // MaxBackgroundJobs is the maximum number of concurrent background jobs allowed
16 MaxBackgroundJobs = 50
17 // CompletedJobRetentionMinutes is how long to keep completed jobs before auto-cleanup (8 hours)
18 CompletedJobRetentionMinutes = 8 * 60
21// BackgroundShell represents a shell running in the background.
22type BackgroundShell struct {
29 cancel context.CancelFunc
34 completedAt int64 // Unix timestamp when job completed (0 if still running)
37// BackgroundShellManager manages background shell instances.
38type BackgroundShellManager struct {
39 shells *csync.Map[string, *BackgroundShell]
43 backgroundManager *BackgroundShellManager
44 backgroundManagerOnce sync.Once
45 idCounter atomic.Uint64
48// GetBackgroundShellManager returns the singleton background shell manager.
49func GetBackgroundShellManager() *BackgroundShellManager {
50 backgroundManagerOnce.Do(func() {
51 backgroundManager = &BackgroundShellManager{
52 shells: csync.NewMap[string, *BackgroundShell](),
55 return backgroundManager
58// Start creates and starts a new background shell with the given command.
59func (m *BackgroundShellManager) Start(ctx context.Context, workingDir string, blockFuncs []BlockFunc, command string, description string) (*BackgroundShell, error) {
61 if m.shells.Len() >= MaxBackgroundJobs {
62 return nil, fmt.Errorf("maximum number of background jobs (%d) reached. Please terminate or wait for some jobs to complete", MaxBackgroundJobs)
65 id := fmt.Sprintf("%03X", idCounter.Add(1))
67 shell := NewShell(&Options{
68 WorkingDir: workingDir,
69 BlockFuncs: blockFuncs,
72 shellCtx, cancel := context.WithCancel(ctx)
74 bgShell := &BackgroundShell{
77 Description: description,
78 WorkingDir: workingDir,
82 stdout: &bytes.Buffer{},
83 stderr: &bytes.Buffer{},
84 done: make(chan struct{}),
87 m.shells.Set(id, bgShell)
90 defer close(bgShell.done)
92 err := shell.ExecStream(shellCtx, command, bgShell.stdout, bgShell.stderr)
95 atomic.StoreInt64(&bgShell.completedAt, time.Now().Unix())
101// Get retrieves a background shell by ID.
102func (m *BackgroundShellManager) Get(id string) (*BackgroundShell, bool) {
103 return m.shells.Get(id)
106// Remove removes a background shell from the manager without terminating it.
107// This is useful when a shell has already completed and you just want to clean up tracking.
108func (m *BackgroundShellManager) Remove(id string) error {
109 _, ok := m.shells.Take(id)
111 return fmt.Errorf("background shell not found: %s", id)
116// Kill terminates a background shell by ID.
117func (m *BackgroundShellManager) Kill(id string) error {
118 shell, ok := m.shells.Take(id)
120 return fmt.Errorf("background shell not found: %s", id)
128// BackgroundShellInfo contains information about a background shell.
129type BackgroundShellInfo struct {
135// List returns all background shell IDs.
136func (m *BackgroundShellManager) List() []string {
137 ids := make([]string, 0, m.shells.Len())
138 for id := range m.shells.Seq2() {
139 ids = append(ids, id)
144// Cleanup removes completed jobs that have been finished for more than the retention period
145func (m *BackgroundShellManager) Cleanup() int {
146 now := time.Now().Unix()
147 retentionSeconds := int64(CompletedJobRetentionMinutes * 60)
149 var toRemove []string
150 for shell := range m.shells.Seq() {
151 completedAt := atomic.LoadInt64(&shell.completedAt)
152 if completedAt > 0 && now-completedAt > retentionSeconds {
153 toRemove = append(toRemove, shell.ID)
157 for _, id := range toRemove {
164// KillAll terminates all background shells.
165func (m *BackgroundShellManager) KillAll() {
166 shells := make([]*BackgroundShell, 0, m.shells.Len())
167 for shell := range m.shells.Seq() {
168 shells = append(shells, shell)
170 m.shells.Reset(map[string]*BackgroundShell{})
172 for _, shell := range shells {
178// GetOutput returns the current output of a background shell.
179func (bs *BackgroundShell) GetOutput() (stdout string, stderr string, done bool, err error) {
182 return bs.stdout.String(), bs.stderr.String(), true, bs.exitErr
184 return bs.stdout.String(), bs.stderr.String(), false, nil
188// IsDone checks if the background shell has finished execution.
189func (bs *BackgroundShell) IsDone() bool {
198// Wait blocks until the background shell completes.
199func (bs *BackgroundShell) Wait() {
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list