Repository navigation
Expand file tree
/
Copy pathcell_list.go
More file actions
67 lines (60 loc) · 2.15 KB
/
Copy pathcell_list.go
File metadata and controls
67 lines (60 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"context"
"errors"
"fmt"
"io"
"text/tabwriter"
"time"
"github.com/Agent-Field/codeaf/internal/cellsync"
"github.com/Agent-Field/codeaf/internal/chatlist"
"github.com/Agent-Field/codeaf/internal/tui2/reltime"
"github.com/Agent-Field/codeaf/internal/wireauth"
)
// cellListAsk bounds one listing so a silent relay ends the command rather
// than hanging an ssh session.
const cellListAsk = 10 * time.Second
// cellListSource is where `cell list --all` reads chats from. It is a seam so
// a test can stand in any source; the real one is the sync setup (syncwire.go).
var cellListSource = syncListSource
// cellList prints every chat this person has, on any machine, one per line:
// id, title, device, what §8.1 says about where it runs, and the age of its
// last durable turn. It is headless on purpose, for ssh-driven demos.
func cellList(args []string, out io.Writer) error {
if len(args) != 1 || args[0] != "--all" {
return errors.New(cellUsage)
}
src, err := cellListSource()
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), cellListAsk)
defer cancel()
rows, err := src.Rows(ctx)
if r, ok := cellsync.RefusalOf(err); ok && (errors.Is(err, wireauth.ErrRotated) || errors.Is(err, wireauth.ErrGone)) {
return errors.New(r.Say(err))
}
if err != nil {
return fmt.Errorf("%s: %w", chatlist.Unreachable, err)
}
return writeChatRows(rows, out)
}
// writeChatRows draws the rows as an aligned table. A column with nothing to
// say (a chat that is here or idle has no status line) shows a dash so the
// columns still line up.
func writeChatRows(rows []chatlist.Row, out io.Writer) error {
w := tabwriter.NewWriter(out, 0, 0, 2, ' ', 0)
for _, r := range rows {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", r.Cell, r.Title, orDash(r.Device),
orDash(statusOf(r)), reltime.Elapsed(r.DurableAgo))
}
return w.Flush()
}
// statusOf is the status column: a branch says only what this build can do with
// it, which is discard (there is no merge, cell_branch.go).
func statusOf(r chatlist.Row) string {
if r.Status == chatlist.Branch {
return chatlist.BranchLine(r, false)
}
return chatlist.StatusLine(r)
}