Class: DiscourseSystray::Systray

Inherits:
Object
  • Object
show all
Defined in:
lib/discourse_systray/systray.rb

Constant Summary collapse

CONFIG_DIR =
File.expand_path("~/.config/discourse-systray")
CONFIG_FILE =
File.join(CONFIG_DIR, "config.json")
OPTIONS =
{ debug: false, path: nil }
BUFFER_SIZE =
1000
BUFFER_TRIM_INTERVAL =

seconds

30
PIPE_PATH =
"/tmp/discourse_systray_cmd"
PID_FILE =
"/tmp/discourse_systray.pid"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSystray

Returns a new instance of Systray.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/discourse_systray/systray.rb', line 80

def initialize
  puts "DEBUG: Initializing DiscourseSystray" if OPTIONS[:debug]
  
  @discourse_path = self.class.load_or_prompt_config unless OPTIONS[:attach]
  puts "DEBUG: Discourse path: #{@discourse_path}" if OPTIONS[:debug]
  
  @running = false
  @ember_output = []
  @unicorn_output = []
  @processes = {}
  @ember_running = false
  @unicorn_running = false
  @status_window = nil
  @buffer_trim_timer = nil
  
  # Initialize pipe queue for background processing
  initialize_pipe_queue
  
  # Add initial welcome message to buffers with timestamp
  timestamp = Time.now.strftime("%H:%M:%S")
  @ember_output << "#{timestamp} - Discourse Ember CLI Log\n"
  @ember_output << "Start Discourse to see Ember CLI logs here.\n"
  @ember_output << "\n"
  
  @unicorn_output << "#{timestamp} - Discourse Unicorn Log\n"
  @unicorn_output << "Start Discourse to see Unicorn logs here.\n"
  @unicorn_output << "\n"
  
  # Add a visual separator
  @ember_output << "=" * 50 + "\n"
  @unicorn_output << "=" * 50 + "\n"
  
  puts "DEBUG: Added initial data to buffers" if OPTIONS[:debug]
  puts "DEBUG: ember_output size: #{@ember_output.size}" if OPTIONS[:debug]
  puts "DEBUG: unicorn_output size: #{@unicorn_output.size}" if OPTIONS[:debug]
  
  # Set up periodic buffer trimming
  setup_buffer_trim_timer unless OPTIONS[:attach]
  
  puts "DEBUG: Initialized DiscourseSystray with path: #{@discourse_path}" if OPTIONS[:debug]
end

Instance Attribute Details

#discourse_pathObject (readonly)

Returns the value of attribute discourse_path.



816
817
818
# File 'lib/discourse_systray/systray.rb', line 816

def discourse_path
  @discourse_path
end

Class Method Details

.load_configObject



71
72
73
74
75
76
# File 'lib/discourse_systray/systray.rb', line 71

def self.load_config
  return {} unless File.exist?(CONFIG_FILE)
  JSON.parse(File.read(CONFIG_FILE))
rescue JSON::ParserError
  {}
end

.load_or_prompt_configObject



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
# File 'lib/discourse_systray/systray.rb', line 14

def self.load_or_prompt_config
  OptionParser
    .new do |opts|
      opts.banner = "Usage: systray.rb [options]"
      opts.on("--debug", "Enable debug mode") { OPTIONS[:debug] = true }
      opts.on("--path PATH", "Set Discourse path") do |p|
        OPTIONS[:path] = p
      end
      opts.on("--console", "Enable console mode") do
        OPTIONS[:console] = true
      end
      opts.on("--attach", "Attach to existing systray") do
        OPTIONS[:attach] = true
      end
    end
    .parse!
  FileUtils.mkdir_p(CONFIG_DIR) unless Dir.exist?(CONFIG_DIR)

  if OPTIONS[:path]
    save_config(path: OPTIONS[:path])
    return OPTIONS[:path]
  end

  if File.exist?(CONFIG_FILE)
    config = JSON.parse(File.read(CONFIG_FILE))
    return config["path"] if config["path"] && Dir.exist?(config["path"])
  end

  # Show dialog to get path
  dialog =
    Gtk::FileChooserDialog.new(
      title: "Select Discourse Directory",
      parent: nil,
      action: :select_folder,
      buttons: [["Cancel", :cancel], ["Select", :accept]]
    )

  path = nil
  if dialog.run == :accept
    path = dialog.filename
    save_config(path: path)
  else
    puts "No Discourse path specified. Exiting."
    exit 1
  end

  dialog.destroy
  path
