hiiro 0.1.12 → 0.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/h-link +78 -18
  3. data/lib/hiiro/version.rb +1 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b4e36b4e41c8423664f340bf38dc96796bb459a9edb161b4c2e937837504bb23
4
- data.tar.gz: c4374d147f3ddb4620349697d14701e0c50c0e3ea74034aa0f9cad6eeae162c9
3
+ metadata.gz: 120a159138b041e06749ec822d31251a980ff168177a23e62d118ce64a9aa7a3
4
+ data.tar.gz: 6e4fbc826a0fc648bc1dd53da2165daf15b081875bce912ff6d8933ca56f5662
5
5
  SHA512:
6
- metadata.gz: 820400fc28e1137fce98dc50687c1aeb4543c9b072bca58419c9de5c133c057601e0a9c8b56ff8f6225c36c3e0531d3039cb0011a7bcb8b1bc64e604f0c2f256
7
- data.tar.gz: 0abdd6cde316418cbf24ea129cd9988f59561fd25c14e15ee7e4227f7d6a82be3a098e295a7ccde5a899580124af4e92e0a8bed3531f99c89c521c7c9976cc5c
6
+ metadata.gz: 0b943ab68f08a7db203c94c5d1f1b096e0ef0bcc3bdb30ba7556e49bd86cd5b398b87cf6181d676bf609870c11df3aaa124f74035fbc636566f3a21fcfe3a107
7
+ data.tar.gz: 4c13f38ad2c94c3fd20c537f6bed9e2bdd865ff09bc03603bf47142387fc340d5a54b5fde18eabfbd0e3a78b8b38bbab78c715f94b9073c185f903371b8e28de
data/bin/h-link CHANGED
@@ -3,10 +3,19 @@
3
3
  require 'yaml'
4
4
  require 'fileutils'
5
5
  require 'time'
6
-
6
+ require 'pry'
7
+ require 'tempfile'
7
8
  require "hiiro"
8
9
 
9
10
  LINKS_FILE = File.join(Dir.home, '.config/hiiro/links.yml')
11
+ LINK_TEMPLATE = {
12
+ 'url' => 'http://EXAMPLE',
13
+ 'description' => '',
14
+ 'shorthand' => nil,
15
+ 'created_at' => Time.now.iso8601,
16
+ }
17
+
18
+
10
19
 
11
20
  o = Hiiro.init(*ARGV, plugins: [Tmux, Pins], links_file: LINKS_FILE)
12
21
 
@@ -16,11 +25,50 @@ def ensure_links_file
16
25
  File.write(LINKS_FILE, [].to_yaml) unless File.exist?(LINKS_FILE)
17
26
  end
18
27
 
28
+ def hash_matches?(links, *args)
29
+ terms = args.map(&:downcase)
30
+
31
+ links.select do |line, link|
32
+ searchable = [
33
+ link['url'],
34
+ link['description'],
35
+ link['shorthand']
36
+ ].compact.join(' ').downcase
37
+
38
+ terms.all? { |term| searchable.include?(term) }
39
+ end
40
+ end
41
+
42
+ def link_matches?(link, *args)
43
+ terms = args.map(&:downcase)
44
+
45
+ searchable = [
46
+ link['url'],
47
+ link['description'],
48
+ link['shorthand']
49
+ ].compact.join(' ').downcase
50
+
51
+ terms.all? { |term| searchable.include?(term) }
52
+ end
53
+
19
54
  def load_links
20
55
  ensure_links_file
21
56
  YAML.load_file(LINKS_FILE) || []
22
57
  end
23
58
 
59
+ def edit_link(link=LINK_TEMPLATE)
60
+ tmpfile = Tempfile.new(['link-edit-', '.yml'])
61
+ tmpfile.write(link.to_yaml)
62
+ tmpfile.close
63
+
64
+ system(ENV['EDITOR'] || 'vim', tmpfile.path)
65
+
66
+ updated = YAML.load_file(tmpfile.path)
67
+ tmpfile.unlink
68
+
69
+ updated
70
+ end
71
+
24
72
  def save_links(links)
