nknm 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f7682d91b7519b141a17817c2c54d7c0396c52bb5a94a7a551fdc0d82a425907
4
+ data.tar.gz: f888e6194163c955f14f1057f45be5a18ebc473daa03359e5f83b0b8aa251489
5
+ SHA512:
6
+ metadata.gz: 341035cde6c5c19f769e353e7fb830cdde3058dccae2b61b31319f31671b863e0b43fb95608d33fda0419f199589f2d3bb7a630d6f2d5b794cfa1cedf54a7429
7
+ data.tar.gz: 8e5f1bf38bc3d4801d91dff1ea93bf7d777e73e3331ad1993e6a313cc50cbca5d91a7721528915c639fa12a7e6ca812b6b4438632c4b596c1444ff8addc4ca78
@@ -0,0 +1,31 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ jobs:
8
+ build:
9
+ name: Build + Publish
10
+ runs-on: ubuntu-latest
11
+ permissions:
12
+ contents: read
13
+ packages: write
14
+
15
+ steps:
16
+ - uses: actions/checkout@v2
17
+ - name: Set up Ruby 2.6
18
+ uses: actions/setup-ruby@v1
19
+ with:
20
+ ruby-version: 2.6.x
21
+
22
+ - name: Publish to RubyGems
23
+ run: |
24
+ mkdir -p $HOME/.gem
25
+ touch $HOME/.gem/credentials
26
+ chmod 0600 $HOME/.gem/credentials
27
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
28
+ gem build *.gemspec
29
+ gem push *.gem
30
+ env:
31
+ GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
@@ -0,0 +1,34 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ main ]
13
+ pull_request:
14
+ branches: [ main ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+ strategy:
21
+ matrix:
22
+ ruby-version: ['2.6', '2.7', '3.0']
23
+
24
+ steps:
25
+ - uses: actions/checkout@v2
26
+ - name: Set up Ruby
27
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
28
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
29
+ uses: ruby/setup-ruby@v1
30
+ with:
31
+ ruby-version: ${{ matrix.ruby-version }}
32
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
33
+ - name: Run tests
34
+ run: bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ **.DS_Store
2
+ *.gem
3
+ Gemfile.lock
4
+ .bundle
5
+ .yardoc
6
+ doc
7
+ coverage
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --protected lib/**/*.rb
@@ -0,0 +1,76 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, sex characteristics, gender identity and expression,
9
+ level of experience, education, socio-economic status, nationality, personal
10
+ appearance, race, religion, or sexual identity and orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at kevinstuffandthings@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72
+
73
+ [homepage]: https://www.contributor-covenant.org
74
+
75
+ For answers to common questions about this code of conduct, see
76
+ https://www.contributor-covenant.org/faq
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2021 Kevin McDonald
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # Nknm ![Build Status](https://github.com/kevinstuffandthings/nknm/actions/workflows/ruby.yml/badge.svg) [![Gem Version](https://badge.fury.io/rb/nknm.svg)](https://badge.fury.io/rb/nknm)
2
+
3
+ Select from a list of items using easy-to-type nicknames.
4
+
5
+ ## Installation
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ # update with the version of your choice
10
+ gem 'nknm'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ $ gem install nknm
23
+ ```
24
+
25
+ ## Usage
26
+ Require the gem in your code:
27
+ ```ruby
28
+ require "nknm"
29
+ ```
30
+
31
+ You can use a static list of names:
32
+ ```ruby
33
+ # source your names from a known list, or from a file, or anything...
34
+ options = Nknm::Lists::Names.new(%w[First Second Third Fourth Fifth])
35
+
36
+ nickname = Nknm::Nickname.new("fh")
37
+ options.matches_for(nickname).map(&:to_s)
38
+ # => ["Fourth", "Fifth"]
39
+
40
+ nickname = Nknm::Nickname.new("s")
41
+ options.matches_for(nickname).map(&:to_s)
42
+ # => ["Second"]
43
+ ```
44
+
45
+ If you want to choose items from a directory, you can do that too:
46
+ ```ruby
47
+ # try named params like `include_subdirs` or `include_files` (both default to true)
48
+ options = Nknm::Lists::Directory.new("/Users/kevin/Development")
49
+ ```
50
+
51
+ #### Selectors
52
+ Using a terminal, and getting back multiple selections? Use a `Selector`:
53
+ ```ruby
54
+ options = Nknm::Lists::Names.new(%w[First Second Third Fourth Fifth])
55
+ nickname = Nknm::Nickname.new("f")
56
+ matches = options.matches_for(nickname)
57
+
58
+ selection = Nknm::List::Selectors::Terminal.build(matches).selection
59
+ puts "You selected: #{selection}" if selection
60
+ ```
61
+
62
+ You'll be prompted with:
63
+ ```
64
+ 1) First
65
+ 2) Fourth
66
+ 3) Fifth
67
+ ?
68
+ ```
69
+
70
+ Enter the number of your selection at the `?` prompt, and you'll have your match!
71
+
72
+ # Why?
73
+ As a terminal user and microservice engineer, I jump back-and-forth between a lot of different projects.
74
+ I have a single directory that holds about 60 different git repos, and I like to be able to jump to any one of them from anywhere,
75
+ without having to set up a ton of aliases, or type out full names.
76
+
77
+ So, in my `~/.bashrc`, I might set up something like this:
78
+ ```bash
79
+ export GIT_REPO_BASEDIR=/Users/kevin/Development
80
+
81
+ function nd
82
+ {
83
+ local dir=$(nknm-dir -F $GIT_REPO_BASEDIR $1)
84
+ if [ -n "$dir" ]
85
+ then
86
+ cd $dir
87
+ else
88
+ echo "No matching directory found" >&2
89
+ return 1
90
+ fi
91
+ }
92
+ export -f nd
93
+ ```
94
+
95
+ Then, from my terminal prompt, I can be whisked away to the directory of my choosing:
96
+ ```bash
97
+ -(git:main)-(16:57:28)-(~/Development/nknm/bin)-
98
+ $ nd dar
99
+ 1) /Users/kevin/Development/darryl
100
+ 2) /Users/kevin/Development/docker-library
101
+ ? 1
102
+ -(git:features/overlaps2)-(16:57:44)-(~/Development/darryl)-
103
+ $
104
+ ```
105
+
106
+ And when there's a single match for the nickname, no additional prompting is required.
107
+
108
+ # Problems?
109
+ Please submit an [issue](https://github.com/kevinstuffandthings/nknm/issues).
110
+ We'll figure out how to get you up and running with Nknm as smoothly as possible.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/setup"
4
+ require "bundler/gem_tasks"
5
+ require "standard/rake"
6
+
7
+ Dir.glob("lib/tasks/*.rake").each { |r| load r }
data/bin/nknm-dir ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "nknm"
4
+ require "optparse"
5
+
6
+ options = {include_files: true, include_subdirs: true}
7
+ OptionParser.new do |opts|
8
+ opts.banner = "Usage: #{File.basename($0)} [options] DIRECTORY NICKNAME"
9
+
10
+ opts.on("-F", "--no-files", "Avoid files") { options[:include_files] = false }
11
+ opts.on("-D", "--no-subdirs", "Avoid sub-directories") { options[:include_subdirs] = false }
12
+ end.parse!
13
+
14
+ if ARGV.length == 2
15
+ directory = Nknm::Lists::Directory.new(ARGV[0])
16
+ nickname = Nknm::Nickname.new(ARGV[1])
17
+ matches = directory.matches_for(nickname)
18
+ puts Nknm::List::Selectors::Terminal.build(matches).selection
19
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nknm
4
+ class List
5
+ class Matches
6
+ include Enumerable
7
+ attr_reader :items
8
+
9
+ def initialize(items = nil)
10
+ @items = Array(items).compact
11
+ end
12
+
13
+ def each
14
+ items.each { |i| yield i }
15
+ end
16
+
17
+ # Is this set of matches empty?
18
+ # @return [Boolean]
19
+ def empty?
20
+ items.length == 0
21
+ end
22
+
23
+ # Does this set of matches contain a single item?
24
+ # @return [Boolean]
25
+ def single?
26
+ items.length == 1
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nknm
4
+ class List
5
+ class Selector
6
+ UnselectableItemsError = Class.new(StandardError)
7
+
8
+ # Build the desired selector for the specified items.
9
+ # @param items [Array, Nknm::List::Matches]
10
+ # @return [Selector]
11
+ def self.build(items)
12
+ matches = if items.is_a?(Array)
13
+ Nknm::List::Matches.new(items)
14
+ elsif items.is_a?(Nknm::List::Matches)
15
+ items
16
+ end
17
+ raise UnselectableItemsError if matches.nil?
18
+
19
+ selector = matches.single? || matches.empty? ? Nknm::List::Selector : self
20
+ selector.new(matches.items)
21
+ end
22
+
23
+ attr_reader :items
24
+
25
+ # Get the resolved selection from the items.
26
+ # @return [Object] the first item in the list
27
+ def selection
28
+ items.first
29
+ end
30
+
31
+ protected
32
+
33
+ def initialize(items)
34
+ @items = items
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ %w[terminal].each { |f| require_relative "./selectors/#{f}" }
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nknm
4
+ class List
5
+ module Selectors
6
+ class Terminal < Nknm::List::Selector
7
+ # Generate a menu within the terminal that allows a user to
8
+ # select the desired item from the list.
9
+ # @return [Object] the user-selected option
10
+ def selection
11
+ items.each_with_index { |s, i| stream.puts "#{i + 1}) #{s}" }
12
+ stream.write "? "
13
+ idx = $stdin.readline.to_i - 1
14
+ items[idx] if idx >= 0
15
+ end
16
+
17
+ private
18
+
19
+ def stream
20
+ @_stream ||= $stderr
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
data/lib/nknm/list.rb ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nknm
4
+ class List
5
+ # :nocov:
6
+ def items
7
+ raise NotImplementedError
8
+ end
9
+ # :nocov:
10
+
11
+ # Get a set of matches for the specified nickname.
12
+ # @param nickname [Nknm::Nickname]
13
+ # @return [Matches]
14
+ def matches_for(nickname)
15
+ Matches.new(items.select { |i| nickname.for?(i.to_word) })
16
+ end
17
+ end
18
+ end
19
+
20
+ require_relative "./list/matches"
21
+ require_relative "./list/selector"
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nknm
4
+ module Lists
5
+ class Directory < Nknm::List
6
+ attr_reader :path
7
+
8
+ # Create a new list based on a filesystem directory.
9
+ # @param path [String] the directory path to examine
10
+ # @param include_files [Boolean] should files be included?
11
+ # @param include_subdirs [Boolean] should directories be included?
12
+ # @return [Directory]
13
+ def initialize(path, include_files: true, include_subdirs: true)
14
+ @path = path
15
+ @include_files, @include_subdirs = include_files, include_subdirs
16
+ end
17
+
18
+ # Get all the desired items from the specified directory.
19
+ # @return [Array<Entry>]
20
+ def items
21
+ @_items ||= Dir[File.join(path, "*")]
22
+ .select { |i| include?(i) }
23
+ .map { |i| Entry.new(i) }
24
+ end
25
+
26
+ private
27
+
28
+ def include?(item)
29
+ (@include_files || !File.file?(item)) && (@include_subdirs || !File.directory?(item))
30
+ end
31
+
32
+ class Entry
33
+ attr_reader :path
34
+ alias_method :to_s, :path
35
+
36
+ def initialize(path)
37
+ @path = path
38
+ end
39
+
40
+ # Turn this entry into a word matchable by a nickname.
41
+ # @return [String]
42
+ def to_word
43
+ @_to_word ||= File.basename(path).downcase
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nknm
4
+ module Lists
5
+ class Names < Nknm::List
6
+ # Create a new list based on an array of items.
7
+ # @param items [Array<String>]
8
+ # @return [Names]
9
+ def initialize(items)
10
+ @names = Array(items).compact
11
+ end
12
+
13
+ # Get all the items included in the list.
14
+ # @return [Array<Entry>]
15
+ def items
16
+ @_items ||= @names.map { |n| Entry.new(n) }
17
+ end
18
+
19
+ private
20
+
21
+ class Entry
22
+ attr_reader :name
23
+ alias_method :to_s, :name
24
+
25
+ def initialize(name)
26
+ @name = name
27
+ end
28
+
29
+ # Turn this entry into a word matchable by a nickname.
30
+ # @return [String]
31
+ def to_word
32
+ @_to_word ||= name.downcase
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
data/lib/nknm/lists.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ %w[directory names].each { |f| require_relative "./lists/#{f}" }
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nknm
4
+ class Nickname
5
+ attr_reader :text
6
+ alias_method :to_s, :text
7
+
8
+ def initialize(text)
9
+ @text = text
10
+ end
11
+
12
+ # Is this nickname suitable for the specified word?
13
+ # @param word [String]
14
+ # @return [Boolean]
15
+ def for?(word)
16
+ regex.match?(word)
17
+ end
18
+
19
+ private
20
+
21
+ def regex
22
+ @_regex ||= Regexp.compile("^#{(text || "").to_s.downcase.chars.join(".*")}")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nknm
4
+ VERSION = "0.0.1"
5
+ end
data/lib/nknm.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nknm/version"
4
+
5
+ # Nknm
6
+ module Nknm
7
+ end
8
+
9
+ %w[list lists nickname].each { |f| require_relative "./nknm/#{f}" }
data/nknm.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "nknm/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "nknm"
9
+ spec.version = Nknm::VERSION
10
+ spec.authors = ["Kevin McDonald"]
11
+ spec.email = ["kevinstuffandthings@gmail.com"]
12
+ spec.summary = "Select items from a list using easy-to-type nicknames"
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/kevinstuffandthings/nknm"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler"
23
+ spec.add_development_dependency "pry"
24
+ spec.add_development_dependency "pry-byebug"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "simplecov"
28
+ spec.add_development_dependency "standard"
29
+ spec.add_development_dependency "yard"
30
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nknm
4
+ class List
5
+ describe Matches do
6
+ context "empty" do
7
+ it "is considered empty" do
8
+ expect(subject).to be_empty
9
+ end
10
+
11
+ it "is not single" do
12
+ expect(subject).not_to be_single
13
+ end
14
+
15
+ it "has nothing to iterate over" do
16
+ count = 0
17
+ subject.each { |i| count += 1 }
18
+ expect(count).to eq 0
19
+ end
20
+ end
21
+
22
+ context "single" do
23
+ let(:item) { double }
24
+ subject { described_class.new(item) }
25
+
26
+ it "is not considered empty" do
27
+ expect(subject).not_to be_empty
28
+ end
29
+
30
+ it "is single" do
31
+ expect(subject).to be_single
32
+ end
33
+
34
+ it "can iterate over the one item" do
35
+ count = 0
36
+ subject.each do |i|
37
+ expect(item).to eq item
38
+ count += 1
39
+ end
40
+ expect(count).to eq 1
41
+ end
42
+ end
43
+
44
+ context "multiple" do
45
+ let(:items) { [double, double, double] }
46
+ subject { described_class.new(items) }
47
+
48
+ it "is not considered empty" do
49
+ expect(subject).not_to be_empty
50
+ end
51
+
52
+ it "is not single" do
53
+ expect(subject).not_to be_single
54
+ end
55
+
56
+ it "can iterate over the set of items" do
57
+ subject.each_with_index do |item, i|
58
+ expect(item).to eq items[i]
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nknm
4
+ class List
5
+ describe Selector do
6
+ describe "::build" do
7
+ let(:selector) { described_class }
8
+ let(:input) { items }
9
+ subject { selector.build(input) }
10
+
11
+ context "invalid items" do
12
+ let(:items) { double }
13
+
14
+ it "will raise an error if you attempt to build it with something unselectable" do
15
+ expect { subject }.to raise_error described_class::UnselectableItemsError
16
+ end
17
+ end
18
+
19
+ context "valid items" do
20
+ let(:selector) { Class.new(described_class) }
21
+
22
+ shared_examples_for("a selector with items") do
23
+ it "has the items it was passed, regardless of whether they were wrapped in matches or not" do
24
+ expect(subject.items).to eq items
25
+ end
26
+ end
27
+
28
+ shared_examples_for("a boring item selector") do
29
+ it "does not build the specific selector, because it doesn't need to" do
30
+ expect(subject).not_to be_a selector
31
+ end
32
+
33
+ it "builds a simple selector directly" do
34
+ expect(subject).to be_instance_of described_class
35
+ end
36
+
37
+ it_behaves_like "a selector with items"
38
+ end
39
+
40
+ shared_examples_for("a multi item selector") do
41
+ it "builds the specific selector" do
42
+ expect(subject).to be_a selector
43
+ end
44
+
45
+ it_behaves_like "a selector with items"
46
+ end
47
+
48
+ context "empty items" do
49
+ let(:items) { [] }
50
+
51
+ it_behaves_like "a boring item selector"
52
+
53
+ context "as matches" do
54
+ let(:input) { Nknm::List::Matches.new(items) }
55
+
56
+ it_behaves_like "a boring item selector"
57
+ end
58
+ end
59
+
60
+ context "single item" do
61
+ let(:items) { ["eight"] }
62
+
63
+ it_behaves_like "a boring item selector"
64
+
65
+ context "as matches" do
66
+ let(:input) { Nknm::List::Matches.new(items) }
67
+
68
+ it_behaves_like "a boring item selector"
69
+ end
70
+ end
71
+
72
+ context "multi item" do
73
+ let(:items) { %w[two four six] }
74
+
75
+ it_behaves_like "a multi item selector"
76
+
77
+ context "as matches" do
78
+ let(:input) { Nknm::List::Matches.new(items) }
79
+
80
+ it_behaves_like "a multi item selector"
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ describe "#selection" do
87
+ let(:items) { [] }
88
+ subject { described_class.send(:new, items) }
89
+
90
+ it "gives you nothing with an empty item list" do
91
+ expect(subject.selection).to be_nil
92
+ end
93
+
94
+ context "with items" do
95
+ let(:items) { %w[stuff things whatever] }
96
+
97
+ it "will just give you the first item" do
98
+ expect(subject.selection).to eq "stuff"
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nknm
4
+ class List
5
+ module Selectors
6
+ describe Terminal do
7
+ subject { described_class.build(%w[one two three four]) }
8
+
9
+ it "is a selector, through and through" do
10
+ expect(subject).to be_instance_of described_class
11
+ expect(subject).to be_a Nknm::List::Selector
12
+ end
13
+
14
+ describe "#selection" do
15
+ let(:stream) { $stderr }
16
+ before(:each) do
17
+ expect(stream).to receive(:puts).with("1) one").ordered
18
+ expect(stream).to receive(:puts).with("2) two").ordered
19
+ expect(stream).to receive(:puts).with("3) three").ordered
20
+ expect(stream).to receive(:puts).with("4) four").ordered
21
+ expect(stream).to receive(:write) do |prompt|
22
+ expect(prompt).to include("?")
23
+ expect(prompt).not_to include("\n")
24
+ end
25
+ end
26
+
27
+ it "returns no selection with an empty response" do
28
+ expect($stdin).to receive(:readline).and_return nil
29
+ expect(subject.selection).to be nil
30
+ end
31
+
32
+ it "returns no selection for an invalid response" do
33
+ expect($stdin).to receive(:readline).and_return "x"
34
+ expect(subject.selection).to be nil
35
+ end
36
+
37
+ it "returns the proper selection to correspond to the provided number" do
38
+ expect($stdin).to receive(:readline).and_return "3"
39
+ expect(subject.selection).to eq "three"
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nknm
4
+ describe List do
5
+ let(:item1) { double(to_word: "x1") }
6
+ let(:item2) { double(to_word: "y2") }
7
+ let(:item3) { double(to_word: "z3") }
8
+ before(:each) { allow(subject).to receive(:items).and_return [item1, item2, item3] }
9
+
10
+ describe "#matches_for" do
11
+ let(:nickname) { instance_double(Nknm::Nickname) }
12
+ let(:matches) { subject.matches_for(nickname) }
13
+
14
+ it "will build a list of matches for the items that match the nickname" do
15
+ expect(nickname).to receive(:for?).with("x1").and_return true
16
+ expect(nickname).to receive(:for?).with("y2").and_return false
17
+ expect(nickname).to receive(:for?).with("z3").and_return true
18
+
19
+ expect(matches).to be_a described_class::Matches
20
+ expect(matches.items).to match_array [item1, item3]
21
+ end
22
+ end
23
+ end
24
+ end
File without changes
File without changes
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nknm
4
+ module Lists
5
+ describe Directory do
6
+ let(:path) { File.join(File.dirname(__FILE__), "directory", "example") }
7
+ subject { described_class.new(path) }
8
+
9
+ describe "#items" do
10
+ let(:paths) { subject.items.map(&:to_s) }
11
+ let(:words) { subject.items.map(&:to_word) }
12
+
13
+ context "entire directory" do
14
+ it "finds all files+directories from the path" do
15
+ expect(paths).to match_array [
16
+ File.join(path, "File 1.txt"),
17
+ File.join(path, "file 2.txt"),
18
+ File.join(path, "directory_a"),
19
+ File.join(path, "Directory_B"),
20
+ File.join(path, "directory_c")
21
+ ]
22
+ end
23
+
24
+ it "can translate the entries into words" do
25
+ expect(words).to match_array [
26
+ "file 1.txt",
27
+ "file 2.txt",
28
+ "directory_a",
29
+ "directory_b",
30
+ "directory_c"
31
+ ]
32
+ end
33
+ end
34
+
35
+ context "files only" do
36
+ subject { described_class.new(path, include_subdirs: false) }
37
+
38
+ it "finds all files+directories from the path" do
39
+ expect(paths).to match_array [
40
+ File.join(path, "File 1.txt"),
41
+ File.join(path, "file 2.txt")
42
+ ]
43
+ end
44
+
45
+ it "can translate the entries into words" do
46
+ expect(words).to match_array [
47
+ "file 1.txt",
48
+ "file 2.txt"
49
+ ]
50
+ end
51
+ end
52
+
53
+ context "subdirectories only" do
54
+ subject { described_class.new(path, include_files: false) }
55
+
56
+ it "finds all files+directories from the path" do
57
+ expect(paths).to match_array [
58
+ File.join(path, "directory_a"),
59
+ File.join(path, "Directory_B"),
60
+ File.join(path, "directory_c")
61
+ ]
62
+ end
63
+
64
+ it "can translate the entries into words" do
65
+ expect(words).to match_array [
66
+ "directory_a",
67
+ "directory_b",
68
+ "directory_c"
69
+ ]
70
+ end
71
+ end
72
+
73
+ context "nothing" do
74
+ subject { described_class.new(path, include_files: false, include_subdirs: false) }
75
+
76
+ it "finds nothing at all" do
77
+ expect(paths).to be_empty
78
+ end
79
+
80
+ it "has no words" do
81
+ expect(words).to be_empty
82
+ end
83
+ end
84
+ end
85
+
86
+ describe "#matches_for" do
87
+ it "will respond to #matches_for, and that's good enough" do
88
+ expect(subject).to respond_to :matches_for
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nknm
4
+ module Lists
5
+ describe Names do
6
+ subject { described_class.new(["Flurp", "bloop bloop", "MITTENS!"]) }
7
+
8
+ describe "#items" do
9
+ let(:names) { subject.items.map(&:to_s) }
10
+ let(:words) { subject.items.map(&:to_word) }
11
+
12
+ it "returns all the names it was given" do
13
+ expect(names).to match_array [
14
+ "Flurp",
15
+ "bloop bloop",
16
+ "MITTENS!"
17
+ ]
18
+ end
19
+
20
+ it "can translate the entries into words" do
21
+ expect(words).to match_array [
22
+ "flurp",
23
+ "bloop bloop",
24
+ "mittens!"
25
+ ]
26
+ end
27
+ end
28
+
29
+ describe "#matches_for" do
30
+ it "will respond to #matches_for, and that's good enough" do
31
+ expect(subject).to respond_to :matches_for
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nknm
4
+ describe Nickname do
5
+ describe "#for?" do
6
+ {
7
+ tng: [%w[tngtng things tenalgrap tingy], %w[nothing tognar torg]],
8
+ s: [%w[ssss slim still sandy], %w[goop fsck t-shirt]]
9
+ }.each do |nickname, (yes, no)|
10
+ context "#{nickname}" do
11
+ subject { described_class.new(nickname) }
12
+
13
+ yes.each do |word|
14
+ it "matches #{word}" do
15
+ expect(subject.for?(word)).to be true
16
+ end
17
+ end
18
+
19
+ no.each do |word|
20
+ it "does not match #{word}" do
21
+ expect(subject.for?(word)).to be false
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "simplecov"
4
+ SimpleCov.start
5
+
6
+ require "nknm"
7
+
8
+ RSpec.configure do |config|
9
+ config.expect_with :rspec do |expectations|
10
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
11
+ end
12
+
13
+ config.mock_with :rspec do |mocks|
14
+ mocks.verify_partial_doubles = true
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,204 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nknm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kevin McDonald
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-12-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: standard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: yard
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Select items from a list using easy-to-type nicknames
126
+ email:
127
+ - kevinstuffandthings@gmail.com
128
+ executables:
129
+ - nknm-dir
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".github/workflows/gem-push.yml"
134
+ - ".github/workflows/ruby.yml"
135
+ - ".gitignore"
136
+ - ".rspec"
137
+ - ".yardopts"
138
+ - CODE_OF_CONDUCT.md
139
+ - Gemfile
140
+ - LICENSE.txt
141
+ - README.md
142
+ - Rakefile
143
+ - bin/nknm-dir
144
+ - lib/nknm.rb
145
+ - lib/nknm/list.rb
146
+ - lib/nknm/list/matches.rb
147
+ - lib/nknm/list/selector.rb
148
+ - lib/nknm/list/selectors/terminal.rb
149
+ - lib/nknm/lists.rb
150
+ - lib/nknm/lists/directory.rb
151
+ - lib/nknm/lists/names.rb
152
+ - lib/nknm/nickname.rb
153
+ - lib/nknm/version.rb
154
+ - nknm.gemspec
155
+ - spec/lib/nknm/list/matches_spec.rb
156
+ - spec/lib/nknm/list/selector_spec.rb
157
+ - spec/lib/nknm/list/selectors/terminal_spec.rb
158
+ - spec/lib/nknm/list_spec.rb
159
+ - spec/lib/nknm/lists/directory/example/Directory_B/.gitkeep
160
+ - spec/lib/nknm/lists/directory/example/File 1.txt
161
+ - spec/lib/nknm/lists/directory/example/directory_a/.gitkeep
162
+ - spec/lib/nknm/lists/directory/example/directory_c/.gitkeep
163
+ - spec/lib/nknm/lists/directory/example/file 2.txt
164
+ - spec/lib/nknm/lists/directory_spec.rb
165
+ - spec/lib/nknm/lists/names_spec.rb
166
+ - spec/lib/nknm/nickname_spec.rb
167
+ - spec/spec_helper.rb
168
+ homepage: https://github.com/kevinstuffandthings/nknm
169
+ licenses:
170
+ - MIT
171
+ metadata: {}
172
+ post_install_message:
173
+ rdoc_options: []
174
+ require_paths:
175
+ - lib
176
+ required_ruby_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ required_rubygems_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ requirements: []
187
+ rubygems_version: 3.0.3.1
188
+ signing_key:
189
+ specification_version: 4
190
+ summary: Select items from a list using easy-to-type nicknames
191
+ test_files:
192
+ - spec/lib/nknm/list/matches_spec.rb
193
+ - spec/lib/nknm/list/selector_spec.rb
194
+ - spec/lib/nknm/list/selectors/terminal_spec.rb
195
+ - spec/lib/nknm/list_spec.rb
196
+ - spec/lib/nknm/lists/directory/example/Directory_B/.gitkeep
197
+ - spec/lib/nknm/lists/directory/example/File 1.txt
198
+ - spec/lib/nknm/lists/directory/example/directory_a/.gitkeep
199
+ - spec/lib/nknm/lists/directory/example/directory_c/.gitkeep
200
+ - spec/lib/nknm/lists/directory/example/file 2.txt
201
+ - spec/lib/nknm/lists/directory_spec.rb
202
+ - spec/lib/nknm/lists/names_spec.rb
203
+ - spec/lib/nknm/nickname_spec.rb
204
+ - spec/spec_helper.rb