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
-
.change_passwd(*args) ⇒ Object
Changes the encryption password.
-
.copy(key) ⇒ Object
Copys the value of a key onto the system clipboard.
-
.copy_raw(key = nil) ⇒ Object
Copys the value of a key onto the system clipboard.
-
.copyc(argstr) ⇒ Object
Copies the value of a single 1-indexed character of the value of a key to the system clipboard.
-
.del(key, force = false) ⇒ Object
Deletes a key.
-
.help(*args) ⇒ Object
Prints help text.
-
.paste(key) ⇒ Object
Sets a value from the system clipboard.
-
.print(key = nil) ⇒ Object
Prints either the tree if no argument is provided, or prints the value of a certain key.
-
.quit(*args) ⇒ Object
Quits.
-
.set(key) ⇒ Object
Set a value.
Class Method Details
.change_passwd(*args) ⇒ Object
Changes the encryption password.
104 105 106 107 108 109 |
# File 'lib/imp/commands.rb', line 104 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.
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/imp/commands.rb', line 158 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.
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/imp/commands.rb', line 140 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.
176 177 178 179 180 |
# File 'lib/imp/commands.rb', line 176 def self.copyc(argstr) pos, key = argstr.split(2) pos = pos.to_i (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.
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.
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 86 87 |
# 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). E.g. in a tree structure like some/ long/ path other/ path 'some/long/path' would be a valid key. Nodes can also have values, so path 'some/long/path' and 'some/long' can both have values assigned to them. Nodes are automatically created and destroyed as needed.") end |
.paste(key) ⇒ Object
Sets a value from the system clipboard. Fails if the clipboard is empty.
128 129 130 131 132 133 134 |
# File 'lib/imp/commands.rb', line 128 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 |
.print(key = nil) ⇒ Object
Prints either the tree if no argument is provided, or prints the value of a certain key.
93 94 95 96 97 98 99 |
# File 'lib/imp/commands.rb', line 93 def self.print(key = nil) if key print_val(key) else print_tree end end |
.quit(*args) ⇒ Object
Quits.
185 186 187 |
# File 'lib/imp/commands.rb', line 185 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.
115 116 117 118 119 120 121 122 |
# File 'lib/imp/commands.rb', line 115 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 |