Module: Flammarion::Writeable

Defined in:
lib/cem/cflame/clickable_img.rb,
lib/cem/cflame/missing_html.rb,
lib/cem/cflame/progress.rb,
lib/cem/cflame/popen.rb,
lib/cem/cflame/p.rb

Overview

Paragraph which can be updated easily as a mixin for Flammarion

Defined Under Namespace

Classes: ClickableImage, Paragraph, Progress

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cem/cflame/missing_html.rb', line 18

def method_missing(m, *args, &block)
  
  if !isHTML5Element(m)
    puts "Warning: #{m} is not a valid HTML 5 Tag"
  end    
  
  attribs = args.last.is_a?(Hash) ? args.pop.map { |key, value| %| #{key.to_s}="#{value}"| }.join : ""
  
  if args.size == 0
    puts "<#{m} #{attribs}/>", raw: true
  else
    puts args.map { |value| "<#{m} #{attribs}>#{value}</#{m}>" }.join, raw: true
  end 
end

Instance Attribute Details

#altObject

Returns the value of attribute alt.



11
12
13
# File 'lib/cem/cflame/clickable_img.rb', line 11

def alt
  @alt
end

#heightObject

Returns the value of attribute height.



11
12
13
# File 'lib/cem/cflame/clickable_img.rb', line 11

def height
  @height
end

#titleObject

Returns the value of attribute title.



11
12
13
# File 'lib/cem/cflame/clickable_img.rb', line 11

def title
  @title
end

#urlObject

Returns the value of attribute url.



11
12
13
# File 'lib/cem/cflame/clickable_img.rb', line 11

def url
  @url
end

#widthObject

Returns the value of attribute width.



11
12
13
# File 'lib/cem/cflame/clickable_img.rb', line 11

def width
  @width
end

Instance Method Details

#img_raw(url, width, height, alt, title, options = {}, &block) ⇒ Object



54
55
56
# File 'lib/cem/cflame/clickable_img.rb', line 54

def img_raw(url, width, height, alt, title, options = {}, &block)
  return ClickableImage.new(url, width, height, alt, title, @engraving.make_id, self, block)    
end

#p(text) ⇒ Object



31
32
33
34
35
# File 'lib/cem/cflame/p.rb', line 31

def p(text)
  object = Paragraph.new(text, @engraving.make_id, self)
  puts object, raw:true
  object
end

#progress(name = nil) ⇒ Object



45
46
47
# File 'lib/cem/cflame/progress.rb', line 45

def progress(name = nil)
  return Progress.new(name || @engraving.make_id, self)    
end

#system(*cmd, pane_out: "out", pane_err: "out", pane_in: "in", status: true, close_btn: true, auto_clear: true, kill_sig: 'KILL') ⇒ Object

This method does not block, but returns after the program has been started.

Caution: By default most program run via Open3.popen3 assume they are started in non PTY mode and will buffer their output. To tell the program to be interactive use something like ‘script -qefc "<program>" /dev/null’ as command. See stackoverflow.com/questions/1401002/trick-an-application-into-thinking-its-stdout-is-a-terminal-not-a-pipe

*cmd

cmd string to pass to Open3.popen3

pane_out

Name of the pane that the STDOUT of the program should be print to. Defaults to “out”

pane_err

Name of the pane that the STDERR of the program should be print to. Defaults to “out”. Set to nil/false to prevent STDERR to be printed.

pane_in

Name of the pane that should be used to wait for input from the user. Defaults to “in”. Set to nil/false to prevent user input.

status

Should status updates (start, termination) be printed to Writeable::status. Defaults to true. Set to nil/false to disable.

close_btn

Should a close button be shown in the pane_in. Defaults to true. Set to nil/false to disable.

auto_clear

Should the panes be cleared prior to launching the program? Defaults to true. Set to nil/false to disable.

kill_sig

What to pass to Process.kill when close_btn is pressed but program does not react to STDIN being closed. Defaults to “KILL”. Could be “TERM”.



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cem/cflame/popen.rb', line 22

def system(*cmd, pane_out: "out", pane_err: "out", pane_in: "in", status: true, close_btn: true, auto_clear: true, kill_sig: 'KILL')

  # Clear all panes if auto_clear is set
  [pane_out, pane_err, pane_in].uniq.each { |p| p && subpane(p).clear } if auto_clear

  i, o, e, t = Open3.popen3(*cmd, pgroup: true)
  cmd = cmd.join

  status("Running '#{cmd}'".light_green) if status

  if pane_in
  
    subpane(pane_in).input("> ", autoclear:true, history:true) do |msg|
      i.puts msg['text']
    end 

    if close_btn
    
      subpane(pane_in).button("Close") do       
      
        subpane(pane_in).clear if auto_clear
        i.close
        
        value = begin
          if t.join(1)
            t.value
          else
            # TERM/KILL whole progress group
            Process.kill(kill_sig, -Process.getpgid(t.pid))
            if t.join(3)
              t.value
            else
              nil
            end
          end
        end
            
        if !value 
          status("Command '#{cmd}' did not terminate promptly.".light_red);
        end if status
      end
    end
  end

  Thread.new do
    begin
      while l = e.readpartial(4096)
        subpane(pane_err).print l.red
      end
    rescue EOFError
      # Do nothing
    end
  end if pane_err

  begin
    while l = o.readpartial(4096)
      subpane(pane_out).print l
    end
  rescue EOFError
    subpane(pane_in).clear if auto_clear
    
    if status && t.join(5)
      t.value.success? ? status("Command '#{cmd}' exited with success".light_green) : status("Command '#{cmd}' exited with error code: #{t.value}".light_red)
    end
  end  
end