Module: KamalX

Defined in:
lib/kamalx.rb

Overview

Provides the entry point for the command-line utility.

Examples:

KamalX.run(ARGV)

Defined Under Namespace

Classes: ConnectionHandler

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.color_mapObject



241
242
243
244
245
246
247
248
249
# File 'lib/kamalx.rb', line 241

def self.color_map
  @color_map ||= {
    blue: Curses::COLOR_BLUE,
    green: Curses::COLOR_GREEN,
    yellow: Curses::COLOR_YELLOW,
    red: Curses::COLOR_RED,
    white: Curses::COLOR_WHITE
  }
end

.command_finishedObject



154
155
156
157
158
159
160
# File 'lib/kamalx.rb', line 154

def self.command_finished
  @command_finished = true
  @@progress_bar.finish
  @progress_window.refresh
  display_line([:red, [:bold, "Kamal finished. Press 'ctrl+c' to exit."]])
  EventMachine.run {} # Keep the event loop running
end

.display_line(parsed_line) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/kamalx.rb', line 286

def self.display_line(parsed_line)
  # Check if the line is a stage command
  if parsed_line.any? { |element| element.is_a?(Array) && element[1] == 'Stage:' }
    display_stage(parsed_line)
  else
    @output_window.scroll
    @output_window.setpos(@output_window.maxy - 1, 0)

    current_color = nil

    parsed_line.each do |element|
      if element.is_a?(Symbol)
        current_color = color_map[element]
      elsif element.is_a?(Array)
        if element[0] == :bold
          @output_window.attron(Curses::A_BOLD) do
            @output_window.attron(Curses.color_pair(current_color)) if current_color
            @output_window.addstr(element[1].to_s)
            @output_window.attroff(Curses.color_pair(current_color)) if current_color
          end
        else
          @output_window.addstr(element[1].to_s)
        end
      else
        @output_window.attron(Curses.color_pair(current_color)) if current_color
        @output_window.addstr(element.to_s)
        @output_window.attroff(Curses.color_pair(current_color)) if current_color
      end
    end

    @output_window.refresh
    @output_box.refresh
  end
end

.display_stage(parsed_line) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/kamalx.rb', line 263

def self.display_stage(parsed_line)
  @stage_window.scroll
  @stage_window.setpos(@stage_window.maxy - 1, 0)

  current_color = nil

  parsed_line.each do |element|
    if element.is_a?(Symbol)
      current_color = color_map[element]
      @stage_window.attron(Curses.color_pair(current_color)) if current_color
    elsif element.is_a?(Array) && element[0] == :bold
      @stage_window.attron(Curses::A_BOLD)
      @stage_window.addstr(element[1].to_s)
      @stage_window.attroff(Curses::A_BOLD)
    else
      @stage_window.addstr(element.to_s)
    end
  end

  @stage_window.attroff(Curses.color_pair(current_color)) if current_color
  @stage_window.refresh
end

.refresh_displayObject



162
163
164
165
166
167
# File 'lib/kamalx.rb', line 162

def self.refresh_display
  @stage_window.refresh
  @output_window.refresh
  @output_box.refresh
  @progress_window.refresh
end

.run(args) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/kamalx.rb', line 137

def self.run(args)
  loop do
    setup_curses
    setup_signal_trap
    @command_finished = false
    EventMachine.run do
      command = ['kamal', *args].join(' ')
      @connection_handler = EventMachine.popen(command, ConnectionHandler, @@progress_bar)

      # Add a timer to blink the cursor
      EventMachine.add_periodic_timer(0.5) do
        @@progress_bar.draw unless @command_finished
      end
    end
  end
end

.setup_cursesObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/kamalx.rb', line 181

def self.setup_curses
  Curses.init_screen
  Curses.start_color
  Curses.use_default_colors
  Curses.cbreak
  Curses.noecho
  Curses.stdscr.keypad(true)
  Curses.timeout = 0 # Non-blocking getch

  # Initialize color pairs
  Curses.init_pair(Curses::COLOR_BLUE, Curses::COLOR_BLUE, -1)
  Curses.init_pair(Curses::COLOR_GREEN, Curses::COLOR_GREEN, -1)
  Curses.init_pair(Curses::COLOR_YELLOW, Curses::COLOR_YELLOW, -1)
  Curses.init_pair(Curses::COLOR_RED, Curses::COLOR_RED, -1)
  Curses.init_pair(Curses::COLOR_WHITE, Curses::COLOR_WHITE, -1)
  Curses.init_pair(6, Curses::COLOR_GREEN, -1) # New color pair for green progress bar

  total_lines = Curses.lines
  progress_height = 4
  space_height = 1
  empty_line_height = 1
  remaining_height = total_lines - progress_height - space_height - empty_line_height

  # Create a boxed window for the progress bar
  progress_box = Curses::Window.new(progress_height, Curses.cols, 0, 0)
  progress_box.box('|', '-')
  progress_box.setpos(0, 2)
  progress_box.addstr(' Progress ')
  progress_box.refresh
  @progress_window = progress_box.subwin(2, Curses.cols - 2, 1, 1)

  # Calculate equal heights for stages and output
  section_height = remaining_height / 2

  # Create a boxed window for the stages
  stages_start = progress_height + space_height
  stage_box = Curses::Window.new(section_height, Curses.cols, stages_start, 0)
  stage_box.box('|', '-')
  stage_box.setpos(0, 2)
  stage_box.addstr(' Stage History ')
  stage_box.refresh
  @stage_window = stage_box.subwin(section_height - 2, Curses.cols - 2, stages_start + 1, 1)

  # Create a boxed window for the output area
  output_start = stages_start + section_height + empty_line_height
  @output_box = Curses::Window.new(section_height, Curses.cols, output_start, 0)
  @output_box.box('|', '-')
  @output_box.setpos(0, 2)
  @output_box.addstr(' Command Outputs ')
  @output_box.refresh

  # Create a subwindow inside the box for scrollable content
  @output_window = @output_box.subwin(section_height - 2, Curses.cols - 2, output_start + 1, 1)

  @stage_window.scrollok(true)
  @output_window.scrollok(true)

  @@progress_bar = ProgressBar.new(@progress_window)
end

.setup_signal_trapObject



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/kamalx.rb', line 169

def self.setup_signal_trap
  Signal.trap('INT') do
    Thread.new do
      sleep 2
      exit!(1)
    end

    EventMachine.stop
    exit(0)
  end
end

Instance Method Details

#receive_data(data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



254
255
256
257
258
259
260
261
# File 'lib/kamalx.rb', line 254

def receive_data(data)
  parser = LogParser.new
  data.each_line do |line|
    parsed_line = parser.parse(line)
    KamalX.display_line(parsed_line)
    @progress_bar.update(line)
  end
end