1
2
3
4
5
6
7
8
9
10
11
12
13
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env ruby

require 'osx/cocoa'
include OSX

class AppDelegate < NSObject

  def applicationDidFinishLaunching(aNotification)

    url = ARGV[0].dup
    url.gsub!(/http:\/\//, "http://slactoid:easy4kastner@")
    
    begin
      
      Thread.start { OSX::NSRunLoop.currentRunLoop.run }
    
      cmd = "cd ~ && axel -a #{url}"
      last_number = 0
      IO.popen(cmd) do |pipe|
        pipe.each("\r") do |line|
          number = line[/[^\d]([\d]+)%/].to_i
          speed = line[/[^\d][\d\.]+KB\/s/].to_f
          # puts number + " / " + speed
          if last_number != number
            last_number = number
            advance_slider(number)
            write_speed(speed)
          end
        end
      end
    rescue
    end

  end
  

  def moveForward(sender)
    # advanceSlider(55)
    puts sender.setTitle("bob")
    # puts app.mainWindow
  end
  
  def advance_slider(value)
    # $pro.startAnimation($pro)
    $pro.setDoubleValue(value.to_f)
    $pro.stopAnimation($pro)
  end
  
  def write_speed(speed)
    $txt.setStringValue(speed)
  end
end

if $0 == __FILE__ then
  OSX.ruby_thread_switcher_start(0.001, 0.1)
  
  $stderr.print "just wait..." ; $stderr.flush
  app = NSApplication.sharedApplication()

  app.setDelegate(AppDelegate.alloc.init)

  frame = [400.0, 300.0, 452.0, 107.0]
  win = NSWindow.alloc.initWithContentRect_styleMask_backing_defer(frame, 15, 2, 0)
  win.setTitle 'Axel Progress'
  win.setLevel(3)     # floating window

  $pro = NSProgressIndicator.alloc.initWithFrame [18.0, 48.0, 416.0, 20.0]
  $pro.setIndeterminate(0)
  $pro.setDoubleValue(0)
  win.contentView.addSubview($pro)
  # hel.setTarget( app.delegate )

  $txt = NSTextField.alloc.initWithFrame [316.0, 10.0, 114.0, 22.0]
  # $txt.setTitle("Bob")
  $txt.setEditable(0)
  $txt.setSelectable(0)
  $txt.setBezeled(0)
  $txt.setDrawsBackground(0)
  # $txt.setStringValue("Bob")
  $txt.setAlignment(NSRightTextAlignment)
  win.contentView.addSubview($txt)
  # hel = NSButton.alloc.initWithFrame [356.0, 12.0, 82.0, 32.0]
  # win.contentView.addSubview(hel)
  # hel.setBezelStyle(1)
  # hel.setTitle( 'Advance!' )
  # hel.setTarget( app.delegate )
  # hel.setAction( "moveForward:" )
  
  win.display()
  win.orderFrontRegardless()    ## but this one does

  app.run()
  
  exit 0
end