25
73
  ensure_links_file
26
74
  File.write(LINKS_FILE, links.to_yaml)
@@ -38,16 +86,20 @@ def find_link_by_ref(ref, links)
38
86
  [nil, nil]
39
87
  end
40
88
 
41
- o.add_subcmd(:save) do |*args|
89
+ o.add_subcmd(:add) do |*args|
90
+ links = load_links
91
+
42
92
  if args.empty?
43
- puts "Usage: h link save <url> [description...]"
44
- exit 1
93
+ updated = edit_link
94
+
95
+ links << updated
96
+ save_links(links)
97
+ exit 0
45
98
  end
46
99
 
47
100
  url = args.shift
48
101
  description = args.join(' ')
49
102
 
50
- links = load_links
51
103
  links << {
52
104
  'url' => url,
53
105
  'description' => description,
@@ -93,7 +145,7 @@ o.add_subcmd(:search) do |*args|
93
145
  link['shorthand']
94
146
  ].compact.join(' ').downcase
95
147
 
96
- terms.any? { |term| searchable.include?(term) }
148
+ terms.all? { |term| searchable.include?(term) }
97
149
  end
98
150
 
99
151
  if matches.empty?
@@ -115,14 +167,18 @@ o.add_subcmd(:select) do |*args|
115
167
  exit 1
116
168
  end
117
169
 
118
- lines = links.each_with_index.map do |link, idx|
170
+ lines = links.each_with_index.each_with_object({}) do |(link, idx), h|
119
171
  num = (idx + 1).to_s.rjust(3)
120
172
  desc = link['description'].to_s.empty? ? "" : " - #{link['description']}"
121
- "#{num}. #{link['url']}#{desc}"
173
+ h["#{num}. #{link['url']}#{desc}"] = link
174
+ end
175
+
176
+ if args.any?
177
+ lines = hash_matches?(lines, *args)
122
178
  end
123
179
 
124
180
  require 'open3'
125
- selected, status = Open3.capture2('sk', stdin_data: lines.join("\n"))
181
+ selected, status = Open3.capture2('sk', stdin_data: lines.keys.join("\n"))
126
182
 
127
183
  if status.success? && !selected.strip.empty?
128
184
  if selected =~ /^\s*(\d+)\.\s+(\S+)/
@@ -131,6 +187,18 @@ o.add_subcmd(:select) do |*args|
131
187
  end
132
188
  end
133
189
 
190
+ o.add_subcmd(:editall) do |*args|
191
+ links_before = load_links
192
+ system(ENV['EDITOR'] || 'vim', LINKS_FILE)
193
+
194
+ begin
195
+ links_after = load_links
196
+ rescue => e
197
+ puts "ERROR: Unable to read updated file...reverting."
198
+ save_links(links_before)
199
+ end
200
+ end
201
+
134
202
  o.add_subcmd(:edit) do |*args|
135
203
  if args.empty?
136
204
  puts "Usage: h link edit <number|shorthand>"
@@ -145,15 +213,7 @@ o.add_subcmd(:edit) do |*args|
145
213
  exit 1
146
214
  end
147
215
 
148
- require 'tempfile'
149
- tmpfile = Tempfile.new(['link-edit-', '.yml'])
150
- tmpfile.write(link.to_yaml)
151
- tmpfile.close
152
-
153
- system(ENV['EDITOR'] || 'vim', tmpfile.path)
154
-
155
- updated = YAML.load_file(tmpfile.path)
156
- tmpfile.unlink
216
+ updated = edit_link(link)
157
217
 
158
218
  links[idx] = updated
159
219
  save_links(links)
data/lib/hiiro/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Hiiro
2
- VERSION = "0.1.12"
2
+ VERSION = "0.1.14"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiiro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Toyota