Class: RBWatch::Dashboard

Inherits:
Object
  • Object
show all
Defined in:
lib/rbwatch/dashboard.rb

Defined Under Namespace

Classes: NullSpinner

Constant Summary collapse

"Watching... Press CTRL+C to stop."
SCROLLBACK_CLEAR =
"\e[3J"
STARTUP_TITLE =
" RBWatch "
RESTART_TITLE =
" 🔄 Changes detected "

Instance Method Summary collapse

Constructor Details

#initialize(output:, theme:, clear: false) ⇒ Dashboard

Returns a new instance of Dashboard.



16
17
18
19
20
# File 'lib/rbwatch/dashboard.rb', line 16

def initialize(output:, theme:, clear: false)
  @output = output
  @theme = theme
  @clear = clear
end

Instance Method Details

#clear_screenObject



22
23
24
25
26
27
28
29
# File 'lib/rbwatch/dashboard.rb', line 22

def clear_screen
  return "" unless tty?

  output.print SCROLLBACK_CLEAR
  output.print TTY::Cursor.clear_screen
  output.print TTY::Cursor.move_to(0, 0)
  ""
end

#hide_cursorObject



31
32
33
34
35
# File 'lib/rbwatch/dashboard.rb', line 31

def hide_cursor
  return unless tty?

  output.print TTY::Cursor.hide
end

#log_error(message) ⇒ Object



128
129
130
# File 'lib/rbwatch/dashboard.rb', line 128

def log_error(message)
  log(:error, message)
end

#log_info(message) ⇒ Object



116
117
118
# File 'lib/rbwatch/dashboard.rb', line 116

def log_info(message)
  log(:info, message)
end

#log_restart(message) ⇒ Object



132
133
134
# File 'lib/rbwatch/dashboard.rb', line 132

def log_restart(message)
  log(:restart, message)
end

#log_success(message) ⇒ Object



120
121
122
# File 'lib/rbwatch/dashboard.rb', line 120

def log_success(message)
  log(:success, message)
end

#log_warning(message) ⇒ Object



124
125
126
# File 'lib/rbwatch/dashboard.rb', line 124

def log_warning(message)
  log(:warning, message)
end

#render_banner(version:, command:, watch_paths:, config_file: nil, theme_name: nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rbwatch/dashboard.rb', line 43

def render_banner(version:, command:, watch_paths:, config_file: nil, theme_name: nil)
  lines = [
    theme.accent("RBWatch #{version}"),
    theme.info("Delightful process restarts for Ruby projects"),
    theme.muted("Command: #{command}"),
    theme.muted("Watching: #{Array(watch_paths).join(', ')}")
  ]
  lines << theme.muted("Config: #{config_file}") if config_file
  lines << theme.muted("Theme: #{theme_name}") if theme_name
  frame = TTY::Box.frame(
    lines.join("\n"),
    width: box_width(lines),
    align: :center,
    padding: [1, 2],
    title: { top_left: STARTUP_TITLE },
    **theme.box_options(:banner)
  )
  output.puts frame
  frame
end


110
111
112
113
114
# File 'lib/rbwatch/dashboard.rb', line 110

def render_footer
  line = theme.muted(FOOTER_MESSAGE)
  output.puts line
  line
end

#render_restart(change_set) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rbwatch/dashboard.rb', line 86

def render_restart(change_set)
  lines = [
    theme.restart("Changes detected"),
    theme.warning("Stopping previous process..."),
    theme.info("Starting new process..."),
    theme.success("Process started successfully")
  ]
  if change_set&.changed?
    summary = change_set.paths.first(5).join(", ")
    summary = "#{summary}, ..." if change_set.paths.length > 5
    lines << theme.muted("Changed files: #{summary}")
  end
  frame = TTY::Box.frame(
    lines.join("\n"),
    width: box_width(lines),
    align: :center,
    padding: [1, 2],
    title: { top_left: RESTART_TITLE },
    **theme.box_options(:restart)
  )
  output.puts frame
  frame
end

#render_status(status:, files:, extensions:, delay:, pid:, command:, config_file: nil, theme_name: nil, stats: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rbwatch/dashboard.rb', line 64

def render_status(status:, files:, extensions:, delay:, pid:, command:, config_file: nil, theme_name: nil, stats: nil)
  rows = [
    [theme.label("Status"), theme.status(status)],
    [theme.label("Files"), theme.files(files.to_s)],
    [theme.label("Extensions"), theme.command(Array(extensions).join(", "))],
    [theme.label("Delay"), theme.delay("#{delay}ms")],
    [theme.label("PID"), theme.pid(pid.to_s)],
    [theme.label("Command"), theme.command(command)]
  ]
  rows.insert(4, [theme.label("Theme"), theme.command(theme_name.to_s)]) if theme_name
  rows.insert(5, [theme.label("Config"), theme.command(config_file.to_s)]) if config_file
  if stats
    rows.insert(4, [theme.label("Restarts"), theme.command(stats.fetch(:restarts).to_s)])
    rows.insert(5, [theme.label("Uptime"), theme.command(stats.fetch(:uptime).to_s)])
  end
  table = TTY::Table.new(header: ["Field", "Value"], rows: rows)
  body = table.render(:basic).lines.drop(1).map { |line| line.rstrip }
  frame = render_table_frame(body, border: theme.border(:status))
  output.puts frame
  frame
end

#show_cursorObject



37
38
39
40
41
# File 'lib/rbwatch/dashboard.rb', line 37

def show_cursor
  return unless tty?

  output.print TTY::Cursor.show
end

#spinner(message) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/rbwatch/dashboard.rb', line 136

def spinner(message)
  if tty?
    TTY::Spinner.new("[:spinner] #{message}", format: :spin, output: output)
  else
    NullSpinner.new(self)
  end
end