Module: Flammarion::Writeable

Included in:
Engraving, Pane
Defined in:
lib/flammarion/writeable.rb

Defined Under Namespace

Classes: DeferredValue, Spectrum

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#front_endObject (readonly)

Returns the value of attribute front_end.



3
4
5
# File 'lib/flammarion/writeable.rb', line 3

def front_end
  @front_end
end

Instance Method Details

#break(options = {}) ⇒ Object



127
128
129
# File 'lib/flammarion/writeable.rb', line 127

def break(options = {})
  send_json({action:'break'}.merge(options))
end

#button(label, options = {}, &block) ⇒ Object



84
85
86
87
88
# File 'lib/flammarion/writeable.rb', line 84

def button(label, options = {}, &block)
  id = @front_end.make_id
  send_json({action:'button', label:label, id:id}.merge(options))
  @front_end.callbacks[id] = block
end

#button_box(name) ⇒ Object



187
188
189
190
# File 'lib/flammarion/writeable.rb', line 187

def button_box(name)
  send_json({action:'buttonbox', name:name})
  return Pane.new(@front_end, name)
end


96
97
98
99
100
# File 'lib/flammarion/writeable.rb', line 96

def callback_link(label, options = {}, &block)
  id = @front_end.make_id
  @front_end.callbacks[id] = block
  %|<a href="#" onClick="$ws.send({id:'#{id}', action:'callback', source:'link'})">#{label}</a>|
end

#checkbox(label, options = {}, &block) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/flammarion/writeable.rb', line 114

def checkbox(label, options = {}, &block)
  id = @front_end.make_id
  send_json({action:'checkbox', label:label, id:id}.merge(options))
  if block_given?
    @front_end.callbacks[id] = block
  else
    d = DeferredValue.new
    d.__setobj__(options[:value] || options['value'])
    @front_end.callbacks[id] = Proc.new {|v| d.__setobj__(v["checked"])}
    return d
  end
end

#clearObject



62
63
64
65
# File 'lib/flammarion/writeable.rb', line 62

def clear
  send_json({action:'clear'})
  return nil
end

#closeObject



67
68
69
# File 'lib/flammarion/writeable.rb', line 67

def close
  send_json({action:'closepane'})
end

#embedded_button(label, options = {}, &block) ⇒ Object



90
91
92
93
94
# File 'lib/flammarion/writeable.rb', line 90

def embedded_button(label, options = {}, &block)
  id = @front_end.make_id
  @front_end.callbacks[id] = block
  %|<a class="floating-button" href="#" onClick="$ws.send({id:'#{id}', action:'callback', source:'embedded_button'})">#{label}</a>|
end

#gets(prompt = "", options = {}) ⇒ Object



200
201
202
203
204
205
# File 'lib/flammarion/writeable.rb', line 200

def gets(prompt = "", options = {})
  str = nil
  input(prompt, {once:true, focus:true}.merge(options)) {|msg| str = msg["text"]}
  sleep 0.1 while str.nil?
  return str
end

#hideObject



164
165
166
# File 'lib/flammarion/writeable.rb', line 164

def hide
  send_json({action:'hidepane'})
end

#highlight(text, options = {}) ⇒ Object



77
78
79
80
81
82
# File 'lib/flammarion/writeable.rb', line 77

def highlight(text, options = {})
  output = text
  output = JSON.pretty_generate(text) if text.is_a? Hash or text.is_a? Array
  send_json({action:'highlight', text:output}.merge(options))
  nil
end

#html(data) ⇒ Object



131
132
133
# File 'lib/flammarion/writeable.rb', line 131

def html(data)
  send_json({action:'replace', text:data, raw:true})
end

#input(label, options = {}, &block) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/flammarion/writeable.rb', line 102

def input(label, options = {}, &block)
  id = @front_end.make_id
  send_json({action:'input', label:label, id:id}.merge(options))
  if block_given?
    @front_end.callbacks[id] = block
  else
    d = DeferredValue.new
    @front_end.callbacks[id] = Proc.new {|v| d.__setobj__ v["text"] }
    return d
  end
end

#live_reload_template(file) ⇒ Object



149
150
151
# File 'lib/flammarion/writeable.rb', line 149

def live_reload_template(file)
  FileWatcher.new(file).watch {|file| template(file) }
end

#map(*args) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/flammarion/writeable.rb', line 207

