Sha256: 9dc041f35d0c83844ba1dd3d99a24fb18b57307e7384bbdc3b923f0bb1186fad

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

#!/usr/bin/env ruby
require 'curses'
require 'optparse'
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)

def parse_options
  options = OptionParser.new do |opts|
    opts.banner = <<BANNER
[Ru]by [Co]mmandline editor

Shortcuts:
 Ctrl+w/q   Exit
 Ctrl+s     Save

Usage:
    ruco FILE

Options:
BANNER
    opts.on("-h", "--help","Show this.") { puts opts; exit }
  end
  options.parse!

  if ARGV.empty?
    puts options
    exit
  end
end

def write(line,row,text)
  Curses.setpos(line,row)
  Curses.addstr(text);
end

def init_screen
  Curses.noecho # do not show typed chars
  Curses.init_screen
  Curses.stdscr.keypad(true) # enable arrow keys
  Curses.raw # give us all other keys

  begin
    yield
  ensure
    Curses.close_screen
  end
end

def display(widget, offset, color)
  Curses.attrset color
  lines = widget.view.naive_split("\n")
  Curses.stdscr.maxy.times do |i|
    line = lines[i] || ''
    clearer = Curses.stdscr.maxx - line.size
    clearer = " " * clearer
    write(i + offset, 0, line + clearer)
  end
end

parse_options

require 'ruco'
editor_offset = 1
editor = Ruco::Editor.new(ARGV[0], :lines => Curses.stdscr.maxy - editor_offset, :columns => Curses.stdscr.maxx)
status = Ruco::StatusBar.new(editor, :columns => Curses.stdscr.maxx)

init_screen do
  loop do
    display status, 0, Curses::A_REVERSE
    display editor, editor_offset, Curses::A_NORMAL

    Curses.setpos(editor.cursor_line + editor_offset, editor.cursor_column)

    key = Curses.getch

    case key
    when Curses::Key::UP then editor.move(-1,0)
    when Curses::Key::DOWN then editor.move(1,0)
    when Curses::Key::RIGHT then editor.move(0,1)
    when Curses::Key::LEFT then editor.move(0,-1)
    when 32..126 then editor.insert(key.chr) # printable
    when 10 then editor.insert("\n") # enter
    when 263 then editor.delete(-1) # backspace
    when ?\C-s then editor.save
    when ?\C-w, ?\C-q then break # quit
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruco-0.0.2 bin/ruco