end

.runObject



818
819
820
# File 'lib/discourse_systray/systray.rb', line 818

def self.run
  new.run
end

.running?Boolean

Returns:

  • (Boolean)


802
803
804
805
806
807
808
809
810
811
812
813
814
# File 'lib/discourse_systray/systray.rb', line 802

def self.running?
  return false unless File.exist?(PID_FILE)
  pid = File.read(PID_FILE).to_i
  Process.kill(0, pid)
  true
rescue Errno::ESRCH, Errno::ENOENT
  begin
    File.unlink(PID_FILE)
  rescue StandardError
    nil
  end
  false
end

.save_config(path:, window_geometry: nil) ⇒ Object



64
65
66
67
68
69
# File 'lib/discourse_systray/systray.rb', line 64

def self.save_config(path:, window_geometry: nil)
  config = { path: path }
  config[:window_geometry] = window_geometry if window_geometry
  File.write(CONFIG_FILE, JSON.generate(config))
  nil # Prevent return value from being printed
end

Instance Method Details

#cleanupObject



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/discourse_systray/systray.rb', line 227

def cleanup
  return if @processes.empty?

  # First disable updates to prevent race conditions
  @view_timeouts&.values&.each do |id|
    begin
      GLib::Source.remove(id)
    rescue StandardError => e
      puts "Error removing timeout: #{e}" if OPTIONS[:debug]
    end
  end
  @view_timeouts&.clear
  
  # Remove buffer trim timer if it exists
  if @buffer_trim_timer
    begin
      GLib::Source.remove(@buffer_trim_timer)
      @buffer_trim_timer = nil
    rescue StandardError => e
      puts "Error removing buffer trim timer: #{e}" if OPTIONS[:debug]
    end
  end
  
  # Stop pipe thread
  if @pipe_thread
    begin
      @pipe_queue.push(:exit) if @pipe_queue
      @pipe_thread.join(2) # Wait up to 2 seconds
      @pipe_thread.kill if @pipe_thread.alive?
    rescue StandardError => e
      puts "Error stopping pipe thread: #{e}" if OPTIONS[:debug]
    end
  end

  # Then stop processes
  @processes.each do |name, process|
    begin
      Process.kill("TERM", process[:pid])
      # Wait for process to finish with timeout
      Timeout.timeout(10) { process[:thread].join }
    rescue StandardError => e
      puts "Error stopping #{name}: #{e}" if OPTIONS[:debug]
    end
  end
  @processes.clear
  @ember_running = false
  @unicorn_running = false

  # Finally clean up UI elements
  update_tab_labels if @notebook && !@notebook.destroyed?

  if @status_window && !@status_window.destroyed?
    @status_window.destroy
    @status_window = nil
  end
end

#create_log_view(buffer) ⇒ Object



569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/discourse_systray/systray.rb', line 569

