Class: Yarsh::Shell

Inherits:
Object
  • Object
show all
Includes:
Yarsh, InstanceMethods
Defined in:
lib/yarsh/shell.rb

Constant Summary collapse

SHELL_BUILTINS =
%w[alias bg bind break builtin cd command echo eval exec exit
export false fc fg getopts hash help history jobs kill let
local logout mapfile popd printf pushd pwd read readonly
return set shift shopt source suspend test times trap true
type typeset ulimit umask unalias unset wait].freeze

Constants included from InstanceMethods

InstanceMethods::SEVERITIES

Constants included from Yarsh

CONFIG_FILE, HISTORY_FILE, VERSION, YARSH_DIR

Instance Method Summary collapse

Methods included from InstanceMethods

#configure, #sh_alias, #source

Methods included from Yarsh

console, execute_system_command, expand_aliases, #shell_path?

Constructor Details

#initialize(bind) ⇒ Shell

Returns a new instance of Shell.



13
14
15
16
17
18
19
20
21
# File 'lib/yarsh/shell.rb', line 13

def initialize(bind)
  @bind = bind
  @core = Reline.core
  @history_file = File.expand_path(HISTORY_FILE)
  @config_file = File.expand_path(CONFIG_FILE)
  @cf = Yarsh::AnsiColorFormatter.new
  setup_reline_history
  setup_reline_completion
end

Instance Method Details

#shellObject



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
# File 'lib/yarsh/shell.rb', line 23

def shell
  # TODO: defined method in the config file should be availabel in the shell
  load @config_file, InstanceMethods if File.exist? @config_file
  loop do
    input = begin
      @core.readline(Config.instance.prompt, true)
    rescue Interrupt
      next
    end
    if input.nil? or input == 'exit'
      puts 'Bye...'
      break
    end

    append_history(input, @history_file)

    begin
      if input.include?('-->')
        fp execute_pipe(input, @bind)
        next
      end

      if shell_path?(input)
        execute(input)
        next
      end

      if input == 'cd' || input.start_with?('cd ')
        cd(input.split(' ', 2)[1])
        next
      end

      begin
        result = @bind.eval(input)
        fp result
      rescue NameError, SyntaxError, ArgumentError => rb_error
        unless shell_command?(input) || rb_error.is_a?(NameError) || rb_error.is_a?(SyntaxError)
          $rber = rb_error
          puts @cf.fg_red("Error: ArgumentError: #{rb_error.message}")
          next
        end
        info("Assuming it is not a ruby expression, the error: #{rb_error.message}")
        debug("The detail: #{rb_error.backtrace.join("\n")}")
        begin
          input = Yarsh.expand_aliases(input, Config.instance.aliases)
          execute(input)
        rescue Exception => e
          $sher = e
          info("#{e.class}: #{e.message}")
          puts "#{@cf.bold.underline.fg_blue('Shell:')} '#{input}': #{e.message}"
          puts "#{@cf.bold.underline.fg_red('Ruby:')} '#{input}': #{rb_error.message}"
        end
      end
    rescue Interrupt
      next
    rescue SystemExit
      raise
    rescue Exception => e
      $rber = e
      puts @cf.fg_red("Error: #{e.class}: #{e.message}\n#{e.backtrace.join("\n")}")
    end
  end
end