cobi 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d6d12bd06e58a43f3e29a696bc0b9aeba2f14f283dec3e6f1180fbe0d27e0477
4
+ data.tar.gz: 45d56facdb616525cd199b9d1cb73690db9985ca55a76f6b0d92a039c15d24fd
5
+ SHA512:
6
+ metadata.gz: 39b46e162d755bb91a263cb80afa7d37692d1a884758f1ae29b7ccb665e4ba5e07777361cf76c907a9ee61ba9ff9356318f8ebd5742774c24200fe617c38a634
7
+ data.tar.gz: f1f7517971267974559ff9e90e41ed1ad376d241254292230f3695b86cb395cf7083d5258b4ee03db02a886b4378bd40aa8cc4f489bddfa5ee7cf06dffc3aecd
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.5.1
6
+ before_install: gem install bundler -v 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cobi.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem "pastel"
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cobi (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ equatable (0.6.1)
11
+ pastel (0.7.3)
12
+ equatable (~> 0.6)
13
+ tty-color (~> 0.5)
14
+ rake (12.3.2)
15
+ rspec (3.8.0)
16
+ rspec-core (~> 3.8.0)
17
+ rspec-expectations (~> 3.8.0)
18
+ rspec-mocks (~> 3.8.0)
19
+ rspec-core (3.8.0)
20
+ rspec-support (~> 3.8.0)
21
+ rspec-expectations (3.8.2)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.8.0)
24
+ rspec-mocks (3.8.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.8.0)
27
+ rspec-support (3.8.0)
28
+ tty-color (0.5.1)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ cobi!
35
+ pastel
36
+ rake (~> 12.0)
37
+ rspec (~> 3.0)
38
+
39
+ BUNDLED WITH
40
+ 2.1.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Sean Goedecke
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.
@@ -0,0 +1,32 @@
1
+ # Cobi
2
+
3
+ Cobi is a terminal menu library loosely inspired by COBOL's `SCREEN SECTION`. It aims to be (a) as simple as possible to use, and (b) to be a better user experience than the conversational menus that almost every generator uses: users should be able to see all the questions immediately, and navigate around your answers before submitting.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'cobi'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ See `bin/demo` for an example. Initialize a new `Cobi::Screen`, attach some headings or fields to it, then call `run` to display it. It's significantly less powerful than COBOL's `SCREEN SECTION` by design.
16
+
17
+ ```ruby
18
+ dog_info = Cobi::Screen.new
19
+ .heading("Please enter the details of your dog")
20
+ .field("Name")
21
+ .field("Breed")
22
+ .field("Age")
23
+ .run
24
+
25
+ puts dog_info.inspect
26
+ ```
27
+
28
+ ![Screenshot](/screenshot.png?raw=true)
29
+
30
+ ## License
31
+
32
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cobi"
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__)
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cobi"
5
+
6
+ dog_info = Cobi::Screen.new
7
+ .heading("Please enter the details of your dog")
8
+ .field("Name")
9
+ .field("Breed")
10
+ .field("Age")
11
+ .run
12
+
13
+ puts dog_info.inspect
@@ -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,28 @@
1
+ require_relative 'lib/cobi/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "cobi"
5
+ spec.version = Cobi::VERSION
6
+ spec.authors = ["Sean Goedecke"]
7
+ spec.email = ["sgoedecke@zendesk.com"]
8
+
9
+ spec.summary = %q{COBOL-inspired terminal menus}
10
+ spec.homepage = "http://github.com/sgoedecke/cobi"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = spec.homepage
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+ end
@@ -0,0 +1,4 @@
1
+ require 'pastel'
2
+ require 'readline'
3
+ require 'io/console'
4
+ require "cobi/screen"
@@ -0,0 +1,21 @@
1
+ module Cobi
2
+ class Field
3
+ attr_accessor :value, :text, :focused, :length
4
+ def initialize(text, length)
5
+ @text = text
6
+ @length = length
7
+ @focused = false
8
+ @value = ''
9
+ end
10
+
11
+ def to_s
12
+ pastel_ctx = focused? ? Pastel.new.on_white : Pastel.new.on_black
13
+ pastel_ctx.blue(text + ': ') + pastel_ctx.green(value + '_' * (length - value.length))
14
+ end
15
+
16
+ def focused?
17
+ !!focused
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,16 @@
1
+ module Cobi
2
+ class Heading
3
+ attr_reader :text
4
+ def initialize(text)
5
+ @text = text
6
+ end
7
+
8
+ def to_s
9
+ Pastel.new.on_black.white(text)
10
+ end
11
+
12
+ def focused?
13
+ false
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,104 @@
1
+ require 'pastel'
2
+ require 'readline'
3
+ require 'io/console'
4
+ require "cobi/version"
5
+ require "cobi/field"
6
+ require "cobi/heading"
7
+
8
+ module Cobi
9
+ class Screen
10
+ attr_reader :screen_height
11
+
12
+ def initialize
13
+ @elements = []
14
+ @screen_height = `tput lines`.to_i
15
+ @running = false
16
+ end
17
+
18
+ def run
19
+ # Focus the first field
20
+ @elements.find { |e| e.is_a?(Field) }.focused = true
21
+ @running = true
22
+
23
+ while @running
24
+ draw
25
+ get_command
26
+ end
27
+
28
+ values
29
+ end
30
+
31
+ def on_submit(&block)
32
+ @on_submit = block
33
+ self
34
+ end
35
+
36
+ def heading(text)
37
+ @elements << Heading.new(text)
38
+ self
39
+ end
40
+
41
+ def field(text, length = 10)
42
+ @elements << Field.new(text, length)
43
+ self
44
+ end
45
+
46
+ private
47
+
48
+ def pastel
49
+ @pastel ||= Pastel.new.on_black
50
+ end
51
+
52
+ def draw
53
+ # first clear the screen, in case the terminal size calculation is offset by footers/tmux separators
54
+ puts pastel.black("\e[H\e[2J")
55
+
56
+ @elements.each do |e|
57
+ puts e.to_s
58
+ end
59
+
60
+ puts pastel.black("\n" * (screen_height - @elements.length - 4))
61
+ puts pastel.green("Tab to switch fields | Enter to submit")
62
+ end
63
+
64
+ def get_command
65
+ cmd = STDIN.getch
66
+ exit(1) if cmd == "\u0003" # exit on Ctrl+C
67
+
68
+ if cmd == "\t" # tab
69
+ handle_switch_focus
70
+ elsif cmd == "\n" || cmd == "\r" # TODO
71
+ handle_submit
72
+ elsif cmd =~/[[:alpha:]]/ || cmd =~ /[[:digit:]]/
73
+ handle_character(cmd)
74
+ end
75
+ end
76
+
77
+ def handle_submit
78
+ @running = false
79
+ end
80
+
81
+ def values
82
+ @elements.select { |e| e.is_a?(Field) }
83
+ .map { |e| [e.text, e.value] }
84
+ .to_h
85
+ end
86
+
87
+ def handle_character(char)
88
+ @elements.find { |e| e.focused? }.value += char
89
+ end
90
+
91
+ def handle_switch_focus
92
+ focused_index = @elements.find_index { |e| e.focused? }
93
+
94
+ next_index = focused_index + 1
95
+ until @elements[next_index] && @elements[next_index].is_a?(Field)
96
+ next_index += 1
97
+ next_index = 0 unless @elements[next_index]
98
+ end
99
+
100
+ @elements[focused_index].focused = false
101
+ @elements[next_index].focused = true
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,3 @@
1
+ module Cobi
2
+ VERSION = "0.1.0"
3
+ end
Binary file
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cobi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sean Goedecke
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - sgoedecke@zendesk.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".travis.yml"
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/demo
30
+ - bin/setup
31
+ - cobi.gemspec
32
+ - lib/cobi.rb
33
+ - lib/cobi/field.rb
34
+ - lib/cobi/heading.rb
35
+ - lib/cobi/screen.rb
36
+ - lib/cobi/version.rb
37
+ - screenshot.png
38
+ homepage: http://github.com/sgoedecke/cobi
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ allowed_push_host: https://rubygems.org
43
+ homepage_uri: http://github.com/sgoedecke/cobi
44
+ source_code_uri: http://github.com/sgoedecke/cobi
45
+ changelog_uri: http://github.com/sgoedecke/cobi
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.3.0
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.7.6
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: COBOL-inspired terminal menus
66
+ test_files: []