Class: BTCT::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/btct/terminal.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Terminal



11
12
# File 'lib/btct/terminal.rb', line 11

def initialize(argv)
end

Instance Method Details

#runObject



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

def run
  sources = [
    BitstampAPI.new,
    BtceAPI.new,
    CampBxAPI.new,
    MtGoxAPI.new,
    TheRockAPI.new
  ]
  begin
    def onsig(sig)
      Curses.close_screen
      exit sig
    end
    for i in %w[HUP INT QUIT TERM]
      if trap(i, "SIG_IGN") != 0 then  # 0 for SIG_IGN
        trap(i) {|sig| onsig(sig) }
      end
    end
    Curses.init_screen
    Curses.nl
    Curses.noecho
    Curses.setpos(0,  0) ; Curses.addstr "BTC/USD"
    Curses.setpos(0, 21) ; Curses.addstr "Bid"
    Curses.setpos(0, 46) ; Curses.addstr "Ask"
    Curses.setpos(1, 14) ; Curses.addstr "Amount       Price"
    Curses.setpos(1, 39) ; Curses.addstr "Price      Amount"
    Curses.refresh
    while true
      bids = Array.new
      asks = Array.new
      sources.each do |source|
        bid, ask = source.top
        bids.push(bid)
        asks.push(ask)
      end
      bids.sort! { |x,y| y.price <=> x.price }
      asks.sort! { |x,y| x.price <=> y.price }
      row = 2
      bids.zip(asks).each do |bid, ask|
        text = "%-10s %10.6f %12.6f  %-12.6f %-10.6f %-10s" % [bid.exchange, bid.amount, bid.price, ask.price, ask.amount, ask.exchange]
        Curses.setpos(row, 0) ; Curses.addstr text
        row = row + 1
      end
      Curses.refresh
      sleep 5
    end
  ensure
    Curses.close_screen
  end
end