Module: VER::Methods::Preview

Defined in:
lib/ver/methods/preview.rb

Defined Under Namespace

Classes: Frame

Class Method Summary collapse

Class Method Details

.open_rxvt(text, command) ⇒ Object

Open a new urxvt term and manage it inside the layout of VER. Please let me know if you notice weird behaviour around the focus



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
# File 'lib/ver/methods/preview.rb', line 40

def open_rxvt(text, command)
  layout = text.layout

  # No Tk::Tile::Frame as it doesn't support container
  frame = Frame.new(layout, container: true)
  layout.add_buffer(frame)

  frame.bind '<MapRequest>' do |event|
    # Quite the weird hack, but once the MapRequest was handled, we can
    # focus the frame again to actually focus the term.
    # The delay was chosen by trying it on my notebook, which may or may
    # not be enough for everybody, so i added a bit of margin for error.
    # Usually humans don't notice the delay, so we could increase it a bit
    # if need arises.
    Tk::After.ms 100 do
      text.focus
      frame.focus
    end
  end

  frame.bind '<Map>' do |event|
    cmd = "urxvt -embed #{frame.winfo_id} -e $SHELL -c '%s' &" % [command]
    # puts cmd
    `#{cmd}`
  end

  frame.bind '<Destroy>' do |event|
    Tk::After.idle do
      layout.close_buffer(frame)
      text.focus(:force) # need to use force, the term was outside tk
    end
  end
end

.preview(text) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/ver/methods/preview.rb', line 16

def preview(text)
  return unless syntax = text.syntax

  case syntax.name
  when 'Ruby'; preview_ruby(text)
  end
end

.preview_ruby(text) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/ver/methods/preview.rb', line 24

def preview_ruby(text)
  save(text)
  open_rxvt(text, <<-SHELL)
ruby #{text.filename.shellescape}
echo "\nPreview finished, press <Return> to return to VER"
read
exit
  SHELL
end

.save(text) ⇒ Object



34
35
36
# File 'lib/ver/methods/preview.rb', line 34

def save(text)
  Save.file_save(text)
end