Module: Yarsh

Included in:
Shell
Defined in:
lib/yarsh.rb,
lib/yarsh.rb,
lib/yarsh/shell.rb,
lib/yarsh/config.rb,
lib/yarsh/prompt.rb,
lib/yarsh/version.rb,
lib/yarsh/instance_methods.rb,
lib/yarsh/ansi_color_formatter.rb,
sig/yarsh.rbs

Defined Under Namespace

Modules: InstanceMethods, Prompt Classes: AnsiColorFormatter, Config, Error, ExecOutput, InvalidPromptError, Shell

Constant Summary collapse

YARSH_DIR =
'~/.yarsh'
HISTORY_FILE =
File.join Yarsh::YARSH_DIR, 'history'
CONFIG_FILE =
File.join Yarsh::YARSH_DIR, 'config'
VERSION =

Returns:

  • (String)
'0.1.1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.consoleObject



76
77
78
79
# File 'lib/yarsh.rb', line 76

def self.console
  require 'irb'
  IRB.start(__FILE__)
end

.execute_system_command(command) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/yarsh.rb', line 58

def self.execute_system_command(command)
  Open3.popen3(command) do |input, output, error, wait_thread|
    output_printer = Thread.new do
      output.read.tap { |data| print data }
    end
    error_printer = Thread.new do
      error.read.tap { |data| $stderr.print data }
    end
    Thread.new do
      input.write($stdin.read)
    end
    output_printer.join
    error_printer.join

    wait_thread.value
  end
end

.expand_aliases(input, aliases) ⇒ Object

in a Shell input, find all command matching the keys of aliases and replace them with the values. Leading and trailing whitespace around the match is preserved.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/yarsh.rb', line 39

def self.expand_aliases(input, aliases)
  return input if aliases.empty?

  terms = aliases.keys.map { |k| Regexp.escape(k.to_s) }.join('|')
  regex = Regexp.new(/(^| +)(#{terms})(?![A-Za-z0-9_])/)
  input.gsub(regex) do |match|
    rep = aliases[match.strip]
    next match unless rep

    match[/\A( +)/, 1].to_s + rep
  end
end

Instance Method Details

#shell_path?(input) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/yarsh.rb', line 52

def shell_path?(input)
  input.match?(%r{\A\.\.?/}) ||
    input.match?(%r{\A/}) ||
    input.match?(/\A~/)
end