Class: TermSlides::TTYRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/term-slides.rb

Instance Method Summary collapse

Instance Method Details

#center(text) ⇒ Object



26
27
28
29
# File 'lib/term-slides.rb', line 26

def center text
  width = HighLine.new.output_cols
  text.split("\n").map { |x| x.center(width) }.join("\n")
end

#pstree(pid = Process.pid) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/term-slides.rb', line 33

def pstree pid = Process.pid
  processes = []
  while pid != 0
    `/bin/ps #{::OS.mac? ? "-p" : "axww -q"} #{pid} -o ppid,command`.each_line do |line|
      next if line !~ /^\s*\d+/
      line.strip!
      result = line.split(/\s+/, 2)
      processes << result[1].split(" ").first.split("/").last
      pid = result[0].to_i
    end
  end
  processes
end

#render_code(code) ⇒ Object



20
21
22
# File 'lib/term-slides.rb', line 20

def render_code code
  puts ::Rouge.highlight(code.content, code.format, 'terminal256')
end

#render_diagram(diagram) ⇒ Object



67
68
69
# File 'lib/term-slides.rb', line 67

def render_diagram diagram
  render_image_file diagram.build
end

#render_image(image) ⇒ Object



64
65
66
# File 'lib/term-slides.rb', line 64

def render_image image
  render_image_file image.src
end

#render_image_file(path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/term-slides.rb', line 46

def render_image_file path
  commands = {
    "kitty" => [(::OS.mac? ? "/Applications/kitty.app/Contents/MacOS/" : "") + "kitty", "+kitten", "icat"],
    "terminology" => %w(tycat),
    "iTerm" => %w(imgcat)
  }
  ptree = pstree
  terminals = commands.keys.map { |terminal| ptree.include?(terminal) ? terminal : nil }.select { |x| !x.nil? }
  if terminals.size > 0
    command = commands[terminals.first]
  else
    command = %w(icat)
  end
  if find_executable(command.first)
    co = (command << path)
    system(*co)
  end
end

#render_slide(slide) ⇒ Object



70
71
72
73
74
# File 'lib/term-slides.rb', line 70

def render_slide slide
  puts center(slide.name).colorize(:light_blue).bold
  puts
  slide.content.each { |c| c.render }
end

#render_table(table) ⇒ Object



23
24
25
# File 'lib/term-slides.rb', line 23

def render_table table
  puts center(TTY::Table.new(table.headers, table.rows).render(:unicode))
end

#render_text(text) ⇒ Object



30
31
32
# File 'lib/term-slides.rb', line 30

def render_text text
  puts center(text.text)
end