command_tree 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 15fb161d51f86da4d50b5c0748051e91373d0258a3c8459e124e238dd79b3cd8
4
+ data.tar.gz: 97f50232b6db9de2aeebaf01681911ab67570b98fe6a54953eaa982c7e83b9ef
5
+ SHA512:
6
+ metadata.gz: d7462dc4a273fdf456e879b9320a54222a204c9192efec5ae294a0113a6fb9cec96fb94b3fe8c472bf6676996f317c682181146609cbb419457dba4322706fbe
7
+ data.tar.gz: d2ed71c2a8b8f587d1fc5d623386a6773e80f370f0207bcd334fb11aa615b7fd7723aac67c5e87d4e1ab62c0e5f490f1ede8d79804d6f9c50590cb0d6a4aaecf
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in command_tree.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ command_tree (0.1.0)
5
+ colorize (~> 0.8)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ colorize (0.8.1)
11
+ minitest (5.11.3)
12
+ rake (10.5.0)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 1.16)
19
+ command_tree!
20
+ minitest (~> 5.0)
21
+ rake (~> 10.0)
22
+
23
+ BUNDLED WITH
24
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Emad Elsaid
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # CommandTree
2
+
3
+ Builds trees of commands for the terminal, each node is either a group of commands or the command itself, every node is associated with a character to access it.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'command_tree'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install command_tree
20
+
21
+ ## Usage
22
+
23
+ You start by creating a new tree
24
+
25
+ ```ruby
26
+ t = CommandTree::Tree.new
27
+ ```
28
+
29
+ Then you can register a command category (a node that contains commands)
30
+
31
+ ```ruby
32
+ t.register 'a', 'Applications' # associate the character 'a' to a category called 'applications'
33
+ ```
34
+
35
+ Then register commands inside that prefix
36
+ ```ruby
37
+ t.register 'ag','Google Chrome' do
38
+ system 'google-chrome-stable'
39
+ end
40
+ ```
41
+ the previous block will be assigned to 'g' inside 'a' which is the application's prefix, so you can execute it with 'ag' when you run that tree in terminal.
42
+
43
+ To run the tree call `#show`
44
+ ```ruby
45
+ t.show
46
+ ```
47
+
48
+ it will print the toplevel categories and commands and wait for you to press a character to execute the command or print sub commands of a category node.
49
+
50
+ when the tree reachs a leaf it'll exit, if a command is the leaf it will execute it and exit the tree giving your code the control again.
51
+
52
+ ## Development
53
+
54
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
55
+
56
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/emad-elsaid/command_tree.
61
+
62
+ ## License
63
+
64
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "command_tree"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "command_tree/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "command_tree"
8
+ spec.version = CommandTree::VERSION
9
+ spec.authors = ["Emad Elsaid"]
10
+ spec.email = ["emad.elsaid@blacklane.com"]
11
+
12
+ spec.summary = %q{Command trees for the terminal}
13
+ spec.description = %q{Builds trees of commands for the terminal, each node is either a group of commands or the command itself, every node is associated with a character to access it.}
14
+ spec.homepage = "https://www.github.com/emad-elsaid/command_tree"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "colorize", "~> 0.8"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "minitest", "~> 5.0"
29
+ end
@@ -0,0 +1,80 @@
1
+ require 'io/console'
2
+ require 'colorize'
3
+ require 'colorized_string'
4
+
5
+ module CommandTree
6
+ class Tree
7
+ def initialize
8
+ @calls = { '' => {} }
9
+ end
10
+
11
+ def register(path, name, &block)
12
+ insure_path(path, name)
13
+ calls[path] = { name: name, block: block } if block_given?
14
+ end
15
+
16
+ def show
17
+ execute_path('')
18
+ end
19
+
20
+ private
21
+
22
+ attr_accessor :calls
23
+
24
+ def insure_path(path, name)
25
+ return if path.empty?
26
+
27
+ insure_path(path[0...-1], name)
28
+ calls[path] = { name: name } unless calls[path]
29
+ end
30
+
31
+ def execute_path(path)
32
+ return puts "#{path} couldn't be found..." unless calls.key?(path)
33
+
34
+ node = calls[path]
35
+ children = calls.keys.select { |key| key.start_with?(path) && key.length == (path.length + 1) }
36
+
37
+ node[:block].call if node.key?(:block)
38
+
39
+ return if children.empty?
40
+
41
+ puts "#{node[:name]}:".light_magenta.bold if node.key?(:name)
42
+
43
+ print_children(children)
44
+
45
+ choice = STDIN.getch
46
+ execute_path(path + choice)
47
+ end
48
+
49
+ def print_children(children)
50
+ table_content = []
51
+
52
+ children.each do |child|
53
+ child_node = calls[child]
54
+
55
+ output = child[-1].green
56
+ output << ' → '.light_black
57
+
58
+ output << if child_node.key?(:block)
59
+ " #{child_node[:name].ljust(40)}".cyan
60
+ else
61
+ "+#{child_node[:name].ljust(40)}".light_magenta.bold
62
+ end
63
+
64
+ table_content << output
65
+ end
66
+
67
+ table(table_content, 40)
68
+ print "\n\n"
69
+ end
70
+
71
+ def table(items, item_width)
72
+ _, width = IO.console.winsize
73
+ items_per_row = width / item_width
74
+ items.each_with_index do |item, index|
75
+ print item
76
+ print "\n" if ((index + 1) % items_per_row).zero? && index > 0
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module CommandTree
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "command_tree/version"
2
+ require "command_tree/tree"
3
+
4
+ module CommandTree
5
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: command_tree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Emad Elsaid
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ description: Builds trees of commands for the terminal, each node is either a group
70
+ of commands or the command itself, every node is associated with a character to
71
+ access it.
72
+ email:
73
+ - emad.elsaid@blacklane.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - command_tree.gemspec
88
+ - lib/command_tree.rb
89
+ - lib/command_tree/tree.rb
90
+ - lib/command_tree/version.rb
91
+ homepage: https://www.github.com/emad-elsaid/command_tree
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.7.3
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Command trees for the terminal
115
+ test_files: []