Permalink
Browse files

do+as+coffee: exec coffee by default. Use Block.do_as_something.

  • Loading branch information...
1 parent 1801f9c commit ac97b17f5b97419ccfd98a9b94586a1560de2a71 @trogdoro committed Jan 1, 2013
Showing with 35 additions and 25 deletions.
  1. +12 −0 lib/xiki/block.rb
  2. +11 −3 lib/xiki/console.rb
  3. +12 −22 menu/coffee_script.rb
View
@@ -39,6 +39,18 @@ def self.do_as_wrap
$el.fill_paragraph nil
end
+ #
+ # Convenience method for keys like do+as+coffee.
+ #
+ # It...
+ # 1. grabs the text from current section (or per prefix),
+ # 2. invoked the block, passing in the text
+ # 3. inserts the output (under a ">>" heading)
+ #
+ # Block.do_as_something do |txt|
+ # CoffeeScript.execute txt
+ # end
+ #
def self.do_as_something
prefix = Keys.prefix
View
@@ -10,11 +10,11 @@ def self.menu
- .tree/
- .history/
- api/
- | In console (asynchronously)
+ > In console (asynchronously)
@ Console.run "ls"
@ Console.run "ls", :dir=>"/tmp"
- | Inline (synchronously)
+ > Inline (synchronously)
@ Console.sync "ls"
@ Console.sync "ls", :dir=>"/etc"
- docs/
@@ -103,6 +103,12 @@ def self.run command, options={}
return command if options[:no_enter]
profile = File.exists?(File.expand_path('~/.profile')) ? '. ~/.profile;' : ''
stdin, stdout, stderr = Open3.popen3("#{profile}cd \"#{dir}\";#{command}")
+
+ if txt = options[:stdin]
+ stdin.puts txt
+ stdin.close
+ end
+
result = ""
result << stdout.readlines.join('')
result << stderr.readlines.join('')
@@ -377,7 +383,9 @@ def self.launch options={}
# Clean up ^H formatting
output.gsub!(/.\cH/, "") # Add linebreak if blank
- Keys.prefix == 1 ? output.gsub!(/^/, '|') : output.gsub!(/^/, '| ').gsub!(/^\| +$/, '|')
+ Keys.prefix == 0 ? output.gsub!(/^/, '|') : output.gsub!(/^/, '| ')
+
+ output.gsub!(/^\| +$/, '|')
output.sub! /\n*\z/, "\n" # Guarantee exactly 1 linebreak at end
Tree.indent(output)
View
@@ -2,32 +2,22 @@ class CoffeeScript
# Called by keyboard shortcut
def self.run_block
- # Get block contents
- txt, left, right = View.txt_per_prefix #:prefix=>Keys.prefix
+ Block.do_as_something do |txt|
+ if Keys.prefix_u
+ self.to_js(txt)
+ else
+ txt = "p = print = console.log\n\n#{txt}"
+ self.execute(txt)
+ end
+ end
+ end
- result = self.to_js txt
- # Insert result at end of block
- orig = Location.new
- View.cursor = right
- Line.to_left
- View.insert ">>\n#{result}\n\n"
- orig.go
+ def self.execute txt
+ Console.sync 'coffee -s', :stdin=>txt
end
def self.to_js txt
-
- path = "/tmp/tmp.coffee"
-
- # If txt is file path, us it
- if txt !~ /\n/ && txt =~ /\.coffee$/
- path = txt
- else
- File.open(path, "w") { |f| f << txt } # Write to temp file
- end
-
- # Call js
- result = Console.run "coffee -pc \"#{path}\"", :sync=>true
- result.split("\n")[1..-2].join("\n")
+ Console.sync 'coffee -sc', :stdin=>txt
end
end

0 comments on commit ac97b17

Please sign in to comment.