all repos — h3rald @ 14b0a0d76c1c8f800eef51b97d082a0088c0686a

The sources of https://h3rald.com

content/rawline.textile

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
----- 
permalink: rawline
filters_pre: 
- erb
- redcloth
title: RawLine
comments: []

date: 2009-02-28 14:02:13 +01:00
tags: []

type: page
toc: true
-----
h2. RawLine

RawLine is a 100% Ruby alternative to the ReadLine library, providing some of its most popular features such as:

    * Basic line editing operations
    * Word completion
    * History Management
    * Custom key/key sequences bindings

h3. Installation

The simplest method to install RawLine is to install the gem:

<% highlight :text do %>gem install rawline<% end %>

h3. Usage

Editor initialization:

<% highlight :ruby do %>
require 'rawline'
editor = RawLine::Editor.new
<% end %>

Key binding:

<% highlight :ruby do %>
editor.bind(:ctrl_z) { editor.undo }
editor.bind(:up_arrow) { editor.history_back }
editor.bind(:ctrl_x) { puts "Exiting..."; exit }
<% end %>

Setup word completion

<% highlight :ruby do %>
editor.completion_proc = lambda do |word|
	if word
		['select', 'update', 'delete', 'debug', 'destroy'].find_all  { |e| e.match(/^#{Regexp.escape(word)}/) }
	end
end

editor.completion_append_string = " "
<% end %>

Read input:

<% highlight :ruby do %>editor.read("=> ", true)<% end %>

h3. Replacing Readline

Simply include the RawLine (or Rawline) module:

<% highlight :ruby do %>include Rawline<% end %>

…and you’ll get:

<% highlight :ruby do %>
readline(prompt, add_history) # RawLine::Editor#read(prompt, add_history)
HISTORY # RawLine::Editor#history
FILENAME_COMPLETION_PROC # Rawline::Editor#filename_completion_proc
# ...
<% end %>

but also:

<% highlight :ruby do %>Rawline.editor # RawLine::Editor<% end %>

…which opens a world of endless possibilities! ;-)

h3. Resources

* "News and Articles":http://www.h3rald.com/tags/rawline
* "RDoc Documentation":http://rawline.rubyforge.org [on RubyForge]
* "Download Files":http://rubyforge.org/projects/rawline [on RubyForge]
* "Source Code":http://github.com/h3rald/rawline/tree/master [on GitHub]
* "Issue Tracking":http://h3rald.lighthouseapp.com/projects/26374-rawline/overview [on LightHouse]