def create_log_view(buffer)
  puts "DEBUG: create_log_view called for #{buffer == @ember_output ? 'ember' : 'unicorn'}" if OPTIONS[:debug]
  
  # Create a scrolled window to contain the text view
  scroll = Gtk::ScrolledWindow.new
  
  # Create a simple text view with minimal configuration
  text_view = Gtk::TextView.new
  text_view.editable = false
  text_view.cursor_visible = false
  text_view.wrap_mode = :word
  
  # Use a fixed-width font
  text_view.monospace = true
  
  # Set colors - white text on black background
  text_view.override_background_color(:normal, Gdk::RGBA.new(0, 0, 0, 1))
  text_view.override_color(:normal, Gdk::RGBA.new(1, 1, 1, 1))
  
  # Set font size explicitly
  font_desc = Pango::FontDescription.new
  font_desc.family = "Monospace"
  font_desc.size = 12 * Pango::SCALE
  text_view.override_font(font_desc)
  
  # Set initial text
  text_view.buffer.text = "Loading log data...\n"
  
  # Add the text view to the scrolled window
  scroll.add(text_view)
  
  # Set up a timer to update the view more frequently
  @view_timeouts ||= {}
  timeout_id = GLib::Timeout.add(250) do
    if text_view.destroyed? || scroll.destroyed?
      @view_timeouts.delete(text_view.object_id)
      false # Stop the timer
    else
      begin
        update_log_view(text_view, buffer)
      rescue => e
        puts "DEBUG: Error updating log view: #{e.message}" if OPTIONS[:debug]
      end
      true # Continue the timer
    end
  end
  
  # Store the timeout ID for cleanup
  @view_timeouts[text_view.object_id] = timeout_id
  
  # Clean up when the view is destroyed
  text_view.signal_connect("destroy") do
    if id = @view_timeouts.delete(text_view.object_id)
      GLib::Source.remove(id) rescue nil
    end
  end
  
  # Do an initial update
  update_log_view(text_view, buffer)
  
  # Return the scrolled window
  scroll
end

#create_status_label(text, running) ⇒ Object



695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
# File 'lib/discourse_systray/systray.rb', line 695

def create_status_label(text, running)
  box = Gtk::Box.new(:horizontal, 5)
  label = Gtk::Label.new(text)
  status = Gtk::Label.new
  color =
    (
      if running
        Gdk::RGBA.new(0.2, 0.8, 0.2, 1)
      else
        Gdk::RGBA.new(0.8, 0.2, 0.2, 1)
      end
    )
  status.override_color(:normal, color)
  status.text = running ? "" : ""
  box.pack_start(label, expand: false, fill: false, padding: 0)
  box.pack_start(status, expand: false, fill: false, padding: 0)
  box.show_all
  box
end

#handle_command(cmd) ⇒ Object



973
974
975
# File 'lib/discourse_systray/systray.rb', line 973

def handle_command(cmd)
  puts "Received command: #{cmd}" if OPTIONS[:debug]
end

#init_systrayObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
# File 'lib/discourse_systray/systray.rb', line 146

def init_systray
  @indicator = Gtk::StatusIcon.new
  @indicator.pixbuf =
    GdkPixbuf::Pixbuf.new(
      file: File.join(File.dirname(__FILE__), "../../assets/discourse.png")
    )
  @indicator.tooltip_text = "Discourse Manager"

  @indicator.signal_connect("popup-menu") do |tray, button, time|
    menu = Gtk::Menu.new

    # Create menu items with icons
    start_item = Gtk::ImageMenuItem.new(label: "Start Discourse")
    start_item.image =
      Gtk::Image.new(icon_name: "media-playback-start", size: :menu)

    stop_item = Gtk::ImageMenuItem.new(label: "Stop Discourse")
    stop_item.image =
      Gtk::Image.new(icon_name: "media-playback-stop", size: :menu)

    status_item = Gtk::ImageMenuItem.new(label: "Show Status")
    status_item.image =
      Gtk::Image.new(icon_name: "utilities-system-monitor", size: :menu)

    quit_item = Gtk::ImageMenuItem.new(label: "Quit")
    quit_item.image =
      Gtk::Image.new(icon_name: "application-exit", size: :menu)

    # Add items in new order
    menu.append(start_item)
    menu.append(stop_item)
    menu.append(Gtk::SeparatorMenuItem.new)
    menu.append(status_item)
    menu.append(Gtk::SeparatorMenuItem.new)
    menu.append(quit_item)

    start_item.signal_connect("activate") do
      set_icon(:running)
      start_discourse
      @running = true
    end

    stop_item.signal_connect("activate") do
      set_icon(:stopped)
      stop_discourse
      @running = false
    end

    quit_item.signal_connect("activate") do
      cleanup
      Gtk.main_quit
    end

    status_item.signal_connect("activate") { show_status_window }

    menu.show_all

    # Show/hide items based on running state - AFTER show_all
    start_item.visible = !@running
    stop_item.visible = @running
    menu.popup(nil, nil, button, time)
  end
end

#initialize_pipe_queueObject

Queue for pipe messages to avoid blocking



933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
# File 'lib/discourse_systray/systray.rb', line 933

def initialize_pipe_queue
  @pipe_queue = Queue.new
  @pipe_thread = Thread.new do
    loop do
      begin
        msg = @pipe_queue.pop
        break if msg == :exit
        
        if File.exist?(PIPE_PATH)
          begin
            # Use non-blocking write with timeout
            Timeout.timeout(0.5) do
              File.open(PIPE_PATH, "w") do |f|
                f.puts(msg)
                f.flush
              end
            end
          rescue Timeout::Error
            puts "Timeout writing to pipe" if OPTIONS[:debug]
          rescue Errno::EPIPE, IOError => e
            puts "Error writing to pipe: #{e}" if OPTIONS[:debug]
          end
        end
      rescue => e
        puts "Error in pipe thread: #{e}" if OPTIONS[:debug]
      end
      
      # Small sleep to prevent CPU hogging
      sleep 0.01
    end
  end
end

#publish_to_pipe(msg, process: nil, stream: nil) ⇒ Object



966
967
968
969
970
971
# File 'lib/discourse_systray/systray.rb', line 966

def publish_to_pipe(msg, process: nil, stream: nil)
  source_info = "[#{process || 'unknown'}:#{stream || 'unknown'}]"
  puts "Publish to pipe #{source_info}: #{msg}" if OPTIONS[:debug]
  
  @pipe_queue.push(msg) if @pipe_queue
end

#runObject



822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
# File 'lib/discourse_systray/systray.rb', line 822

def run
  if OPTIONS[:attach]
    require "rb-inotify"

    # Set up signal handling for Ctrl+C
    Signal.trap("INT") do
      puts "Received interrupt signal, shutting down..."
      exit 0
    end
    
    notifier = INotify::Notifier.new

    begin
      pipe = File.open(PIPE_PATH, "r")

      # Watch for pipe deletion
      notifier.watch(File.dirname(PIPE_PATH), :delete) do |event|
        if event.name == File.basename(PIPE_PATH)
          puts "Pipe was deleted, exiting."
          exit 0
        end
      end

      # Read from pipe in a separate thread
      reader = Thread.new do
        # Set thread abort on exception
        Thread.current.abort_on_exception = true
        
        begin
          while true
            begin
              # Use non-blocking read with timeout
              ready = IO.select([pipe], nil, nil, 0.1)
              if ready && ready[0].include?(pipe)
                line = pipe.gets
                if line
                  puts line
                  STDOUT.flush
                end
              end
            rescue IOError, Errno::EBADF => e
              puts "DEBUG: Pipe read error: #{e.message}" if OPTIONS[:debug]
              break
            end

            # Check if pipe still exists
            unless File.exist?(PIPE_PATH)
              puts "Pipe was deleted, exiting."
              exit 0
            end
            
            # Small sleep to prevent CPU hogging
            sleep 0.01
          end
        rescue EOFError, IOError
          puts "Pipe closed, exiting."
          exit 0
        end
      end
      
      # Set up non-blocking notifier processing
      # Instead of notifier.run which blocks indefinitely, use a loop with timeout
      while true
        # Process any pending inotify events, with timeout
        notifier.process
        
        # Sleep briefly to prevent CPU hogging
        sleep 0.1
      end
    rescue Errno::ENOENT
      puts "Pipe doesn't exist, exiting."
      exit 1
    rescue Interrupt
      puts "Interrupted, exiting."
      exit 0
    ensure
      reader&.kill
      pipe&.close
      notifier&.close
    end
  else
    return if self.class.running?

    system("mkfifo #{PIPE_PATH}") unless File.exist?(PIPE_PATH)

    # Create named pipe and write PID file
    system("mkfifo #{PIPE_PATH}") unless File.exist?(PIPE_PATH)
    File.write(PID_FILE, Process.pid.to_s)

    # Set up cleanup on exit
    at_exit do
      begin
        File.unlink(PIPE_PATH) if File.exist?(PIPE_PATH)
        File.unlink(PID_FILE) if File.exist?(PID_FILE)
      rescue StandardError => e
        puts "Error during cleanup: #{e}" if OPTIONS[:debug]
      end
    end

    # Initialize GTK
    Gtk.init

    # Setup systray icon and menu
    init_systray
    
    # Start GTK main loop
    Gtk.main
  end
