Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151// Command rexdump connects to a Rex server and prints what it sees: the// sessions, and the windows, layouts, and blocks of one of them. With// -events, it then prints events as they arrive.//// It is a headless test bed for the rex package.package main
import ( "context" "flag" "fmt" "log" "os" "os/signal" "strings" "time"
"100gex/internal/rex")
func main() { server := flag.String("server", "", "server address, such as unix:///run/user/1000/rex/server.sock (default: $REX_SERVER, else the local server)") session := flag.String("session", "", "session to show, by ID, label, or ID prefix (default: the one the server picks)") events := flag.Bool("events", false, "print events until interrupted") flag.Parse() log.SetFlags(0)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) defer stop() if err := run(ctx, *server, *session, *events); err != nil { log.Fatal(err) }}
func run(ctx context.Context, server, sessionQuery string, follow bool) error { ep, err := rex.DiscoverEndpoint() if server != "" { ep, err = rex.ParseEndpoint(server) } if err != nil { return err }
dialCtx, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() c, err := rex.Dial(dialCtx, ep, rex.ClientInfo{Kind: "cli", Name: "rexdump"}) if err != nil { return err } defer c.Close() w := c.Welcome() fmt.Printf("server %s: api revision %d (server speaks %d-%d), client %s\n", ep, w.APIRevision, w.APIMinRevision, w.APIServerRevision, w.ClientID())
args := rex.BootstrapArgs{EventScope: "session"} if sessionQuery != "" { args.AttachSessionIDs = []string{} // attach none yet: find the session first } boot, seq, err := c.Bootstrap(ctx, args) if err != nil { return err } fmt.Printf("%d sessions:\n", len(boot.AttachAll.Sessions)) for _, s := range boot.AttachAll.Sessions { fmt.Printf(" %s %s\n", s.SessionID, s.Label) }
var view rex.View switch { case sessionQuery != "": s, err := rex.FindSession(boot.AttachAll.Sessions, sessionQuery) if err != nil { return err } if view, seq, err = c.Attach(ctx, s.SessionID); err != nil { return err } case boot.SelectedSession != nil: view = boot.SelectedSession.View default: fmt.Println("no session to attach to") } if view.SessionID != "" { printView(&view) }
if !follow { return nil } fmt.Println("events:") for { select { case ev, ok := <-c.Events(): if !ok { return c.Err() } if ev.Seq <= seq { continue // already in the view } where := "server" if ev.SessionID != "" { where = ev.SessionID } fmt.Printf(" %d %s %s %s\n", ev.Seq, where, ev.Name, ev.Payload) case <-ctx.Done(): return nil } }}
func printView(v *rex.View) { fmt.Printf("session %s %q, revision %d\n", v.SessionID, v.Label, v.Revision) for _, w := range v.Windows { active := "" if w.WindowID == v.ActiveWindowID { active = " (active)" } fmt.Printf(" window %s %q%s\n", w.WindowID, w.Label, active) for _, l := range w.Layers { fmt.Printf(" layer %s %s %v\n", l.Kind, l.State, l.Bounds) if l.Layout != nil { printNode(&w, l.Layout, 6) } } }}
func printNode(w *rex.Window, n *rex.LayoutNode, indent int) { pad := strings.Repeat(" ", indent) if n.Split != nil { fmt.Printf("%ssplit %s %.2f\n", pad, n.Split.Direction, n.Split.Ratio) printNode(w, n.Split.Before, indent+2) printNode(w, n.Split.After, indent+2) return } var notes []string if n.BlockID == w.FocusedBlockID { notes = append(notes, "focused") } if n.BlockID == w.ZoomedBlockID { notes = append(notes, "zoomed") } if b := w.Block(n.BlockID); b != nil { notes = append(notes, b.Flavor) if b.Owner != "" { notes = append(notes, "owner "+b.Owner) } } fmt.Printf("%sblock %s %v %s\n", pad, n.BlockID, n.Rect, strings.Join(notes, ", "))}