telepath 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b38bc8a0619f00d35224b7e2586a0d193074f7a3
4
- data.tar.gz: 81772bf8131f737c3071e2566158559f576edaa7
3
+ metadata.gz: 9157b212267be8ba4d41ed1cd42f5b4eefdb1c1d
4
+ data.tar.gz: a661c2d3028ebbf03b7470c6c95a82b6066963e9
5
5
  SHA512:
6
- metadata.gz: a32bab376cf4b20156be53dceb4c5e567a103f4dc4174664dc16a99d746eabd8582cbb34329266d7eb79444c231205629071b501b1556ebff4a98258b340f07a
7
- data.tar.gz: 16b7370de869fc20fb802bdea9ca45516c1ddb0b70dd8d947b9a2ef2fd522c68bca4eb252771afe5096bab41fb08a2d0811ee65ba4e3fb5edc2dab06d28e4ea6
6
+ metadata.gz: f7c8c46a8a91a9e7cf25ea59efcfb35a5b4a6a38b3ff1a8f67fbf65a3e856672b1ea687c1c15b727f1d4555b90de1541c1232f7fb9d70187a5cd2d0019944704
7
+ data.tar.gz: aab5d675ed50d4273308bb0ad1143b84349545d9a5ad6b10a95e08be3f1e3860d7e7c5f9f7d93aff2e5610f167acfe14e23c61f70b9986c50f31df1a31c0a9ec
data/README.markdown CHANGED
@@ -66,12 +66,13 @@ Subcommands:
66
66
  ?, lookup Look up item by pattern
67
67
  $, last Get most recent item
68
68
  @, index Get item from (reverse) index
69
+ list List known containers and contents
69
70
 
70
71
  Options:
71
- -q, --quiet Only output when absolutely necessary. (default: $TELEPORT_QUIET, or false)
72
- -f, --file FILE Filename of the Teleport store file. (default: $TELEPORT_FILE, or ".telepath.db")
73
- -p, --path PATH Path where the the Teleport store file is located. (default: $TELEPORT_PATH, or "~")
74
72
  -h, --help print help
73
+ -q, --quiet Only output when absolutely necessary. (default: $TELEPATH_QUIET, or false)
74
+ -f, --file FILE Filename of the Teleport store file. (default: $TELEPATH_FILE, or ".telepath.db")
75
+ -p, --path PATH Path where the the Teleport store file is located. (default: $TELEPATH_PATH, or "~")
75
76
  ```
76
77
 
77
78
  Example
data/bin/tel CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  require_relative '../lib/telepath'
4
4
 
5
- Telepath::Command.run
5
+ Telepath::CommandLine::Main.run
6
6
 
@@ -1,97 +1,137 @@
1
1
  require 'clamp'
2
2
 
3
3
  module Telepath
4
- class Command < ::Clamp::Command
4
+ class CommandLine
5
+ class Command < Clamp::Command
5
6
 
6
- option ['-q', '--quiet'], :flag, 'Only output when absolutely necessary.',
7
- environment_variable: 'TELEPORT_QUIET', default: false
7
+ # GLOBAL OPTIONS
8
8
 
9
- option ['-f', '--file'], 'FILE', 'Filename of the Teleport store file.',
10
- environment_variable: 'TELEPORT_FILE', default: Telepath::Storage::DEFAULT_FILE
9
+ option ['-q', '--quiet'], :flag, 'Only output when absolutely necessary.',
10
+ environment_variable: 'TELEPATH_QUIET', default: false
11
11
 
12
- option ['-p', '--path'], 'PATH', 'Path where the the Teleport store file is located.',
13
- environment_variable: 'TELEPORT_PATH', default: Telepath::Storage::DEFAULT_PATH
12
+ option ['-f', '--file'], 'FILE', 'Filename of the Teleport store file.',
13
+ environment_variable: 'TELEPATH_FILE', default: Telepath::Storage::DEFAULT_FILE
14
14
 
15
- subcommand ['+', 'add'], 'Add item' do
16
- parameter '[VALUE] ...', 'value to add', attribute_name: 'values'
15
+ option ['-p', '--path'], 'PATH', 'Path where the the Teleport store file is located.',
16
+ environment_variable: 'TELEPATH_PATH', default: Telepath::Storage::DEFAULT_PATH
17
17
 
18
- def execute
19
- handler = Telepath::Handler.new self
20
- values = self.values || Array.new
18
+ # HELPERS
19
+
20
+ def setup_environment
21
+ # quiet
22
+ # timeout
23
+ # file
24
+ # path
25
+ end
26
+
27
+ def handler
28
+ @handler ||= Telepath::Handler.new self
29
+ end
21
30
 
22
- unless $stdin.tty? then
23
- buffered = IO.select([$stdin], [], [], 1)
24
- values << $stdin.read.strip if buffered && buffered.first && buffered.first.first
31
+ def data_out value, failure_message
32
+ if value then
33
+ Out.data value
34
+ else
35
+ Out.error self, failure_message
25
36
  end
37
+ end
38
+
39
+ end
40
+
41
+ class Add < Command
42
+ parameter '[ITEM] ...', 'item to add', attribute_name: 'items'
43
+
44
+ option ['-t', '--timeout'], 'TIMEOUT', 'How long to wait for stdin (in seconds).',
45
+ environment_variable: 'TELEPATH_TIMEOUT', default: 1
46
+
47
+ def execute
48
+ container = 'stack'
49
+ values = self.items || Array.new
50
+
51
+ values << read_stdin if stdin?
26
52
 
27
53
  Out.error self, "No values supplied!" if values.empty?
28
54
 
29
- results = values.map do |value|
30
- handler.add value
55
+ values.each do |value|
56
+ handler.add value, container
57
+ # check that value was added correctly, individually?
31
58
  end
32
59
 
33
- success = results.all?{|r|r.first}
34
- name = results.first.last
60
+ info values, container
61
+ end
35
62
 
36
- if success then
37
- Out.info "Added [#{values.map(&:inspect).join ', '}] to `#{name}'!"
38
- else
39
- failures = values.reject.with_index{|_, i| results[i].first}
40
- Out.error self, "Could not add [#{failures.map().join ', '}] to `#{name}'!"
63
+ def info values, container
64
+ value_text = values.length == 1 ? values.first : "[#{values.join ', '}]"
65
+ Out.info "Added #{value_text} to #{container}!"
66
+ end
67
+
68
+ def read_stdin
69
+ $stdin.read.strip if stdin?
70
+ end
71
+
72
+ def stdin?
73
+ if not $stdin.tty? then
74
+ buffered = IO.select([$stdin], [], [], timeout)
75
+ buffered && buffered.first && buffered.first.first
41
76
  end
