Module: Trith::Shell

Defined in:
lib/trith/shell.rb,
lib/trith/shell/inspect.rb,
lib/trith/shell/history.rb

Overview

Supporting code for the 3sh shell.

Defined Under Namespace

Modules: History, Inspect

Class Method Summary (collapse)

Class Method Details

+ (Boolean) broken_append_character? (protected)

Returns true if Readline.completion_append_character is broken on this Ruby implementation.

The feature in question is currently broken on Ruby with libedit and just unimplemented on JRuby 1.4.x/1.5.x



141
142
143
# File 'lib/trith/shell.rb', line 141

def self.broken_append_character?
  [:libedit, :jruby].include?(readline_library)
end

+ (Boolean) broken_initial_history? (protected)

Returns true if the first call to Readline::HISTORY.push is broken on this Ruby implementation.

This is a libedit-related bug encountered with the default Ruby versions shipped with Mac OS X.



128
129
130
# File 'lib/trith/shell.rb', line 128

def self.broken_initial_history?
  readline_library == :libedit
end

+ (Proc) completion_proc(cache)

Returns an input completion proc for use with Readline.

Parameters:

Returns:

  • (Proc)

See Also:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/trith/shell.rb', line 83

def self.completion_proc(cache)
  Readline.completion_append_character = ' ' unless broken_append_character?
  proc do |prefix|
    candidates = []
    # TODO: optimize this once RDF::Queryable supports #query([s, p, /o/])
    cache.each_function do |function|
      function.labels.each do |label|
        candidates << label.to_s if label.to_s.start_with?(prefix)
      end
    end
    candidates.map! { |candidate| candidate << ' ' } if broken_append_character?
    candidates.uniq
  end
end

+ (String) format_error(error, options = {})

Returns a colorized error message string.

Parameters:

  • (Exception) error
  • (Hash{Symbol => Object}) options (defaults to: {})

Returns:

  • (String)


35
36
37
# File 'lib/trith/shell.rb', line 35

def self.format_error(error, options = {})
  Inspect.colorize(error.to_s, has_color? ? :red : nil)
end

+ (Boolean) has_color?

Returns true if the shell's terminal supports ANSI color escapes.

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/trith/shell.rb', line 102

def self.has_color?
  @has_color ||= case
    when !$stdout.isatty        then false
    when ENV['TERM'] =~ /color/ then true  # e.g. "xterm-color"
    else
      begin
        # @see http://rubygems.org/gems/ruby-terminfo
        require 'terminfo' unless defined?(TermInfo)
        TermInfo.default_object.tigetnum('colors').to_i > 1
      rescue
        false # the only safe default
      end
  end
end

+ (String) inspect(value, options = {})

Returns a developer-friendly representation of value.

Parameters:

  • (Object) value
  • (Hash{Symbol => Object}) options (defaults to: {})

Returns:

  • (String)


25
26
27
# File 'lib/trith/shell.rb', line 25

def self.inspect(value, options = {})
  Inspect.inspect(value, {:color => has_color?}.merge(options))
end

+ (String) readline(prompt)

Reads and returns a line of input from the terminal.

Parameters:

  • (String) prompt

Returns:

  • (String)

See Also:



45
46
47
# File 'lib/trith/shell.rb', line 45

def self.readline(prompt)
  Readline.readline(prompt)
end

+ (Symbol) readline_library

Returns a symbol indicating which Readline library this Ruby implementation uses.

The possible return values are :readline, :libedit and :jruby.

The default Ruby versions shipped with Mac OS X use :libedit as their Readline library and have buggy functionality that needs to be worked around.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/trith/shell.rb', line 61

def self.readline_library
  @readline_library ||= case RUBY_PLATFORM.to_sym
    when :java
      RUBY_ENGINE.to_sym rescue :jruby   # JRuby 1.4.x/1.5.x
    else
      begin                              # Ruby with `readline`
        Readline.emacs_editing_mode
        :readline
      rescue NotImplementedError         # Ruby with `libedit` (Mac OS X)
        :libedit
      rescue
        :libedit                         # just to be on the safe side
      end
  end
end

+ setup!(cache)

This method returns an undefined value.

Initializes shell functionality and the Readline library.

Parameters:



15
16
17
# File 'lib/trith/shell.rb', line 15

def self.setup!(cache)
  Readline.completion_proc = completion_proc(cache)
end