Module: Bestliner

Defined in:
lib/bestliner.rb,
lib/bestliner/version.rb,
ext/bestliner/bestliner.c

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.__add_history(line) ⇒ Object



69
70
71
72
73
# File 'ext/bestliner/bestliner.c', line 69

static VALUE bestliner_bestlineHistoryAdd(VALUE self, VALUE line) {
    int rc;
    rc = bestlineHistoryAdd(StringValueCStr(line));
    return INT2NUM(rc);
}

.__bestline(prompt) ⇒ Object

Wrapper Functions



39
40
41
42
43
44
45
46
47
# File 'ext/bestliner/bestliner.c', line 39

static VALUE bestliner_bestline(VALUE self, VALUE prompt) {
    VALUE result;
    char *line;
    line = bestline(StringValueCStr(prompt));
    if (line == NULL) return Qnil;
    result = rb_utf8_str_new_cstr(line);
    free(line);
    return result;
}

.__bestline_raw(prompt, in, out) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'ext/bestliner/bestliner.c', line 59

static VALUE bestliner_bestlineRaw(VALUE self, VALUE prompt, VALUE in, VALUE out) {
    VALUE result;
    char *line;
    line = bestlineRaw(StringValueCStr(prompt), NUM2INT(in), NUM2INT(out));
    if (line == NULL) return Qnil;
    result = rb_utf8_str_new_cstr(line);
    free(line);
    return result;
}

.__bestline_with_history(prompt, filename) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'ext/bestliner/bestliner.c', line 49

static VALUE bestliner_bestlineWithHistory(VALUE self, VALUE prompt, VALUE filename) {
    VALUE result;
    char *line;
    line = bestlineWithHistory(StringValueCStr(prompt), StringValueCStr(filename));
    if (line == NULL) return Qnil;
    result = rb_utf8_str_new_cstr(line);
    free(line);
    return result;
}

.__clear_screen(out) ⇒ Object



92
93
94
95
# File 'ext/bestliner/bestliner.c', line 92

static VALUE bestliner_bestlineClearScreen(VALUE self, VALUE out) {
    bestlineClearScreen(NUM2INT(out));
    return Qnil;
}

.__free_historyObject



87
88
89
90
# File 'ext/bestliner/bestliner.c', line 87

static VALUE bestliner_bestlineHistoryFree(VALUE self) {
    bestlineHistoryFree();
    return Qnil;
}

.__load_history(filename) ⇒ Object



81
82
83
84
85
# File 'ext/bestliner/bestliner.c', line 81

static VALUE bestliner_bestlineHistoryLoad(VALUE self, VALUE filename) {
    int rc;
    rc = bestlineHistoryLoad(StringValueCStr(filename));
    return INT2NUM(rc);
}

.__mask_mode_disableObject



102
103
104
105
# File 'ext/bestliner/bestliner.c', line 102

static VALUE bestliner_bestlineMaskModeDisable(VALUE self) {
    bestlineMaskModeDisable();
    return Qfalse;
}

.__mask_mode_enableObject



97
98
99
100
# File 'ext/bestliner/bestliner.c', line 97

static VALUE bestliner_bestlineMaskModeEnable(VALUE self) {
    bestlineMaskModeEnable();
    return Qtrue;
}

.__save_history(filename) ⇒ Object



75
76
77
78
79
# File 'ext/bestliner/bestliner.c', line 75

static VALUE bestliner_bestlineHistorySave(VALUE self, VALUE filename) {
    int rc;
    rc = bestlineHistorySave(StringValueCStr(filename));
    return INT2NUM(rc);
}

.__set_completion_cb(proc) ⇒ Object



107
108
109
110
111
# File 'ext/bestliner/bestliner.c', line 107

static VALUE bestliner_bestlineSetCompletionCallback(VALUE self, VALUE proc) {
    rb_iv_set(self, "@completion_callback", proc);
    bestlineSetCompletionCallback(completionCallback);
    return proc;
}

.__set_hints_cb(proc) ⇒ Object



113
114
115
116
117
# File 'ext/bestliner/bestliner.c', line 113

static VALUE bestliner_bestlineSetHintsCallback(VALUE self, VALUE proc) {
    rb_iv_set(self, "@hints_callback", proc);
    bestlineSetHintsCallback(hintsCallback);
    return proc;
}

.add_history(line) ⇒ Object

Adds the line to the internal history

Parameters:

  • line (String)

    the line to add

Raises:

  • (ArgumentError)


41
42
43
44
45
# File 'lib/bestliner.rb', line 41

def add_history(line)
  raise ArgumentError, "line must be of class String" unless line.is_a?(String)
  return_code = __add_history line
  raise IOError, "cannot write to history" unless return_code == 1
end

.bestline(prompt = "") ⇒ String?

Reads text input after an optional prompt

Parameters:

  • prompt (String) (defaults to: "")

    the prompt to display before the input cursor