42
77
  end
78
+
79
+ # check that values were added, as a group?
80
+ #def verify_values_added values
81
+ # failures = values.reject.with_index{|_, i| results[i].first}
82
+ # Out.error self, "Could not add [#{failures.map().join ', '}] to `#{container}'!"
83
+ #end
43
84
  end
44
85
 
45
- subcommand ['?', 'lookup'], 'Look up item by pattern' do
86
+ class Lookup < Command
46
87
  parameter '[PATTERN]', 'pattern to find'
47
88
 
48
89
  def execute
49
- handler = Telepath::Handler.new self
50
- value = handler.lookup pattern
51
-
52
- if value && !value.empty? then
53
- Out.data value
54
- else
55
- Out.error self, "Pattern `#{pattern}' not matched."
56
- end
90
+ data_out handler.lookup(pattern),
91
+ "Pattern `#{pattern}' not matched."
57
92
  end
58
93
  end
59
94
 
60
- subcommand ['$', 'last'], 'Get most recent item' do
61
- def execute
62
- handler = Telepath::Handler.new self
63
- value = handler.last
64
-
65
- if value && !value.empty? then
66
- Out.data value
67
- else
68
- Out.error self, "Telepath is empty, is your storage location configured properly?"
95
+ class Last < Command
96
+ parameter '[COUNT]', 'number of most recent items to get',
97
+ default: 1 do |c|
98
+ Integer c
69
99
  end
100
+
101
+ def execute
102
+ data_out handler.last(count),
103
+ "Telepath is empty, is your storage location configured properly?"
70
104
  end
71
105
  end
72
106
 
73
- subcommand ['@', 'index'], 'Get item from (reverse) index' do
107
+ class Index < Command
74
108
  parameter 'INDEX ...', 'index of item, starting from most recent (0)',
75
109
  attribute_name: :indicies do |i|
76
- Integer(i)
110
+ Integer i
77
111
  end
78
112
 
79
113
  def execute
80
- handler = Telepath::Handler.new self
81
- index = indicies.first
82
- value = handler.index index
83
-
84
- if value && !value.to_s.empty? then
85
- Out.data value
86
- else
87
- Out.error self, "Hmm, couldn't find anything at that index."
88
- end
114
+ data_out handler.index(*indicies),
115
+ "Hmm, couldn't find anything at that index."
89
116
  end