end

#save_window_geometryObject



742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
# File 'lib/discourse_systray/systray.rb', line 742

def save_window_geometry
  return unless @status_window&.visible? && @status_window.window

  x, y = @status_window.position
  width, height = @status_window.size

  self.class.save_config(
    path: @discourse_path,
    window_geometry: {
      "x" => x,
      "y" => y,
      "width" => width,
      "height" => height
    }
  )
end

#set_icon(status) ⇒ Object



759
760
761
762
763
# File 'lib/discourse_systray/systray.rb', line 759

def set_icon(status)
  icon_file = status == :running ? "discourse_running.png" : "discourse.png"
  icon_path = File.join(File.dirname(__FILE__), "../../assets", icon_file)
  @indicator.pixbuf = GdkPixbuf::Pixbuf.new(file: icon_path)
end

#setup_buffer_trim_timerObject



122
123
124
125
126
127
# File 'lib/discourse_systray/systray.rb', line 122

def setup_buffer_trim_timer
  @buffer_trim_timer = GLib::Timeout.add_seconds(BUFFER_TRIM_INTERVAL) do
    trim_buffers
    true # Keep the timer running
  end
end

#show_status_windowObject



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/discourse_systray/systray.rb', line 439

def show_status_window
  puts "DEBUG: show_status_window called" if OPTIONS[:debug]
  
  if @status_window&.visible?
    puts "DEBUG: Status window already visible, presenting it" if OPTIONS[:debug]
    @status_window.present
    # Force window to current workspace in i3
    if @status_window.window
      @status_window.window.raise
      if system("which i3-msg >/dev/null 2>&1")
        system("i3-msg '[id=#{@status_window.window.xid}] move workspace current'")
        system("i3-msg '[id=#{@status_window.window.xid}] focus'")
      end
    end
    
    # Force an update of the views even if window is already visible
    GLib::Idle.add do
      update_all_views
      false
    end
    
    return
  end

  # Clean up any existing window and notebook
  if @status_window
    puts "DEBUG: Destroying existing status window" if OPTIONS[:debug]
    @notebook = nil # Clear notebook reference
    @ember_view = nil
    @unicorn_view = nil
    @ember_label = nil
    @unicorn_label = nil
    @status_window.destroy
    @status_window = nil
  end

  # Create new window and components
  puts "DEBUG: Creating new status window" if OPTIONS[:debug]
  @status_window = Gtk::Window.new("Discourse Status")
  @status_window.set_wmclass("discourse-status", "Discourse Status")

  # Load saved geometry or use defaults
  config = self.class.load_config
  if config["window_geometry"]
    geo = config["window_geometry"]
    @status_window.move(geo["x"], geo["y"])
    @status_window.resize(geo["width"], geo["height"])
    puts "DEBUG: Set window geometry from config: #{geo.inspect}" if OPTIONS[:debug]
  else
    @status_window.set_default_size(800, 600)
    @status_window.window_position = :center
    puts "DEBUG: Set default window size 800x600" if OPTIONS[:debug]
  end
  @status_window.type_hint = :dialog
  @status_window.set_role("discourse-status-dialog")

  # Handle window destruction and hide
  @status_window.signal_connect("delete-event") do
    puts "DEBUG: Window delete-event triggered" if OPTIONS[:debug]
    save_window_geometry
    @status_window.hide
    true # Prevent destruction
  end

  # Save position and size when window is moved or resized
  @status_window.signal_connect("configure-event") do
    save_window_geometry
    false
  end

  # Create notebook only if it doesn't exist
  puts "DEBUG: Creating notebook" if OPTIONS[:debug]
  @notebook = Gtk::Notebook.new

  # Only create views if they don't exist
  if @ember_view.nil?
    puts "DEBUG: Creating ember view" if OPTIONS[:debug]
    @ember_view = create_log_view(@ember_output)
    @ember_label = create_status_label("Ember CLI", @ember_running)
    @notebook.append_page(@ember_view, @ember_label)
  end

  if @unicorn_view.nil?
    puts "DEBUG: Creating unicorn view" if OPTIONS[:debug]
    @unicorn_view = create_log_view(@unicorn_output)
    @unicorn_label = create_status_label("Unicorn", @unicorn_running)
    @notebook.append_page(@unicorn_view, @unicorn_label)
  end

  @status_window.add(@notebook)
  puts "DEBUG: Added notebook to status window" if OPTIONS[:debug]
  
  @status_window.show_all
  puts "DEBUG: Called show_all on status window" if OPTIONS[:debug]
  
  # Force an immediate update of the views
  GLib::Idle.add do
    puts "DEBUG: Forcing immediate update after window creation" if OPTIONS[:debug]
    update_all_views
    false
  end
