file_selector 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/file_selector.rb +68 -0
  3. metadata +43 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 58b0cfadf9061aac47af8afc9b6c203e11377aeb2b4f6697436e74a81b374f21
4
+ data.tar.gz: 030e2a1a84e238bd6b5ac68e8dc4893a80c465a86df814d68ac33b7736bf1ba3
5
+ SHA512:
6
+ metadata.gz: 4a96664a2911b778804ed5774714394627fe2d04c02eeb6868392a2403cc59c3b3ce7134069c0871263cc2d4810cbb5c35098544c153b5ba0d8671022f0ea604
7
+ data.tar.gz: 2a251d73df710a45be48723a20dde19ee0b735201e8295ae09d56759545d9b22c9707c79876c3a5be957d3a256f15d08c55053baa4ac670bca8c6ecee14bedc0
@@ -0,0 +1,68 @@
1
+ class FileSelector
2
+ require 'tty-prompt'
3
+ require 'set'
4
+
5
+ def self.explore
6
+ new.explore
7
+ end
8
+
9
+ def initialize
10
+ @files = Set[]
11
+ @prompt = TTY::Prompt.new
12
+ end
13
+
14
+ def explore
15
+ explore_mode
16
+ @files
17
+ end
18
+
19
+ def explore_mode(path = Dir.pwd)
20
+ system 'clear'
21
+ files = files_in path
22
+ options = ['finish', 'include', File.dirname(path)] + files
23
+ prompt = 'Files and directoies to explore',
24
+ choice = @prompt.select(prompt, options, per_page: 20)
25
+ if choice == 'include'
26
+ include_mode(path)
27
+ explore_mode(path)
28
+ elsif choice != 'finish'
29
+ if File.directory?(choice)
30
+ explore_mode(choice) if File.directory?(choice)
31
+ else
32
+ system "less #{choice}" unless File.directory?(choice)
33
+ explore_mode(path)
34
+ end
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def include_mode(dir)
41
+ system 'clear'
42
+ cwd_files = files_in dir
43
+ prompt = 'Files and directoies to include'
44
+ choices = @prompt.multi_select(prompt, [dir] + cwd_files, per_page: 20)
45
+ @files.merge(paths_to_files(choices))
46
+ end
47
+
48
+ def paths_to_files(paths)
49
+ # Converts a set of paths to non-directory files and directories to a set
50
+ # of non-directory files by replacing directories with the files therein
51
+ files = Set[]
52
+ paths.each do |path|
53
+ if !File.directory?(path)
54
+ files.add(path)
55
+ else
56
+ files_to_add = Dir.glob("#{path}/**/*").reject do |f|
57
+ File.directory?(f) || ['.', '..'].include?(f)
58
+ end
59
+ files.merge(files_to_add)
60
+ end
61
+ end
62
+ files
63
+ end
64
+
65
+ def files_in(dir)
66
+ (Dir[dir + '/.*'] + Dir[dir + '/*']) - ["#{dir}/.", "#{dir}/.."]
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: file_selector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Olav Fosse
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple gem for selecting files.
14
+ email: fosseolav@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/file_selector.rb
20
+ homepage: http://fossegr.im
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.1.2
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: A simple gem for selecting files.
43
+ test_files: []