90
117
  end
91
118
 
92
- def execute
93
- exit 1
119
+ class List < Command
120
+ parameter '[CONTAINER] ...', 'container to list contents of',
121
+ attribute_name: :containers
122
+
123
+ def execute
124
+ data_out handler.list(*containers),
125
+ "Container empty (or no container with that name)."
126
+ end
94
127
  end
95
128
 
129
+ class Main < Command
130
+ subcommand ['+', 'add'], 'Add item', Add
131
+ subcommand ['?', 'lookup'], 'Look up item by pattern', Lookup
132
+ subcommand ['$', 'last'], 'Get most recent item', Last
133
+ subcommand ['@', 'index'], 'Get item from (reverse) index', Index
134
+ subcommand ['list'], 'List known containers and contents', List
135
+ end
96
136
  end
97
137
  end
@@ -4,45 +4,47 @@ module Telepath
4
4
  @command = command
5
5
  end
6
6
 
7
- def add value
8
- name = 'stack'
9
-
7
+ def add value, name = 'stack'
10
8
  with_store name do |container|
11
9
  container << value
12
10
  end
13
-
14
- [true, name]
15
11
  end
16
12
 
17
- def last
13
+ def last count = nil
18
14
  with_store 'stack' do |container|
19
- container.last
15
+ if count.nil? then
16
+ container.last
17
+ else
18
+ container.last count
19
+ end
20
20
  end
21
21
  end
22
22
 
23
23
  def lookup pattern
24
24
  with_store 'stack' do |container|
25
-
26
- if pattern && !pattern.empty? then
27
- if container.include? pattern.to_s then
28
- pattern.to_s
29
- else
30
- container.find do |element|
31
- /#{pattern}/ =~ element.to_s
32
- end
33
- end
34
- else
35
- container.last
25
+ container.find do |element|
26
+ /#{pattern}/ =~ element.to_s
36
27
  end
37
-
38
28
  end
39
29
  end
40
30
 
41
31
  def index *indicies
42
- index = indicies.first
43
-
44
32
  with_store 'stack' do |container|
45
- container[-(index.to_i + 1)]
33
+ indicies.map do |index|
34
+ container[-(index.to_i + 1)]
35
+ end
36
+ end
37
+ end
38
+
39
+ def list *containers
40
+ c = containers.first
41
+
42
+ if c then
43
+ with_store c do |container|
44
+ rev_array container
45
+ end
46
+ else
47
+ rev_array storage.store.adapter.backend.keys
46
48
  end
47
49
  end
48
50
 
@@ -56,15 +58,23 @@ module Telepath
56
58
 
57
59
  protected
58
60
 
61
+ def rev_array list_of_things
62
+ Array(list_of_things).reverse
63
+ end
64
+
59
65
  def with_store name = 'stack'
60
66
  container = storage.store[name] || Array.new
61
67
 
62
68
  result = yield container
63
69
 
64
- storage.store[name] = container
70
+ storage.store[name] = container if present? container
65
71
  result
66
72
  ensure
67
73
  storage.store.close
68
74
  end
75
+
76
+ def present? value
77
+ !!value && ((value.respond_to?(:empty?) || true) && !value.empty?)
78
+ end
69
79
  end
70
80
  end
@@ -1,3 +1,3 @@
1
1
  module Telepath
2
- VERSION = '0.0.3'
2
+ VERSION = '0.1.0'
3
3
  end
data/spec/exe_spec.rb CHANGED
@@ -5,6 +5,10 @@ describe 'Telepath Executable' do
5
5
  let(:status){ exe.status }
6
6
  let(:stdout){ exe.stdout.strip }
7
7
 
8
+ let(:handler){ Telepath::Handler.new double(Clamp::Command) }
9
+ let(:value){ '12' }
10
+ let(:next_value){ 'Boo yah!' }
11
+
8
12
  # override these in context
9
13
  let(:command){ '' }
10
14
  let(:args){ [] }
@@ -39,19 +43,31 @@ describe 'Telepath Executable' do
39
43
  specify { expect(status).to be_success }
40
44
 
41
45
  it 'adds the value to the stack' do
42
- expect(stdout).to eq("Added [\"12\"] to `stack'!")
46
+ expect(stdout).to eq("Added 12 to stack!")
43
47
  end
44
48
 
45
49
  it 'should have no error output' do
46
50
  expect(exe.stderr).to eq ''
47
51
  end