end

#start_console_process(command) ⇒ Object



765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
# File 'lib/discourse_systray/systray.rb', line 765

def start_console_process(command)
  stdin, stdout, stderr, wait_thr = Open3.popen3(command)

  # Pipe stdout to console and add to buffer
  Thread.new do
    while line = stdout.gets
      buffer =
        command.include?("ember-cli") ? @ember_output : @unicorn_output
      print line
      buffer << line
      buffer.shift if buffer.size > BUFFER_SIZE
    end
  end

  # Pipe stderr to console and add to buffer
  Thread.new do
    while line = stderr.gets
      buffer =
        command.include?("ember-cli") ? @ember_output : @unicorn_output
      STDERR.print line
      buffer << line
      buffer.shift if buffer.size > BUFFER_SIZE
    end
  end

  {
    pid: wait_thr.pid,
    stdin: stdin,
    stdout: stdout,
    stderr: stderr,
    thread: wait_thr
  }
end

#start_discourseObject



210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/discourse_systray/systray.rb', line 210

def start_discourse
  @ember_output.clear
  @unicorn_output.clear

  Dir.chdir(@discourse_path) do
    @processes[:ember] = start_process("bin/ember-cli")
    @ember_running = true
    @processes[:unicorn] = start_process("bin/unicorn")
    @unicorn_running = true
    update_tab_labels if @notebook
  end
end

#start_process(command, console: false) ⇒ Object



284
285
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/discourse_systray/systray.rb', line 284

