Module: Tins::Complete

Included in:
Object
Defined in:
lib/tins/complete.rb

Overview

A module that provides completion functionality for objects.

Constant Summary collapse

@@sync =
Sync.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.complete(prompt: '', add_hist: false) {|String| ... } ⇒ String

The complete method reads a line of input from the user with optional prompt and history support.

history

Examples:

Prompt with easy tab completion

complete(prompt: 'Pick a ruby file! ') {
  Dir['**/*.rb'].grep(/#{it}/)
}.then { '%u lines' % File.new(it).each_line.count }

Parameters:

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

    the prompt string to display to the user

  • add_hist (Boolean) (defaults to: false)

    whether to add the input to the command

Yields:

  • (String)

    the completion procedure to use for tab completion

Returns:

  • (String)

    the line of input entered by the user



25
26
27
28
29
30
31
32
# File 'lib/tins/complete.rb', line 25

def complete(prompt: '', add_hist: false, &block)
  @@sync.synchronize do
    Readline.completion_proc = block
    Readline.input           = STDIN
    Readline.output          = STDOUT
    Readline.readline(prompt, add_hist)
  end
end

Instance Method Details

#complete(prompt: '', add_hist: false) {|String| ... } ⇒ String (private)

The complete method reads a line of input from the user with optional prompt and history support.

history

Examples:

Prompt with easy tab completion

complete(prompt: 'Pick a ruby file! ') {
  Dir['**/*.rb'].grep(/#{it}/)
}.then { '%u lines' % File.new(it).each_line.count }

Parameters:

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

    the prompt string to display to the user

  • add_hist (Boolean) (defaults to: false)

    whether to add the input to the command

Yields:

  • (String)

    the completion procedure to use for tab completion

Returns:

  • (String)

    the line of input entered by the user



25
26
27
28
29
30
31
32
# File 'lib/tins/complete.rb', line 25

def complete(prompt: '', add_hist: false, &block)
  @@sync.synchronize do
    Readline.completion_proc = block
    Readline.input           = STDIN
    Readline.output          = STDOUT
    Readline.readline(prompt, add_hist)
  end
end