48
52
  end
53
+
54
+ context 'with multiple values' do
55
+ let(:args){ ['12', 'foo/bar'] }
56
+
57
+ it 'adds the value to the stack' do
58
+ expect(stdout).to eq("Added [12, foo/bar] to stack!")
59
+ end
60
+ end
61
+
62
+ context 'redirected input' do
63
+ xit 'reads and stores stdin' do
64
+ # how to test?
65
+ end
66
+ end
49
67
  end
50
68
 
51
69
  describe '? lookup' do
52
70
  let(:command){ '?' }
53
- let(:handler){ Telepath::Handler.new double(Clamp::Command) }
54
- let(:value){ '12' }
55
71
 
56
72
  before do
57
73
  handler.add value
@@ -59,7 +75,6 @@ describe 'Telepath Executable' do
59
75
 
60
76
  context 'without parameters' do
61
77
  context 'with values in telepath' do
62
-
63
78
  specify { expect(status).to be_success }
64
79
  specify { expect(stdout).to eq value }
65
80
  end
@@ -72,4 +87,91 @@ describe 'Telepath Executable' do
72
87
  specify { expect(stdout).to eq value }
73
88
  end
74
89
  end
90
+
91
+ describe '@ index' do
92
+ let(:command){ '@' }
93
+
94
+ before do
95
+ handler.add value
96
+ handler.add next_value
97
+ end
98
+
99
+ context 'with single index' do
100
+ let(:args){ [1] }
101
+
102
+ it 'returns only that item' do
103
+ expect(stdout).to eq value
104
+ end
105
+ end
106
+
107
+ context 'with indicies' do
108
+ let(:args){ [0, 1] }
109
+
110
+ it 'returns each index on a new line' do
111
+ expect(stdout).to eq %Q{Boo yah!\n12}
112
+ end
113
+ end
114
+ end
115
+
116
+ describe '$ last' do
117
+ let(:command){ '$' }
118
+
119
+ before do
120
+ handler.add value
121
+ handler.add next_value
122
+ end
123
+
124
+ context 'without parameters' do
125
+ context 'with values in telepath' do
126
+ specify { expect(status).to be_success }
127
+ specify { expect(stdout).to eq next_value }
128
+ end
129
+ end
130
+
131
+ context 'with parameters' do
132
+ let(:args){ ['2'] }
133
+
134
+ specify { expect(status).to be_success }
135
+
136
+ it 'returns each most recent item on a new line' do
137
+ expect(stdout).to eq %Q{12\nBoo yah!}
138
+ end
139
+ end
140
+ end
141
+
142
+ describe 'list' do
143
+ let(:command){ 'list' }
144
+
145
+ before do
146
+ handler.add value
147
+ handler.add next_value
148
+ handler.add 'something else', 'another container'
149
+ end
150
+
151
+ context 'without parameters' do
152
+ it 'lists known containers, most recent first' do
153
+ expect(stdout).to eq "another container\nstack"
154
+ end
155
+ end
156
+
157
+ context 'with contaienr specified' do
158
+ let(:args){ ['"another container"'] }
159
+
160
+ it 'lists the contents of the specified container' do
161
+ expect(stdout).to eq 'something else'
162
+ end
163
+ end
164
+
165
+ context 'with nonexistant container' do
166
+ let(:args){ ['"totally does not exist"'] }
167
+ let(:container_list_exe){ run './bin/tel list' }
168
+ let(:container_list){ container_list_exe.stdout.strip }
169
+
170
+ it 'does not create listed containers' do
171
+ expect(stdout).to eq ''
172
+ expect(container_list).to eq "another container\nstack"
173
+ end
174
+ end
175
+ end
176
+
75
177
  end
data/spec/handler_spec.rb CHANGED
@@ -8,9 +8,12 @@ describe Telepath::Handler do
8
8
 
9
9
  let(:storage){ Telepath::Storage.new }
10
10
  let(:value){ 'whatever' }
11
+ let(:next_value){ 'all your bass' }
12
+ let(:default_container){ 'stack' }
11
13
 
12
14
  before do
13
15
  handler.add value
16
+ handler.add next_value
14
17
  end
15
18
 
16
19
  it 'exists' do
@@ -22,11 +25,12 @@ describe Telepath::Handler do
22
25
  end
23
26
 
24
27
  describe '#add' do
28
+
25
29
  it 'adds 1 item' do
26
30
  expect{ handler.add 'something' }.to change{
27
31
  storage.store.adapter.backend.sunrise
28
32
  storage.stack.length
29
- }.from(1).to(2)
33
+ }.from(2).to(3)
30
34
  end
