Module: VSM::Lens::TUI

Defined in:
lib/vsm/lens/tui.rb

Class Method Summary collapse

Class Method Details

.box(title, lines, width) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/vsm/lens/tui.rb', line 75

def self.box(title, lines, width)
  out  = +"+" + "-"*(width-2) + "+\n"
  out << "| #{title.ljust(width-4)} |\n"
  out << "+" + "-"*(width-2) + "+\n"
  lines.each do |l|
    out << "| #{l.ljust(width-4)} |\n"
  end
  out << "+" + "-"*(width-2) + "+\n"
  out
end

.draw(ring) ⇒ Object



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
68
69
# File 'lib/vsm/lens/tui.rb', line 35

def self.draw(ring)
  cols, rows = IO.console.winsize.reverse # => [rows, cols]
  rows ||= 24; cols ||= 80
  system("printf", "\e[2J\e[H") # clear

  # Split: left sessions, right timeline
  left_w  = [28, cols * 0.3].max.to_i
  right_w = cols - left_w - 1
  puts header("VSM Lens TUI — press 'q' to quit", cols)

  # Sessions (left)
  sessions = Hash.new { |h,k| h[k] = { count: 0, last: "" } }
  ring.each do |ev|
    sid = ev.dig(:meta, :session_id) or next
    sessions[sid][:count] += 1
    sessions[sid][:last]   = ev[:ts]
  end
  sess_lines = sessions.sort_by { |_id, s| s[:last].to_s }.reverse.first(rows-3).map do |sid, s|
    "#{sid[0,8]}  #{s[:count].to_s.rjust(5)}  #{s[:last]}"
  end

  puts box("Sessions", sess_lines, left_w)

  # Timeline (right)
  tl = ring.last(rows-3).map do |ev|
    kind = ev[:kind].to_s.ljust(16)
    sid  = ev.dig(:meta, :session_id)&.slice(0,8) || ""
    txt  = case ev[:payload]
           when String then ev[:payload].gsub(/\s+/, " ")[0, right_w-40]
           else ev[:payload].to_s[0, right_w-40]
           end
    "#{ev[:ts]}  #{kind} #{sid}  #{txt}"
  end
  puts box("Timeline", tl, right_w)
end

.header(text, width) ⇒ Object



71
72
73
# File 'lib/vsm/lens/tui.rb', line 71

def self.header(text, width)
  "\e[7m #{text.ljust(width-2)} \e[0m"
end

.start(hub, ring_max: 500) ⇒ Object

Start a simple TUI that renders the last N events and sessions. Usage:

hub = VSM::Lens.attach!(capsule)
VSM::Lens::TUI.start(hub)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vsm/lens/tui.rb', line 12

def self.start(hub, ring_max: 500)
  queue, snapshot = hub.subscribe
  ring = snapshot.last(ring_max)

  reader = Thread.new do
    loop { ring << queue.pop; ring.shift if ring.size > ring_max }
  end

  trap("INT")  { exit }
  trap("TERM") { exit }

  STDIN.raw do
    loop do
      draw(ring)
      # Non-blocking single-char read; press 'q' to quit
      ch = if IO.select([STDIN], nil, nil, 0.1) then STDIN.read_nonblock(1) rescue nil end
      exit if ch == "q"
    end
  end
ensure
  reader&.kill
end