Class: Meshchat::Ui::Display::ReadlineDisplay

Inherits:
Base
  • Object
show all
Defined in:
lib/meshchat/ui/display/readline_display.rb

Instance Method Summary collapse

Methods inherited from Base

#log

Instance Method Details

#add_line(line) ⇒ Object

TODO: find a more elegant way to handle color



32
33
34
# File 'lib/meshchat/ui/display/readline_display.rb', line 32

def add_line(line)
  print_non_destructively(line)
end

#alert(msg) ⇒ Object



54
55
56
# File 'lib/meshchat/ui/display/readline_display.rb', line 54

def alert(msg)
  print_non_destructively(msg.colorize(:red))
end

#chat(msg) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/meshchat/ui/display/readline_display.rb', line 72

def chat(msg)
  message_parts_for(msg) do |time, name, message, _|
    colored_time = (time.to_s + ' ').colorize(:light_magenta)
    colored_name = (name + ' ').colorize(:cyan)

    print_non_destructively(colored_time + colored_name + message)
  end
end

#emote(msg) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/meshchat/ui/display/readline_display.rb', line 62

def emote(msg)
  message_parts_for(msg) do |time, name, message, _|
    colored_time = (time.to_s + ' ').colorize(:magenta)
    colored_name = (name + ' ').colorize(:light_black)
    colored_message = message.colorize(:light_black)

    print_non_destructively(colored_time + colored_name + colored_message)
  end
end

#info(msg) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/meshchat/ui/display/readline_display.rb', line 36

def info(msg)
  if msg.is_a?(Hash)
    message_parts_for(msg) do |time, name, message, _|
      colored_time = (time.to_s + ' ').colorize(:magenta)
      colored_name = (name + ' ').colorize(:light_black)
      colored_message = message.colorize(:light_black)

      print_non_destructively(colored_time + colored_name + colored_message)
    end
  else
    print_non_destructively(msg.colorize(:light_black))
  end
end

#message_parts_for(msg) {|time, name, message, to| ... } ⇒ Object

Yields:

  • (time, name, message, to)


93
94
95
96
97
98
99
100
101
102
# File 'lib/meshchat/ui/display/readline_display.rb', line 93

def message_parts_for(msg)
  return yield(nil, nil, msg) if msg.is_a?(String)

  time = msg[:time].strftime('%H:%M:%S')
  name = msg[:from].to_s
  message = msg[:message]
  to = msg[:to]

  yield(time, name, message, to)
end


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/meshchat/ui/display/readline_display.rb', line 15

def print_non_destructively(text)
  # Example of writing output while line is being edited.
  #
  # See also http://stackoverflow.com/questions/1512028/gnu-readline-how-do-clear-the-input-line
  if buffer = Readline.line_buffer
    print "\b \b" * buffer.size
    print "\r"
  end

  begin
    puts text + "\n"
  ensure
    Readline.forced_update_display
  end
end

#startObject



6
7
8
9
10
11
12
13
# File 'lib/meshchat/ui/display/readline_display.rb', line 6

def start
  puts "\n"
  alert 'Welcome to Spiced Rumby!'
  puts "\n"
  puts "\n"
  # Start up Ripl, but it will not receive any user input
  # Ripl.start
end

#success(msg) ⇒ Object



58
59
60
# File 'lib/meshchat/ui/display/readline_display.rb', line 58

def success(msg)
  print_non_destructively(msg.colorize(:green))
end

#warning(msg) ⇒ Object



50
51
52
# File 'lib/meshchat/ui/display/readline_display.rb', line 50

def warning(msg)
  print_non_destructively(msg.colorize(:yellow))
end

#whisper(msg) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/meshchat/ui/display/readline_display.rb', line 81

def whisper(msg)
  message_parts_for(msg) do |time, name, message, to|
    colored_time = (time.to_s + ' ').colorize(:magenta).bold
    colored_name = name.colorize(:light_black).bold
    colored_message = message.colorize(:blue).bold
    colored_to = to.colorize(:blue).bold

    names = "#{colored_name}->#{to} "
    print_non_destructively(colored_time + names + colored_message)
  end
end