def map(*args)
  case (args.size)
  when 1
    if args[0].respond_to? :keys then
      options = args[0]
    else
      options = {address:args[0].to_s}
    end
  when 2
    if args[1].respond_to? :keys then
      options = {address:args[0]}.merge(args[1])
    else
      options = {latitude:args[0], longitude:args[1]}
    end
  when 3
    options = {latitude:args[0], longitude:args[1]}.merge(args[2])
  else
    raise ArgumentError.new("Expected 1..3 arguments")
  end
  send_json({action:'map'}.merge(options))
end

#markdown(text, options = {}) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/flammarion/writeable.rb', line 153

def markdown(text, options = {})
  markdown_html = Redcarpet::Markdown.new(Redcarpet::Render::HTML, {
    tables: true,
    fenced_code_blocks: true,
    autolink: true,
    strikethrough: true,
    superscript: true,
  }.merge(options[:markdown_extensions] || {})).render(text)
  send_json({action:'markdown', text: markdown_html}.merge(options))
end

#orientation=(orientation) ⇒ Object

Raises:

  • (ArgumentError)


182
183
184
185
# File 'lib/flammarion/writeable.rb', line 182

def orientation=(orientation)
  raise ArgumentError.new("Orientation must be :horizontal or :vertical") unless [:horizontal, :vertical].include?(orientation)
  send_json({action:'reorient', orientation:orientation})
end

#pane(name) ⇒ Object



177
178
179
180
# File 'lib/flammarion/writeable.rb', line 177

def pane(name)
  send_json({action:'addpane', name:name})
  return Pane.new(@front_end, name)
end

#plot(values, options = {}) ⇒ Object



71
72
73
74
75
# File 'lib/flammarion/writeable.rb', line 71

def plot(values, options = {})
  id = @front_end.make_id
  send_json({action:'plot', data:values, id:id}.merge(options))
  return Spectrum.new(id, @pane_name, @front_end)
end

#puts(str = "", options = {}) ⇒ Object



51
52
53
54
55
# File 'lib/flammarion/writeable.rb', line 51

def puts(str = "", options = {})
  send str, options
  send "\n"
  return nil
end

#replace(str, options = {}) ⇒ Object



57
58
59
60
# File 'lib/flammarion/writeable.rb', line 57

def replace(str, options = {})
  send_json({action:'replace', text:str}.merge(options))
  return nil
end

#script(coffee, options = {}) ⇒ Object



135
136
137
138
# File 'lib/flammarion/writeable.rb', line 135

def script(coffee, options = {})
  data = options.fetch(:coffee, true) ? CoffeeScript.compile(coffee) : coffee
  send_json({action:'script', data:data}.merge(options))
end

#send(str, options = {}) ⇒ Object Also known as: print



46
47
48
# File 'lib/flammarion/writeable.rb', line 46

def send(str, options = {})
  @front_end.send_json({action:'append', text:str, target:@pane_name}.merge(options))
end

#send_json(hash) ⇒ Object



42
43
44
# File 'lib/flammarion/writeable.rb', line 42

def send_json(hash)
  @front_end.send_json({target: @pane_name}.merge(hash))
end

#showObject



168
169
170
# File 'lib/flammarion/writeable.rb', line 168

def show
  send_json({action:'showpane'})
end

#status(str, position = :right) ⇒ Object



192
193
194
# File 'lib/flammarion/writeable.rb', line 192

def status(str, position = :right)
  @front_end.send_json({action:'status', text: str, position:position})
end

#style(attribute, value) ⇒ Object



140
141
142
# File 'lib/flammarion/writeable.rb', line 140

def style(attribute, value)
  send_json({action: 'style', attribute: attribute, value: value})
end

#subpane(name) ⇒ Object



172
173
174
175
# File 'lib/flammarion/writeable.rb', line 172

def subpane(name)
  send_json({action:'subpane', name:name})
  return Pane.new(@front_end, name)
end

#table(rows, options = {}) ⇒ Object



196
197
198
# File 'lib/flammarion/writeable.rb', line 196

def table(rows, options = {})
  send_json({action:'table', rows: rows}.merge(options))
end

#template(file) ⇒ Object



144
145
146
147
# File 'lib/flammarion/writeable.rb', line 144

def template(file)
  data = Slim::Template.new(file).render
  send_json({action:'replace', text:data, raw:true})
end