def start_process(command, console: false)
  puts "DEBUG: start_process called with command: #{command}" if OPTIONS[:debug]
  
  return start_console_process(command) if console
  
  begin
    stdin, stdout, stderr, wait_thr = Open3.popen3(command)
    puts "DEBUG: Process started with PID: #{wait_thr.pid}" if OPTIONS[:debug]
  rescue => e
    puts "DEBUG: Error starting process: #{e.message}" if OPTIONS[:debug]
    return nil
  end

  # Create a monitor thread that will detect if process dies
  monitor_thread =
    Thread.new do
      begin
        wait_thr.value # Wait for process to finish
        is_ember = command.include?("ember-cli")
        @ember_running = false if is_ember
        @unicorn_running = false unless is_ember
        GLib::Idle.add do
          update_tab_labels if @notebook
          false
        end
      rescue => e
        puts "DEBUG: Error in monitor thread: #{e.message}" if OPTIONS[:debug]
      end
    end

  # Clear the buffer before starting
  buffer = command.include?("ember-cli") ? @ember_output : @unicorn_output
  buffer.clear
  
  # Add a start message to the buffer
  timestamp = Time.now.strftime("%H:%M:%S")
  buffer << "#{timestamp} - Starting #{command}...\n"
  
  # Force immediate GUI update
  GLib::Idle.add do
    update_all_views
    false
  end

  # Monitor stdout
  Thread.new do
    begin
      while line = stdout.gets
        buffer = command.include?("ember-cli") ? @ember_output : @unicorn_output
        puts "[OUT] #{line}" if OPTIONS[:debug]
        
        # Add to buffer with size management
        buffer << line
        
        # Print buffer size for debugging
        if OPTIONS[:debug]
          puts "DEBUG: Added to buffer: #{line.inspect}"
          if buffer.size % 10 == 0
            puts "DEBUG: Buffer size now: #{buffer.size}"
          end
        end
        
        # Trim if needed
        if buffer.size > BUFFER_SIZE
          buffer.shift(buffer.size - BUFFER_SIZE)
        end
        
        # Force GUI update on main thread
        GLib::Idle.add do
          update_all_views
          false
        end
        
        # Also publish to pipe for --attach mode in background
        publish_to_pipe(line, process: command.include?("ember-cli") ? :ember : :unicorn, stream: :stdout)
      end
    rescue => e
      puts "DEBUG: Error in stdout thread: #{e.message}" if OPTIONS[:debug]
      puts e.backtrace.join("\n") if OPTIONS[:debug]
      
      # Add error to buffer for visibility
      buffer = command.include?("ember-cli") ? @ember_output : @unicorn_output
      error_msg = "ERROR: Exception in stdout thread: #{e.message}\n"
      buffer << error_msg
      
      # Force GUI update
      GLib::Idle.add do
        update_all_views
        false
      end
    end
  end

  # Monitor stderr
  Thread.new do
    begin
      while line = stderr.gets
        buffer = command.include?("ember-cli") ? @ember_output : @unicorn_output
        puts "[ERR] #{line}" if OPTIONS[:debug]
        
        # Format error line
        error_line = "E: #{line}"
        
        # Add to buffer with size management
        buffer << error_line
        
        # Print buffer size for debugging
        if OPTIONS[:debug]
          puts "DEBUG: Added to buffer: #{error_line.inspect}"
          if buffer.size % 10 == 0
            puts "DEBUG: Buffer size now: #{buffer.size}"
          end
        end
        
        # Trim if needed
        if buffer.size > BUFFER_SIZE
          buffer.shift(buffer.size - BUFFER_SIZE)
        end
        
        # Force GUI update on main thread
        GLib::Idle.add do
          update_all_views
          false
        end
        
        # Also publish to pipe for --attach mode in background
        publish_to_pipe(error_line, process: command.include?("ember-cli") ? :ember : :unicorn, stream: :stderr)
      end
    rescue => e
      puts "DEBUG: Error in stderr thread: #{e.message}" if OPTIONS[:debug]
      puts e.backtrace.join("\n") if OPTIONS[:debug]
      
      # Add error to buffer for visibility
      buffer = command.include?("ember-cli") ? @ember_output : @unicorn_output
      error_msg = "ERROR: Exception in stderr thread: #{e.message}\n"
      buffer << error_msg
      
      # Force GUI update
      GLib::Idle.add do
        update_all_views
        false
      end
    end
  end

  {
    pid: wait_thr.pid,
    stdin: stdin,
    stdout: stdout,
    stderr: stderr,
    thread: wait_thr,
    monitor: monitor_thread
  }
end

#stop_discourseObject



223
224
225
# File 'lib/discourse_systray/systray.rb', line 223

def stop_discourse
  cleanup
end

#trim_buffersObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/discourse_systray/systray.rb', line 129

def trim_buffers
  # Trim buffers if they exceed the buffer size
  if @ember_output.size > BUFFER_SIZE
    excess = @ember_output.size - BUFFER_SIZE
    @ember_output.shift(excess)
    @ember_line_count = [@ember_line_count - excess, 0].max
  end
  
  if @unicorn_output.size > BUFFER_SIZE
    excess = @unicorn_output.size - BUFFER_SIZE
    @unicorn_output.shift(excess)
    @unicorn_line_count = [@unicorn_line_count - excess, 0].max
  end
  
  true
end

#update_all_viewsObject



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/discourse_systray/systray.rb', line 542

