#!/usr/bin/env ruby require 'curses' require 'optparse' lib = File.expand_path("../../lib", __FILE__) $LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib) # enable local development def parse_options options = {} parser = OptionParser.new do |opts| opts.banner = <<-BANNER.gsub(/^ {6}/, "") [Ru]by [Co]mmandline editor Shortcuts: Ctrl+w/q Exit Ctrl+s Save Usage: ruco FILE Options: BANNER opts.on("-c", "--convert-tabs","Convert tabs to spaces") { options[:convert_tabs] = true } opts.on("-u", "--undo-stack-size SIZE","Maximum size of the undo stack. 0 allows for a complete undo stack.") { |size| options[:undo_stack_size] = size.to_i } opts.on("-n", "--no-colors","No colors -- helps performance / broken terminals") { options[:no_colors] = true } opts.on("--colors","Force colors -- everything could be black") { options[:colors] = true } opts.on("--debug-cache","Show caching in action") { options[:debug_cache] = true } opts.on("--debug-keys", "Show pressed keys") { options[:debug_keys] = true } opts.on("-v", "--version","Show Version") do require 'ruco/version' puts Ruco::VERSION exit end opts.on("-h", "--help","Show this.") { puts opts; exit } end parser.parse! if ARGV.empty? puts parser exit end options end def log(stuff) File.open('ruco.log','ab') { |f| f.puts stuff } end options = parse_options options[:colors] = if options[:no_colors] false elsif options[:colors] true else # so far the only terminal that supports it: # - xterm-256color on osx # - xterm on ubuntu 10.04+ if RbConfig::CONFIG['host_os'] !~ /mswin|mingw/ if ENV["TERM"] == 'xterm-256color' true else require 'ruco/file_store' if ENV['TERM'] == 'xterm' # switch terminal, so curses knows we want colors # setting ENV['TERM'] will sometimes crash un-rescue-able -> test if it works possible = Ruco::FileStore.new('~/.ruco/cache').cache('color_possible') do system(%{TERM=xterm-256color ruby -r curses -e 'Curses.noecho' > /dev/null 2>&1}) end ENV['TERM'] = 'xterm-256color' if possible end end end end $ruco_colors = options[:colors] require 'ruco' # draw app and redraw after each keystroke (or paste) Dispel::Screen.open(options) do |screen| $ruco_screen = screen app = Ruco::Application.new( ARGV[0], :convert_tabs => options[:convert_tabs], :undo_stack_size => options[:undo_stack_size], :lines => screen.lines, :columns => screen.columns ) screen.draw *app.display_info Dispel::Keyboard.output do |key| screen.debug_key(key) if options[:debug_keys] if key == :resize app.resize(screen.lines, screen.columns) else result = app.key key break if result == :quit end screen.draw *app.display_info end end