Class: Mysh::VarsCommand

Inherits:
Action show all
Defined in:
lib/mysh/internal/actions/vars.rb

Overview

  • mysh/internal/actions/vars.rb – The mysh internal variable commands.

Constant Summary collapse

VAR_EXP =

The mysh variable parsing regex.

%r{(?<name>   [a-z][a-z0-9_]*){0}
(?<equals> =){0}
(?<value>  \S.*){0}
\$ (\g<name> \s* (\g<equals> \s* \g<value>?)?)?}x

Constants inherited from Action

Action::ACTIONS_PATH

Instance Attribute Summary

Attributes inherited from Action

#description, #name

Instance Method Summary collapse

Methods inherited from Action

#action_info, #process_quick_command, #short_name

Constructor Details

#initialize(name, description) ⇒ VarsCommand

Setup an internal action.



16
17
18
19
# File 'lib/mysh/internal/actions/vars.rb', line 16

def initialize(name, description)
  @name = @equals = @value = nil
  super(name, description)
end

Instance Method Details

#do_commandObject

Do the actual work here.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mysh/internal/actions/vars.rb', line 30

def do_command
  sym = @name.to_sym if @name

  if @value
    MNV[sym] = @value
  elsif @equals
    MNV[sym] = ""
  elsif @name
    puts "#{@name} = #{MNV.get_source(sym)}"
  else
    show_all_values
  end
end

#process_command(input) ⇒ Object

Execute a command against the internal mysh variables.



22
23
24
25
26
27
# File 'lib/mysh/internal/actions/vars.rb', line 22

def process_command(input)
  match = VAR_EXP.match(input.raw)
  @name, @equals, @value = match[:name], match[:equals], match[:value]
  do_command
  :internal
end

#show_all_valuesObject

Display all variables neatly.



45
46
47
48
49
50
# File 'lib/mysh/internal/actions/vars.rb', line 45

def show_all_values
  puts (MNV.keys - ['$'.to_sym])
         .sort
         .map {|sym| ["$" + sym.to_s, MNV.get_source(sym)]}
         .format_mysh_bullets
end