def update_all_views
  puts "DEBUG: update_all_views called" if OPTIONS[:debug]
  
  # Basic validity checks
  return unless @status_window && !@status_window.destroyed?
  return unless @ember_view && @unicorn_view
  
  begin
    # Always update both views for now to ensure content is displayed
    if @ember_view && !@ember_view.destroyed? && @ember_view.child && !@ember_view.child.destroyed?
      update_log_view(@ember_view.child, @ember_output)
    end
    
    if @unicorn_view && !@unicorn_view.destroyed? && @unicorn_view.child && !@unicorn_view.child.destroyed?
      update_log_view(@unicorn_view.child, @unicorn_output)
    end
    
    # Process any pending GTK events
    while Gtk.events_pending?
      Gtk.main_iteration_do(false)
    end
  rescue => e
    puts "DEBUG: Error in update_all_views: #{e.message}" if OPTIONS[:debug]
    puts e.backtrace.join("\n") if OPTIONS[:debug]
  end
end

#update_log_view(text_view, buffer) ⇒ Object

We’re not using ANSI tags anymore since we’re stripping ANSI codes



635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
# File 'lib/discourse_systray/systray.rb', line 635

def update_log_view(text_view, buffer)
  puts "DEBUG: update_log_view called" if OPTIONS[:debug]
  
  # Basic validity checks
  return if text_view.nil? || text_view.destroyed?
  return if text_view.buffer.nil? || text_view.buffer.destroyed?

  # Debug buffer content
  if OPTIONS[:debug]
    puts "DEBUG: Buffer size: #{buffer.size}"
    if buffer.size > 0
      puts "DEBUG: First line: #{buffer.first.inspect}"
      puts "DEBUG: Last line: #{buffer.last.inspect}"
    end
  end

  # If buffer is empty, add a placeholder message
  if buffer.empty?
    buffer << "No log data available yet. Start Discourse to see logs.\n"
  end

  # Completely replace the buffer content with all lines
  begin
    # Make a local copy of the buffer to avoid race conditions
    buffer_copy = buffer.dup
    
    # Join all buffer lines into a single string
    all_content = buffer_copy.join("")
    
    # Strip ANSI codes
    clean_content = all_content.gsub(/\e\[[0-9;]*[mK]/, '')
    
    # Always update the content to ensure it's displayed
    text_view.buffer.text = clean_content
    
    puts "DEBUG: Updated buffer text (#{clean_content.length} chars)" if OPTIONS[:debug]
    
    # Scroll to bottom
    adj = text_view&.parent&.vadjustment
    if adj
      adj.value = adj.upper - adj.page_size
    end
    
    # Process any pending GTK events to ensure UI updates
    while Gtk.events_pending?
      Gtk.main_iteration_do(false)
    end
  rescue => e
    puts "DEBUG: Error updating text view: #{e.message}" if OPTIONS[:debug]
    puts e.backtrace.join("\n") if OPTIONS[:debug]
    
    # Try a fallback approach
    begin
      text_view.buffer.text = "Error displaying log. See console for details.\n#{e.message}"
    rescue => e2
      puts "DEBUG: Even fallback approach failed: #{e2.message}" if OPTIONS[:debug]
    end
  end
end

#update_tab_labelsObject



715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# File 'lib/discourse_systray/systray.rb', line 715

def update_tab_labels
  return unless @notebook && !@notebook.destroyed?
  return unless @ember_label && @unicorn_label
  return if @ember_label.destroyed? || @unicorn_label.destroyed?

  [@ember_label, @unicorn_label].each do |label|
    next unless label.children && label.children.length > 1
    next if label.children[1].destroyed?

    is_running = label == @ember_label ? @ember_running : @unicorn_running
    begin
      label.children[1].text = is_running ? "" : ""
      label.children[1].override_color(
        :normal,
        Gdk::RGBA.new(
          is_running ? 0.2 : 0.8,
          is_running ? 0.8 : 0.2,
          0.2,
          1
        )
      )
    rescue StandardError => e
      puts "Error updating label: #{e}" if OPTIONS[:debug]
    end
  end
end