pick 0.0.0 → 0.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 791aa79bc210597caff6be93c520612b86a1a10b
4
- data.tar.gz: c58d260ff2eeb1e20e87b1e5e768eb74f466973e
3
+ metadata.gz: 231a0858bec544792385a82e9111bdb7debc6860
4
+ data.tar.gz: 3178c3fb7fd85d4a5e0630e1df1fcb004d7fa08a
5
5
  SHA512:
6
- metadata.gz: 1cf79a751a99164de1aa1611a5970d2024f2bd8f9acf820abaa70f9d61aa4e88d5ca7b4977c8173d7105ab99cf557e2933f457e8f13df10822ce3cfcca305fca
7
- data.tar.gz: 310ea997c21866e428138b59657b5caa7375eee4846b055eda42055b79a46bd06af777f1378d4c807556fd215c36328fded8ce1956920f43b242376d6f79bf2b
6
+ metadata.gz: ff367787f4196c730c831eea89878df45c1f70091ac4725fb730522e45b8095e4a233214406c64067c570af6dec8f0164bbb01d1162baa6cd4cc6f13e10cc1af
7
+ data.tar.gz: 026d6830026c67daca200dc0f59934bc2c8dbf6c3244b88d83310547abc3222747e63c27acc99f22afcb6ce52a55343762270d9188fef04dc7bc3318b076732f
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Change Log
2
+ All notable changes to Sixword should be documented in this file.
3
+ This project adheres to [Semantic Versioning](http://semver.org).
4
+
5
+ ## [Unreleased]
6
+
7
+ ## [0.1.0] -- 2017-10-17
8
+
9
+ - Add most of the basic pick command line interface.
10
+
11
+ ## [0.0.0] -- 2017-10-15
12
+
13
+ - Initial public release
data/bin/pick CHANGED
@@ -3,8 +3,95 @@
3
3
  require 'bundler/setup'
4
4
  require 'pick'
5
5
 
6
+ require 'optparse'
7
+
8
+ # known escapes for delimiter
9
+ ESCAPES = {
10
+ '\0' => "\0",
11
+ '\a' => "\a",
12
+ '\b' => "\b",
13
+ '\e' => "\e",
14
+ '\f' => "\f",
15
+ '\n' => "\n",
16
+ '\r' => "\r",
17
+ '\s' => "\s",
18
+ '\t' => "\t",
19
+ '\v' => "\v",
20
+ }.freeze
21
+
6
22
  def main(args)
7
- raise NotImplementedError.new('TODO')
23
+ args = args.dup
24
+
25
+ options = {}
26
+
27
+ optparse = OptionParser.new do |opts|
28
+ opts.banner = <<-EOM
29
+ usage: #{File.basename($0)} [OPTION]... [FILE]
30
+
31
+ Interactively pick lines from stdin or FILE. This is useful in building
32
+ CLI pipelines for humans to select among several options.
33
+
34
+ By default, input and output items are terminated by a trailing newline (\\n).
35
+ Pass -0/--null to use a null character, compatible with \`find -print0\` or
36
+ \`xargs -0\`.
37
+
38
+ Options:
39
+ EOM
40
+
41
+ opts.on('-h', '--help', 'Display this message') do
42
+ STDERR.puts opts, ''
43
+ exit 0
44
+ end
45
+ opts.on('-v', '--version', 'Print version number', ' ') do
46
+ puts 'pick ' + Pick::VERSION
47
+ exit 0
48
+ end
49
+
50
+ opts.on('-m', '--multiple', 'Select multiple items from input') do
51
+ options[:multiple] = true
52
+ end
53
+
54
+ opts.on('-d', '--delimiter DELIM',
55
+ 'Split input on delimiter (default \n)') do |arg|
56
+ if ESCAPES.include?(arg)
57
+ arg = ESCAPES.fetch(arg)
58
+ end
59
+ options[:input_delimiter] = arg
60
+ end
61
+
62
+ opts.on('-0', '--null', 'Delimit input and output items by \0') do
63
+ options[:input_delimiter] = "\0"
64
+ options[:output_delimiter] = "\0"
65
+ end
66
+
67
+ opts.on('-c', '--clipboard', 'Copy output to the clipboard') do
68
+ options[:clipboard] = true
69
+ raise NotImplementedError.new
70
+ end
71
+
72
+ opts.on('--tty DEV', 'Set DEV as TTY device instead of /dev/tty') do |arg|
73
+ options[:tty_dev] = arg
74
+ end
75
+ end
76
+
77
+ optparse.parse!(args)
78
+
79
+ case args.length
80
+ when 0
81
+ file = STDIN
82
+ when 1
83
+ file = File.open(args.fetch(0), 'r')
84
+ else
85
+ puts optparse
86
+ exit 1
87
+ end
88
+
89
+ begin
90
+ Pick::CLI.run(file, options)
91
+ rescue TTY::Reader::InputInterrupt
92
+ STDERR.puts
93
+ exit 130
94
+ end
8
95
  end
9
96
 
10
97
  if $0 == __FILE__
data/lib/pick.rb CHANGED
@@ -2,3 +2,5 @@ require 'pick/version'
2
2
 
3
3
  module Pick
4
4
  end
5
+
6
+ require 'pick/cli'
data/lib/pick/cli.rb ADDED
@@ -0,0 +1,61 @@
1
+ require 'tty/prompt'
2
+ require 'tty/screen'
3
+
4
+ module Pick
5
+ class CLI
6
+ def self.run(input_io, options={})
7
+ answer = prompt(input_io, options)
8
+
9
+ output_io = options[:output_io] || STDOUT
10
+
11
+ out_sep = options[:output_delimiter]
12
+ out_sep ||= "\n"
13
+
14
+ output_io.print(Array(answer).join(out_sep) + out_sep)
15
+ end
16
+
17
+ def self.prompt(input_io, options={})
18
+ prompt_opts = {}
19
+ options = options.dup
20
+
21
+ tty_dev = options[:tty_dev] || '/dev/tty' # TODO windows?
22
+
23
+ options[:prompt_input] ||= File.open(tty_dev, 'r')
24
+ options[:prompt_output] ||= File.open(tty_dev, 'a')
25
+
26
+ options[:per_page] ||= TTY::Screen.height - 2
27
+ if options[:per_page] && options[:per_page] >= 1
28
+ prompt_opts[:per_page] = options.fetch(:per_page)
29
+ end
30
+
31
+
32
+ # read the input io
33
+ data = input_io.read
34
+
35
+ # set default separator
36
+ separator = options[:input_delimiter]
37
+ if separator.nil?
38
+ data.gsub!("\r\n", "\n")
39
+ separator = "\n"
40
+ end
41
+
42
+ # split input into choices
43
+ choices = data.split(separator)
44
+
45
+ prompt = options.fetch(:prompt, 'Pick an option:')
46
+
47
+ p = TTY::Prompt.new(
48
+ input: options.fetch(:prompt_input),
49
+ output: options.fetch(:prompt_output)
50
+ )
51
+
52
+ if options[:multiple]
53
+ prompt = options.fetch(:prompt, 'Select multiple items:')
54
+ p.multi_select(prompt, choices, prompt_opts)
55
+ else
56
+ prompt = options.fetch(:prompt, 'Select an item:')
57
+ p.select(prompt, choices, prompt_opts)
58
+ end
59
+ end
60
+ end
61
+ end
data/lib/pick/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pick
2
- VERSION = "0.0.0"
2
+ VERSION = '0.1.0'.freeze
3
3
  end
data/pick.gemspec CHANGED
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.require_paths = ['lib']
26
26
 
27
27
  spec.add_dependency('tty-prompt', '~> 0.13')
28
+ spec.add_dependency('tty-screen', '~> 0.5')
28
29
 
29
30
  spec.add_development_dependency 'bundler', '~> 1.14'
30
31
  spec.add_development_dependency 'rake', '~> 10.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Brody
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-15 00:00:00.000000000 Z
11
+ date: 2017-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tty-prompt
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: tty-screen
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -96,12 +110,14 @@ files:
96
110
  - ".gitignore"
97
111
  - ".rspec"
98
112
  - ".travis.yml"
113
+ - CHANGELOG.md
99
114
  - Gemfile
100
115
  - LICENSE.txt
101
116
  - README.md
102
117
  - Rakefile
103
118
  - bin/pick
104
119
  - lib/pick.rb
120
+ - lib/pick/cli.rb
105
121
  - lib/pick/version.rb
106
122
  - pick.gemspec
107
123
  homepage: https://github.com/ab/pick