Returns:

  • (String, nil)

    the line of input, ‘nil` if EOF

Raises:

  • (ArgumentError)


9
10
11
12
# File 'lib/bestliner.rb', line 9

def bestline(prompt = "")
  raise ArgumentError, "prompt must be of class String" unless prompt.is_a?(String)
  __bestline prompt
end

.bestline_raw(prompt = "", input, output) ⇒ Object

Reads text input after an optional prompt from the input and displays on output

Parameters:

  • prompt (String) (defaults to: "")

    the prompt to display before the input cursor

  • input (IO)

    the stream to use as input

  • output (IO)

    the stream to use as output

Raises:

  • (ArgumentError)


31
32
33
34
35
36
# File 'lib/bestliner.rb', line 31

def bestline_raw(prompt = "", input, output)
  raise ArgumentError, "prompt must be of class String" unless prompt.is_a?(String)
  raise ArgumentError, "input must be of class IO" unless input.is_a?(IO)
  raise ArgumentError, "output must be of class IO" unless output.is_a?(IO)
  __bestline_raw prompt, input.fileno, output.fileno
end

.bestline_with_history(prompt = "", filename = ".bestliner_history") ⇒ Object

Reads text input after an optional prompt and saves input to a file

Parameters:

  • prompt (String) (defaults to: "")

    the prompt to display before the input cursor

  • filename (String) (defaults to: ".bestliner_history")

    the file to use for saving input history

Raises:

  • (ArgumentError)


19
20
21
22
23
# File 'lib/bestliner.rb', line 19

def bestline_with_history(prompt = "", filename = ".bestliner_history")
  raise ArgumentError, "prompt must be of class String" unless prompt.is_a?(String)
  raise ArgumentError, "filename must be of class String" unless filename.is_a?(String)
  __bestline_with_history prompt, filename
end

.clear_screen(output) ⇒ Object

Clears the screen of the output stream

Parameters:

  • output (IO)

    the stream to use as output

Raises:

  • (ArgumentError)


73
74
75
76
# File 'lib/bestliner.rb', line 73

def clear_screen(output)
  raise ArgumentError, "output must be of class IO" unless output.is_a?(IO)
  __clear_screen output.fileno
end

.completion_callback=(callback) ⇒ Object

Sets the completion callback

Parameters:

  • callback (#call)

    callback that returns completions

Raises:

  • (ArgumentError)


99
100
101
102
# File 'lib/bestliner.rb', line 99

def completion_callback=(callback)
  raise ArgumentError, "callback must respond to :call" unless callback.respond_to?(:call)
  __set_completion_cb callback
end

.free_historyObject

Frees the memory used for the internal history



66
67
68
# File 'lib/bestliner.rb', line 66

def free_history
  __free_history
end

.hints_after=(ansi_code) ⇒ Object

Sets the ANSI code to use after a hint

Parameters:

  • ansi_code (String)

    ANSI code output after hint

Raises:

  • (ArgumentError)


124
125
126
127
# File 'lib/bestliner.rb', line 124

def hints_after=(ansi_code)
  raise ArgumentError, "ansi_code must be of class String" unless ansi_code.is_a?(String)
  @hints_after = ansi_code
end

.hints_before=(ansi_code) ⇒ Object

Sets the ANSI code to use before a hint

Parameters:

  • ansi_code (String)

    ANSI code output before hint

Raises:

  • (ArgumentError)


116
117
118
119
# File 'lib/bestliner.rb', line 116

def hints_before=(ansi_code)
  raise ArgumentError, "ansi_code must be of class String" unless ansi_code.is_a?(String)
  @hints_before = ansi_code
end

.hints_callback=(callback) ⇒ Object

Sets the hints callback

Parameters:

  • callback (#call)

    callback that returns a hint

Raises:

  • (ArgumentError)


107
108
109
110
# File 'lib/bestliner.rb', line 107

def hints_callback=(callback)
  raise ArgumentError, "callback must respond to :call" unless callback.respond_to?(:call)
  __set_hints_cb callback
end

.load_history(filename) ⇒ Object

Loads the history from the file

Raises:

  • (Errno::ENOENT)


59
60
61
62
63
# File 'lib/bestliner.rb', line 59

def load_history(filename)
  raise Errno::ENOENT, filename unless File.exist?(filename)
  return_code = __load_history filename
  raise IOError, "cannot load history from #{filename}" unless return_code == 0
end

.mask_mode=(is_enabled) ⇒ Object

Sets the mask mode

Parameters:

  • is_enabled (Boolean)

    whether mask mode is enabled



81
82
83
84
85
86
87
# File 'lib/bestliner.rb', line 81

def mask_mode=(is_enabled)
  @mask_mode = if is_enabled
    __mask_mode_enable
  else
    __mask_mode_disable
  end
end

.mask_mode?Boolean

Checks the status of mask mode

Returns:

  • (Boolean)

    whether mask mode is enabled



92
93
94
# File 'lib/bestliner.rb', line 92

def mask_mode?
  !!(defined?(@mask_mode) && @mask_mode)
end

.save_history(filename) ⇒ Object

Saves the history to the file

Parameters:

  • filename (String)

    the filename to use

Raises:

  • (ArgumentError)


50
51
52
53
54
# File 'lib/bestliner.rb', line 50

def save_history(filename)
  raise ArgumentError, "filename must be of class String" unless filename.is_a?(String)
  return_code = __save_history filename
  raise IOError, "cannot save history to #{filename}" unless return_code == 0
end