Module: Cryptum::UI

Defined in:
lib/cryptum/ui.rb,
lib/cryptum/ui/exit.rb,
lib/cryptum/ui/order.rb,
lib/cryptum/ui/matrix.rb,
lib/cryptum/ui/ticker.rb,
lib/cryptum/ui/command.rb,
lib/cryptum/ui/portfolio.rb,
lib/cryptum/ui/order/plan.rb,
lib/cryptum/ui/order/timer.rb,
lib/cryptum/ui/market_trend.rb,
lib/cryptum/ui/order/execute.rb,
lib/cryptum/ui/signal_engine.rb,
lib/cryptum/ui/key_press_event.rb,
lib/cryptum/ui/terminal_window.rb,
lib/cryptum/ui/order/plan_details.rb,
lib/cryptum/ui/order/execute_details.rb

Overview

Cryptum::UI Module used for Presenting the Cryptum Curses Interface

Defined Under Namespace

Modules: Command, Exit, MarketTrend, Matrix, Order, Portfolio, SignalEngine, Ticker Classes: KeyPressEvent, TerminalWindow

Class Method Summary collapse

Class Method Details

.col_center(opts = {}) ⇒ Object

Jump to First Column



204
205
206
207
208
209
210
211
212
# File 'lib/cryptum/ui.rb', line 204

public_class_method def self.col_center(opts = {})
  str = opts[:str]

  str_divided_by_two = str.length / 2
  (Curses.cols / 2) - str_divided_by_two
rescue Interrupt, StandardError => e
  # Produce a Stacktrace for anything else
  Cryptum::Log.append(level: :error, msg: e, which_self: self)
end

.col_firstObject



214
215
216
217
218
219
# File 'lib/cryptum/ui.rb', line 214

public_class_method def self.col_first
  0
rescue Interrupt, StandardError => e
  # Produce a Stacktrace for anything else
  Cryptum::Log.append(level: :error, msg: e, which_self: self)
end

.col_fourthObject

Jump to Fourth Column



238
239
240
241
242
243
# File 'lib/cryptum/ui.rb', line 238

public_class_method def self.col_fourth
  ((Curses.cols / 4) * 3) - 3
rescue Interrupt, StandardError => e
  # Produce a Stacktrace for anything else
  Cryptum::Log.append(level: :error, msg: e, which_self: self)
end

.col_secondObject

Jump to Second Column



222
223
224
225
226
227
# File 'lib/cryptum/ui.rb', line 222

public_class_method def self.col_second
  (Curses.cols / 8) + 5
rescue Interrupt, StandardError => e
  # Produce a Stacktrace for anything else
  Cryptum::Log.append(level: :error, msg: e, which_self: self)
end

.col_thirdObject

Jump to Third Column



230
231
232
233
234
235
# File 'lib/cryptum/ui.rb', line 230

public_class_method def self.col_third
  ((Curses.cols / 8) * 3) + 2
rescue Interrupt, StandardError => e
  # Produce a Stacktrace for anything else
  Cryptum::Log.append(level: :error, msg: e, which_self: self)
end

.colorize(opts = {}) ⇒ Object

Jump to First Column



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
# File 'lib/cryptum/ui.rb', line 108

public_class_method def self.colorize(opts = {})
  ui_win = opts[:ui_win]
  color = opts[:color].to_s.to_sym
  red = opts[:red].to_i
  green = opts[:green].to_i
  blue = opts[:blue].to_i

  style = opts[:style].to_s.to_sym
  style = :normal if opts[:style].nil?

  bg = opts[:bg].to_i
  bg = -1 if opts[:bg].nil?

  string = opts[:string]

  case color
  when :black
    # It's more Gray than Black
    # color_id = 0
    # color_fg = Curses::COLOR_BLACK
    color_id = 256
    Curses.init_color(color_id, 0, 0, 500)
    color_fg = color_id
    color_bg = bg
  when :red
    color_id = 1
    color_fg = Curses::COLOR_RED
    color_bg = bg
  when :green
    color_id = 2
    color_fg = Curses::COLOR_GREEN
    color_bg = bg
  when :yellow
    color_id = 3
    color_fg = Curses::COLOR_YELLOW
    color_bg = bg
  when :blue
    color_id = 4
    color_fg = Curses::COLOR_BLUE
    color_bg = bg
  when :magenta
    color_id = 5
    color_fg = Curses::COLOR_MAGENTA
    color_bg = bg
  when :cyan
    color_id = 6
    color_fg = Curses::COLOR_CYAN
    color_bg = bg
  when :white
    color_id = 7
    color_fg = Curses::COLOR_WHITE
    color_bg = bg
  when :rainbow
    color_id = 254
    red = Random.rand(0..1000)
    green = Random.rand(0..1000)
    blue = Random.rand(0..1000)
    Curses.init_color(color_id, red, green, blue)
    color_fg = color_id
    color_bg = bg
  when :custom
    color_id = 255
    Curses.init_color(color_id, red, green, blue)
    color_fg = color_id
    color_bg = bg
  else
    e = "Color Not Implemented for this Method: #{color}"
    Cryptum::Log.append(level: :error, msg: e, which_self: self)
  end

  case style
  when :blink
    font = Curses::A_BLINK
  when :bold
    font = Curses::A_BOLD
  when :highlight
    font = Curses::A_STANDOUT
  when :normal
    font = Curses::A_NORMAL
  when :reverse
    font = Curses::A_REVERSE
  else
    e = "Font Style Not Implemented for this Method: #{style}"
    Cryptum::Log.append(level: :error, msg: e, which_self: self)
  end

  Curses.init_pair(color_id, color_fg, color_bg)
  ui_win.attron(Curses.color_pair(color_id) | font) do
    ui_win.addstr(string)
  end
