Class: Shoes::Console

Inherits:
Object
  • Object
show all
Defined in:
shoes-core/lib/shoes/console.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConsole

Returns a new instance of Console.



7
8
9
10
11
12
# File 'shoes-core/lib/shoes/console.rb', line 7

def initialize
  @queued_messages = Queue.new

  @messages = []
  @message_stacks = []
end

Instance Attribute Details

#message_stacksObject (readonly)

Returns the value of attribute message_stacks.



5
6
7
# File 'shoes-core/lib/shoes/console.rb', line 5

def message_stacks
  @message_stacks
end

#messagesObject (readonly)

Returns the value of attribute messages.



5
6
7
# File 'shoes-core/lib/shoes/console.rb', line 5

def messages
  @messages
end

Instance Method Details

#add_message(type, message) ⇒ Object



91
92
93
94
# File 'shoes-core/lib/shoes/console.rb', line 91

def add_message(type, message)
  @messages << [type, message]
  add_message_stack(type, message, @messages.count - 1)
end

#add_message_stack(type, message, index = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'shoes-core/lib/shoes/console.rb', line 96

def add_message_stack(type, message, index = nil)
  return unless defined?(@app) && @app.open?

  @app.instance_exec do
    append do
      @console.message_stacks << stack do
        background "#f1f5e1" if index.even?
        background rgb(220, 220, 220) if index.odd?
        para type, stroke: blue
        flow do
          stack margin: 4 do
            s = message.to_s
            para s, margin: 4, margin_top: 0
          end
        end
      end
    end
  end
end

#clearObject



136
137
138
139
140
141
# File 'shoes-core/lib/shoes/console.rb', line 136

def clear
  messages.clear

  message_stacks.each(&:remove)
  message_stacks.clear
end

#copyObject



122
123
124
# File 'shoes-core/lib/shoes/console.rb', line 122

def copy
  @app.clipboard = formatted_messages if messages.any?
end

#create_appObject



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 'shoes-core/lib/shoes/console.rb', line 26

def create_app
  # Capture ourself for access inside our app block
  console = self

  @app = Shoes.app do
    @console = console

    stack do
      flow do
        background black
        stack do
          tagline "Shoes Console", stroke: white
        end

        button "Copy", margin: 6, width: 80, height: 40, right: 170 do
          @console.copy
        end

        button "Save", margin: 6, width: 80, height: 40, right: 90 do
          @console.save
        end

        button "Clear", margin: 6, width: 80, height: 40, right: 10 do
          @console.clear
        end
      end
    end

    every 0.1 do
      # Periodically update UI with new log lines
      @console.drain_queued_messages
    end
  end

  # Synchronously drain queue first showing console
  drain_queued_messages
end

#debug(message) ⇒ Object



75
76
77
# File 'shoes-core/lib/shoes/console.rb', line 75

def debug(message)
  @queued_messages << [:debug, message]
end

#drain_queued_messagesObject



64
65
66
67
68
69
70
71
72
73
# File 'shoes-core/lib/shoes/console.rb', line 64

def drain_queued_messages
  10.times do
    # Non-blocking = true, so will raise ThreadError once we're done
    type, message = @queued_messages.pop(true)
    add_message(type, message)
  end
rescue ThreadError # rubocop:disable Lint/HandleExceptions
  # Queue's drained, carry on!
  # Disabled cop because this is Queue#pop's non-blocking API
end

#error(message) ⇒ Object



87
88
89
# File 'shoes-core/lib/shoes/console.rb', line 87

def error(message)
  @queued_messages << [:error, message]
end

#formatted_messagesObject



116
117
118
119
120
# File 'shoes-core/lib/shoes/console.rb', line 116

def formatted_messages
  @messages.inject("") do |memo, (type, message)|
    "#{memo}#{type.to_s.capitalize}\n #{message}\n\n"
  end
end

#info(message) ⇒ Object



79
80
81
# File 'shoes-core/lib/shoes/console.rb', line 79

def info(message)
  @queued_messages << [:info, message]
end

#saveObject



126
127
128
129
130
131
132
133
134
# File 'shoes-core/lib/shoes/console.rb', line 126

def save
  filename = @app.ask_save_file

  return unless filename

  File.open(filename, "w") do |f|
    f.write(formatted_messages)
  end
end

#showObject



14
15
16
17
18
19
20
# File 'shoes-core/lib/shoes/console.rb', line 14

def show
  if showing?
    @app.focus
  else
    create_app
  end
end

#showing?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'shoes-core/lib/shoes/console.rb', line 22

def showing?
  @app&.open?
end

#warn(message) ⇒ Object



83
84
85
# File 'shoes-core/lib/shoes/console.rb', line 83

def warn(message)
  @queued_messages << [:warn, message]
end