31
35
 
32
36
  it 'adds the right item' do
@@ -34,52 +38,64 @@ describe Telepath::Handler do
34
38
  expect(storage.stack).to include('something')
35
39
  end
36
40
 
37
- context 'redirected input' do
38
- xit 'reads and stores stdin' do
41
+ context 'with container specified' do
42
+ let(:container){ 'keepsafe' }
43
+ let(:not_in_stack){ 'are belong to us' }
44
+
45
+ before do
46
+ handler.add not_in_stack, container
47
+ end
48
+
49
+ it do
50
+ expect(handler.list container).to include not_in_stack
51
+ end
52
+
53
+ it do
54
+ expect(handler.list).not_to include not_in_stack
39
55
  end
40
56
  end
57
+
41
58
  end
42
59
 
43
60
  describe '#last' do
44
61
  it 'returns the last value' do
45
- expect(handler.last).to eq(value)
62
+ expect(handler.last).to eq(next_value)
46
63
  end
47
64
 
48
65
  it 'does not delete the item' do
49
- expect{ handler.last }.to change{
50
- storage.store.adapter.backend.sunrise
51
- storage.stack.length
52
- }.by(0)
66
+ store_unchanged { handler.last }
53
67
  end
54
- end
55
68
 
56
- describe '#index' do
57
- before do
58
- handler.add 'blah'
69
+ context 'with a count' do
70
+ it 'returns the count last items' do
71
+ expect(handler.last 2).to eq ["whatever", "all your bass"]
72
+ end
59
73
  end
74
+ end
60
75
 
76
+ describe '#index' do
61
77
  it 'returns the item at the specified reverse index (1)' do
62
- expect(handler.index 1).to eq(value)
78
+ expect(handler.index 1).to eq [value]
63
79
  end
64
80
 
65
81
  it 'returns the item at the specified reverse index (0)' do
66
- expect(handler.index 0).to eq('blah')
82
+ expect(handler.index 0).to eq [next_value]
67
83
  end
68
84
 
69
85
  it 'does not delete the item' do
70
- expect{ handler.index value }.to change{
71
- storage.store.adapter.backend.sunrise
72
- storage.stack.length
73
- }.by(0)
86
+ store_unchanged { handler.index 1 }
87
+ end
88
+
89
+ context 'multiple indicies' do
90
+ it 'should return the items in the same order specified' do
91
+ expect(handler.index 0, 1).to eq [next_value, value]
92
+ end
74
93
  end
75
94
  end
76
95
 
77
96
  describe '#lookup' do
78
97
  it 'does not delete the item' do
79
- expect{ handler.lookup value }.to change{
80
- storage.store.adapter.backend.sunrise
81
- storage.stack.length
82
- }.by(0)
98
+ store_unchanged { handler.lookup value }
83
99
  end
84
100
 
85
101
  context 'pattern matching' do
@@ -96,4 +112,29 @@ describe Telepath::Handler do
96
112
  end
97
113
  end
98
114
  end
115
+
116
+ describe '#list' do
117
+
118
+ let(:other_container){ 'keepsafe' }
119
+
120
+ before do
121
+ handler.add 'not_in_stack', other_container
122
+ end
123
+
124
+ it 'does not alter the store' do
125
+ store_unchanged { handler.list; handler.list default_container }
126
+ end
127
+
128
+ context 'without specifying a container' do
129
+ it do
130
+ expect(handler.list).to eq([other_container, default_container])
131
+ end
132
+ end
133
+
134
+ context 'with container specified' do
135
+ it do
136
+ expect(handler.list default_container).to eq ['all your bass', 'whatever']
137
+ end
138
+ end
139
+ end
99
140
  end
data/spec/spec_helper.rb CHANGED
@@ -28,6 +28,13 @@ module SpecHelpers
28
28
  def unassign_test_path; ENV[path_env_var] = nil; end
29
29
  def pre_test_setup; assign_test_path; cleanup_db; end
30
30
  def post_test_teardown; unassign_test_path; cleanup_db; end
31
+
32
+ def store_unchanged
33
+ expect{ yield }.to change{
34
+ storage.store.adapter.backend.sunrise
35
+ storage.stack.length
36
+ }.by(0)
37
+ end
31
38
  end
32
39
 
33
40
  RSpec.configure do |c|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telepath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Cook
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-01 00:00:00.000000000 Z
11
+ date: 2013-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: moneta