Created
February 5, 2014 02:22
-
-
Save seven1m/8816411 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'curses' | |
include Curses | |
class Editor | |
LOG = File.open('editor.log', 'w') | |
def initialize | |
@x = 1 | |
@y = 1 | |
end | |
def move | |
print "\033[#{@y};#{@x}f" | |
end | |
def act(key) | |
case key | |
when "\e[A" | |
@y -= 1 | |
move | |
when "\e[B" | |
@y += 1 | |
move | |
when "\e[C" | |
@x += 1 | |
move | |
when "\e[D" | |
@x -= 1 | |
move | |
when "\r" | |
@x = 1 | |
@y += 1 | |
move | |
else | |
print key | |
@x += 1 | |
move | |
end | |
LOG.puts([@y, @x].inspect) | |
LOG.puts(key.inspect); LOG.flush | |
end | |
def loop | |
key = '' | |
in_escape = false | |
begin | |
char = STDIN.getc | |
in_escape = true if char == "\e" | |
key += char | |
if in_escape | |
if key.length == 3 | |
act(key) | |
key = '' | |
in_escape = false | |
end | |
else | |
act(key) | |
key = '' | |
end | |
end until key == 'Q' | |
end | |
end | |
begin | |
clear; refresh | |
cbreak; noecho #; curs_set(0) | |
editor = Editor.new | |
editor.loop | |
ensure | |
nocbreak; echo; curs_set(1) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment