Module: Imp::Commands

Defined in:
lib/imp/commands.rb

Overview

Contains the methods for all commands issued by the user. All commands are executed with Commands#send

Constant Summary collapse

METHODS =

The signals which should be sent to this module.

[
"help",
"set",
"change_passwd",
"paste",
"print",
"copy",
"copy_raw",
"copyc",
"del",
"exit"]

Class Method Summary collapse

Class Method Details

.change_passwd(*args) ⇒ Object

Changes the encryption password.

Parameters:

  • args (Array)

    Ignored.



91
92
93
94
95
96
# File 'lib/imp/commands.rb', line 91

def self.change_passwd(*args)
  pass = Util.read_passwd("password for file '#{$file}'")
  return unless pass
  $tree.password = pass
  $tree.flush
end

.copy(key) ⇒ Object

Copys the value of a key onto the system clipboard. And auto-clears it afterwards.

Parameters:

  • key (String)

    The key of the value to copy.



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/imp/commands.rb', line 145

def self.copy(key)
  fail "Key must be supplied." unless key
  begin
    UI.timeout do
      copy_raw key
      $stdout.print "Value copied. Press enter to wipe..."
      gets
    end
  ensure
    Clipboard.clear
  end
end

.copy_raw(key = nil) ⇒ Object

Copys the value of a key onto the system clipboard.

Parameters:

  • key (String, nil) (defaults to: nil)

    The key of the value to copy. If nil, clears the clipboard instead.



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/imp/commands.rb', line 127

def self.copy_raw(key = nil)
  begin
    if key
      Clipboard.copy($tree[key])
    else
      Clipboard.clear
    end
  # No method error arises from trying to work on a nil tree (or trying to
  # decrypt a nil value).
  rescue NoMethodError
    fail "No value entered for key '#{key}'."
  end
end

.copyc(argstr) ⇒ Object

Copies the value of a single 1-indexed character of the value of a key to the system clipboard.

Parameters:

  • argstr (String)

    The index to copy followed by the key, seperated by whitespace.



163
164
165
166
167
# File 'lib/imp/commands.rb', line 163

def self.copyc(argstr)
  pos, key = argstr.split(2)
  pos = pos.to_i
  copyc_expanded(char, key)
end

.del(key, force = false) ⇒ Object

Deletes a key. If the key has no children, it is removed from the tree. If it has children, it is removed from the tree if and only if it’s value was previously nil. Otherwise it’s value is set to nil.

Parameters:

  • key (String)

    The key to delete.

  • force (Boolean) (defaults to: false)

    Doesn’t require confirmation from the user if it is true.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/imp/commands.rb', line 33

def self.del(key, force = false)
  unless force ||
      agree("Are you sure you want to delete the key '#{key}'? ")
    return
  end
  node = $tree.cont.descendant(key)
  fail "Key does not exist." if node == nil
  
  if node.val == nil
    $tree.delete key
  else
    node.val = nil
  end
  # Remove any nil-leaves. (This may remove key IF it is a leaf)
  $tree.prune
  # Write out the tree.
  $tree.flush
end

.help(*args) ⇒ Object

Prints help text.

Parameters:

  • args (Array)

    Ignored.



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

def self.help(*args)
  puts ("
help          - Prints this help text
set KEY       - Sets the value of the key to a value entered by the user.
change_passwd - Changes the password of the current file.
paste KEY     - Sets the value of the key from the system clipboard.
print         - Prints a representation of the tree, without values.
print KEY     - Prints the value of the key.
copy KEY      - Copies the value of the key, auto clears clipboard afterward.
copy_raw      - Clears the clipboard.
copy_raw KEY  - Copies the value of a key, without clearing the clipboard.
            Useful for moving values around between keys.
copyc INT KEY - Copies the (1-indexed) character from the value of the key.
del KEY       - Deletes the key from the tree. If it has subtrees, the
            subtrees get deleted if and only if the key had no value.
exit          - Exit.

Keys are sorted in forward-slash seperated tree structure (slightly
remenicient of urls).")
end

.paste(key) ⇒ Object

Sets a value from the system clipboard. Fails if the clipboard is empty.

Parameters:

  • key (String)

    The key to set.



115
116
117
118
119
120
121
# File 'lib/imp/commands.rb', line 115

def self.paste(key)
  fail "Key must be supplied." unless key
  pass = Clipboard.paste
  fail "Clipboard empty, could not paste." if pass == ''
  $tree[key] = pass
  $tree.flush
end

Prints either the tree if no argument is provided, or prints the value of a certain key.

Parameters:

  • key (String, nil) (defaults to: nil)

    The key if provided, or nil to print the tree.



80
81
82
83
84
85
86
# File 'lib/imp/commands.rb', line 80

def self.print(key = nil)
  if key
    print_val(key)
  else
    print_tree
  end
end

.quit(*args) ⇒ Object

Quits.

Parameters:

  • args (Array)

    Ignored.



172
173
174
# File 'lib/imp/commands.rb', line 172

def self.quit(*args)
  exit
end

.set(key) ⇒ Object

Set a value. Require entering the value to set it to twice until they match. An empty value will cancel setting.

Parameters:

  • key (String)

    The key to set the value for.



102
103
104
105
106
107
108
109
# File 'lib/imp/commands.rb', line 102

def self.set(key)
  fail "Key must be supplied." unless key
  pass = Util.read_passwd
  return unless pass
  $tree[key] = pass
  # We save the tree whenever it is modified.
  $tree.flush
end