Class: Live
- Inherits:
-
Object
- Object
- Live
- Defined in:
- lib/motion/project/live.rb
Instance Method Summary collapse
- #adieu(string) ⇒ Object
- #colorize_console_output(string) ⇒ Object
- #console_attach ⇒ Object
- #console_put(input) ⇒ Object
-
#console_self ⇒ Object
Interactions with the ruby motion REPL.
-
#create_scratch_pad_if_needed ⇒ Object
Helpers.
- #dirs ⇒ Object
- #find_changed_lines ⇒ Object
-
#initialize ⇒ Live
constructor
A new instance of Live.
- #run ⇒ Object
- #scratch_pad ⇒ Object
- #scratchpad_file ⇒ Object
- #send_changes ⇒ Object
- #start_watching ⇒ Object
- #wait_console ⇒ Object
- #wait_console_result ⇒ Object
-
#welcome(string) ⇒ Object
Custom output.
Constructor Details
#initialize ⇒ Live
Returns a new instance of Live.
32 33 34 |
# File 'lib/motion/project/live.rb', line 32 def initialize @buffered_output = '' end |
Instance Method Details
#adieu(string) ⇒ Object
156 157 158 159 160 |
# File 'lib/motion/project/live.rb', line 156 def adieu(string) puts puts string.yellow puts end |
#colorize_console_output(string) ⇒ Object
142 143 144 145 |
# File 'lib/motion/project/live.rb', line 142 def colorize_console_output(string) color = string.include?('Error') ? :yellow : :green string.gsub(/\n*(=> .*)/,"\n" + '\1'.send(color)) end |
#console_attach ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/motion/project/live.rb', line 129 def console_attach Thread.new do loop do buffer = '' @stdout.readpartial(4096, buffer) @buffered_output << buffer print colorize_console_output(buffer) STDOUT.flush end end wait_console end |
#console_put(input) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/motion/project/live.rb', line 110 def console_put(input) wait_console @stdin.puts input @buffered_output = '' wait_console end |
#console_self ⇒ Object
Interactions with the ruby motion REPL
105 106 107 108 |
# File 'lib/motion/project/live.rb', line 105 def console_self wait_console @buffered_output[/\(.*\)>/] + ' ' end |
#create_scratch_pad_if_needed ⇒ Object
Helpers
87 88 89 |
# File 'lib/motion/project/live.rb', line 87 def create_scratch_pad_if_needed FileUtils.touch(scratchpad_file) unless File.exist?(scratchpad_file) end |
#dirs ⇒ Object
99 100 101 |
# File 'lib/motion/project/live.rb', line 99 def dirs Dir.pwd end |
#find_changed_lines ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/motion/project/live.rb', line 73 def find_changed_lines new = scratch_pad if @old_code && !new.include?('#nodiff') delta = new.split("\n") - @old_code.split("\n") else delta = new.split("\n") end @old_code = new # reject comments delta.reject{ |l| l =~ /^\s*$/ || l =~ /^\s*#/ } end |
#run ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/motion/project/live.rb', line 36 def run create_scratch_pad_if_needed # TODO: running rake inside rake is very lame, # need to do something about it. PTY.spawn("rake", 'simulator') do |stdout, stdin, pid| @stdout, @stdin = stdout, stdin console_attach welcome "Welcome to liveconding" print console_self start_watching end rescue PTY::ChildExited adieu "[LIVE] Terminating as the simulator quitted" # TODO: need to trap interrupt to exit gracefully. # rescue Interrupt # adieu "Bye Bye" end |
#scratch_pad ⇒ Object
91 92 93 |
# File 'lib/motion/project/live.rb', line 91 def scratch_pad File.read(scratchpad_file) end |
#scratchpad_file ⇒ Object
95 96 97 |
# File 'lib/motion/project/live.rb', line 95 def scratchpad_file 'LiveScratchpad.rb' end |
#send_changes ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/motion/project/live.rb', line 62 def send_changes code = find_changed_lines return if code.empty? puts "\n\n#{code.join("\n")}\n".magenta print console_self code.each { |l| console_put l } wait_console_result puts "\n\n Changes processed \n\n".reversed print console_self end |
#start_watching ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/motion/project/live.rb', line 54 def start_watching fsevent = FSEvent.new fsevent.watch Dir.pwd do |dirs| send_changes end fsevent.run end |
#wait_console ⇒ Object
117 118 119 120 121 |
# File 'lib/motion/project/live.rb', line 117 def wait_console until @buffered_output =~ /\(.*\)[>,?]/ # Wait end end |
#wait_console_result ⇒ Object
123 124 125 126 127 |
# File 'lib/motion/project/live.rb', line 123 def wait_console_result until @buffered_output =~ /=> .*/ # Wait end end |
#welcome(string) ⇒ Object
Custom output
149 150 151 152 153 154 |
# File 'lib/motion/project/live.rb', line 149 def welcome(string) puts puts puts "-> #{string}".green puts end |