rescue Interrupt, StandardError => e
  # Produce a Stacktrace for anything else
  Cryptum::Log.append(level: :error, msg: e, which_self: self)
end

.detect_key_press_in_ui(opts = {}) ⇒ Object



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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/cryptum/ui.rb', line 245

public_class_method def self.detect_key_press_in_ui(opts = {})
  key_press_event = opts[:key_press_event]
  ui_win = opts[:ui_win]

  key_press = ui_win.get_char

  # Useful for detecting and logging actual key presses to file
  # if key_press
  #   debug_keys = "#{key_press.class} => key_press.inspect\n\n\n"
  #   Cryptum::Log.append(level: :debug, msg: debug_keys, which_self: self)
  # end

  case key_press
  when 'C'
    key_press_event.key_c = true
  when 'G'
    key_press_event.key_g = true
  when 'r'
    key_press_event.key_r = true
  when 'u'
    key_press_event.key_u = true
  when 'w'
    key_press_event.key_w = true
  when 'x'
    key_press_event.key_x = true
  when "\e"
    key_press_event.key_esc = true
  when "\n"
    key_press_event.key_enter = true
  when "\t"
    key_press_event.key_tab = true
  end

  # TODO: Fix this - sometimes this results in unintended behavior.
  # What a hack to detect special keys (-.-)
  if key_press_event.key_esc
    key_press_event.key_ansi = true if key_press == '['
    if key_press_event.key_ansi
      key_press_event.key_up_arrow = true if key_press == 'A'
      key_press_event.key_down_arrow = true if key_press == 'B'
      # NOTE: the ~ at the end of these keys are ignored
      # which may result in other keys working in place of them
      # e.g. home is \e[1~ but we know it's home as soon as
      # \e[1 is detected.
      key_press_event.key_page_up = true if key_press == '5'
      key_press_event.key_page_down = true if key_press == '6'
      key_press_event.key_home = true if key_press == '1'
      key_press_event.key_end = true if key_press == '4'
    end
  end

  key_press_event
rescue Interrupt, StandardError => e
  # Produce a Stacktrace for anything else
  Cryptum::Log.append(level: :error, msg: e, which_self: self)
end

.helpObject

Display a List of Every UI Module



304
305
306
# File 'lib/cryptum/ui.rb', line 304

public_class_method def self.help
  puts constants.sort
end

.init(opts = {}) ⇒ Object

Initialize the UI



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
# File 'lib/cryptum/ui.rb', line 19

public_class_method def self.init(opts = {})
  event_history = opts[:event_history]
  msg = ''
  if event_history.respond_to?('order_book')
    msg = "session: #{event_history.order_book[:path].split('/').first}, "
    msg += "symbol: #{event_history.order_book[:symbol]}"
  end
  msg += ' => Session Started.'
  Cryptum::Log.append(level: :info, msg: msg, which_self: self)

  # Initialize curses Screen
  Curses.init_screen

  # Ensure the cursor is invisible
  Curses.curs_set(0)

  # Do not echo keystrokes back to the UI
  Curses.noecho

  # Used to immediately evaluate characters submitted
  # without pressing ENTER (e.g. b, r, & w key events)
  Curses.crmode
  # Curses.nocrmode
  # Curses.raw # NO
  # Curses.noraw
  # Disable Line Buffering
  Curses.ESCDELAY = 0

  # Start Color!
  Curses.start_color

  # This is important to ensure -1 maintains
  # the original background color in the terminal
  # when defining color pairs
  Curses.use_default_colors

  # This object is used to pass all of the UI sections
  # around to various Cryptum modules
  Cryptum::UI::TerminalWindow.new
rescue Interrupt, StandardError => e
  # Produce a Stacktrace for anything else
  Cryptum::Log.append(level: :error, msg: e, which_self: self)
end

.line(opts = {}) ⇒ Object

Draw a Box Around a Window



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

public_class_method def self.line(opts = {})
  ui_win = opts[:ui_win]
  out_line_no = opts[:out_line_no].to_i
  color = opts[:color]
  color ||= :white

  style = :normal
  style = :bold unless color == :white

  ui_win.setpos(out_line_no, 0)
  colorize(
    ui_win: ui_win,
    color: color,
    style: style,
    string: "\u2500" * Curses.cols
  )
rescue Interrupt, StandardError => e
  # Produce a Stacktrace for anything else
  Cryptum::Log.append(level: :error, msg: e, which_self: self)
end

.window(opts = {}) ⇒ Object

Create New Curses Window



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

public_class_method def self.window(opts = {})
  height = opts[:height].to_i
  width = opts[:width].to_i
  top = opts[:top].to_i
  left = opts[:left].to_i

  window = Curses::Window.new(
    height,
    width,
    top,
    left
  )
  window.nodelay = true

  window
rescue Interrupt, StandardError => e
  # Produce a Stacktrace for anything else
  Curses.close_screen
  Cryptum::Log.append(level: :error, msg: e, which_self: self)
end