Class: Mysh::VarsCommand

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

Overview

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

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/vars.rb', line 16

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

Instance Method Details

#do_commandObject

Do the actual work here.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mysh/internal/vars.rb', line 34

def do_command
  sym = @var_name.to_sym if @var_name

  if @value
    MNV[sym] = @value
  elsif @equals
    MNV[sym] = ""
  elsif @var_name
    puts "$#{@var_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
28
29
30
31
# File 'lib/mysh/internal/vars.rb', line 22

def process_command(input)
  if (match = VAR_EXP.match(input.raw_body))
    @var_name, @equals, @value = match[:name], match[:equals], match[:value]
  else
    @var_name, @equals, @value = nil
  end

  do_command
  :internal
end

#show_all_valuesObject

Display all variables neatly.



49
50
51
52
53
54
# File 'lib/mysh/internal/vars.rb', line 49

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