tty-box 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: 385f1356929f59e50fd7818fa826c6276a1a110862d05273a549879683301874
4
+ data.tar.gz: 308a4a0d28e4933f0856990a5ba072822568bfe9c63b52879ba0147bdae95546
5
+ SHA512:
6
+ metadata.gz: 32d66ca08caafc132eee52d584da928861493361cbefc0a1376513bfff138f9993061c10374e88da3ba8ce5f44799024af46c5a00cb7f731111095f00fc9fa31
7
+ data.tar.gz: 9a705f75359808f18022b2f0fde41dfd4ee1c0fc989d23b20ad229105037a0103da39fee329d516470f7a15df1952f7a639ce27a741ed6ba2537dbcac7798a39
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /Gemfile.lock
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --warnings
@@ -0,0 +1,26 @@
1
+ ---
2
+ language: ruby
3
+ sudo: false
4
+ cache: bundler
5
+ before_install: "gem update bundler"
6
+ script: "bundle exec rake ci"
7
+ rvm:
8
+ - 2.0.0
9
+ - 2.1.10
10
+ - 2.2.9
11
+ - 2.3.6
12
+ - 2.4.4
13
+ - 2.5.1
14
+ - ruby-head
15
+ - jruby-9.1.5.0
16
+ - jruby-head
17
+ matrix:
18
+ allow_failures:
19
+ - rvm: ruby-head
20
+ - rvm: jruby-9.1.5.0
21
+ - rvm: jruby-head
22
+ fast_finish: true
23
+ branches:
24
+ only: master
25
+ notifications:
26
+ email: false
@@ -0,0 +1,7 @@
1
+ # Change log
2
+
3
+ ## [v0.1.0] - 2018-07-23
4
+
5
+ * Initial implementation and release
6
+
7
+ [v0.1.0]: https://github.com/piotrmurach/tty-box/compare/v0.1.0
@@ -0,0 +1,74 @@
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, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ 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 [email]. 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 [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
6
+
7
+ group :test do
8
+ gem 'benchmark-ips', '~> 2.7.2'
9
+ gem 'simplecov', '~> 0.14.1'
10
+ gem 'coveralls', '~> 0.8.21'
11
+ end
12
+
13
+ group :metrics do
14
+ gem 'yard', '~> 0.9.14'
15
+ gem 'yardstick', '~> 0.9.9'
16
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Piotr Murach
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,310 @@
1
+ # TTY::Box [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/tty-box.svg)][gem]
4
+ [![Build Status](https://secure.travis-ci.org/piotrmurach/tty-box.svg?branch=master)][travis]
5
+ [![Build status](https://ci.appveyor.com/api/projects/status/h9b88fk5xpya3fh1?svg=true)][appveyor]
6
+ [![Maintainability](https://api.codeclimate.com/v1/badges/dfac05073e1549e9dbb6/maintainability)][codeclimate]
7
+ [![Coverage Status](https://coveralls.io/repos/github/piotrmurach/tty-box/badge.svg)][coverage]
8
+ [![Inline docs](http://inch-ci.org/github/piotrmurach/tty-box.svg?branch=master)][inchpages]
9
+
10
+ [gitter]: https://gitter.im/piotrmurach/tty
11
+ [gem]: http://badge.fury.io/rb/tty-box
12
+ [travis]: http://travis-ci.org/piotrmurach/tty-box
13
+ [appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-box
14
+ [codeclimate]: https://codeclimate.com/github/piotrmurach/tty-box/maintainability
15
+ [coverage]: https://coveralls.io/github/piotrmurach/tty-box
16
+ [inchpages]: http://inch-ci.org/github/piotrmurach/tty-box
17
+
18
+ > Draw various frames and boxes in your terminal interface.
19
+
20
+ **TTY::Box** provides box drawing component for [TTY](https://github.com/piotrmurach/tty) toolkit.
21
+
22
+ ![Box drawing](https://cdn.rawgit.com/piotrmurach/tty-box/master/assets/tty-box-drawing.png)
23
+
24
+ ## Installation
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ ```ruby
29
+ gem 'tty-box'
30
+ ```
31
+
32
+ And then execute:
33
+
34
+ $ bundle
35
+
36
+ Or install it yourself as:
37
+
38
+ $ gem install tty-box
39
+
40
+ ## Contents
41
+
42
+ * [1. Usage](#1-usage)
43
+ * [2. Interface](#2-interface)
44
+ * [2.1 frame](#21-frame)
45
+ * [2.2 position](#22-position)
46
+ * [2.3 dimension](#23-dimension)
47
+ * [2.4 title](#24-title)
48
+ * [2.5 border](#25-border)
49
+ * [2.6 styling](#26-styling)
50
+ * [2.7 formatting](#27-formatting)
51
+
52
+ ## 1. Usage
53
+
54
+ Using the `frame` method, you can draw a box in a terminal emulator:
55
+
56
+ ```ruby
57
+ box = TTY::Box.frame(
58
+ width: 30,
59
+ height: 10,
60
+ align: :center,
61
+ padding: 3
62
+ ) do
63
+ "Drawing a box in terminal emulator"
64
+ end
65
+
66
+ ```
67
+
68
+ ```ruby
69
+ print box
70
+ # =>
71
+ # ┌────────────────────────────┐
72
+ # │ │
73
+ # │ │
74
+ # │ │
75
+ # │ Drawing a box in │
76
+ # │ terminal emulator │
77
+ # │ │
78
+ # │ │
79
+ # │ │
80
+ # └────────────────────────────┘
81
+ ```
82
+
83
+ ## 2. Interface
84
+
85
+ ### 2.1 frame
86
+
87
+ You can draw a box in the top left corner of your terminal window by using the `frame` method and providing at the very minimum the height and the width:
88
+
89
+ ```ruby
90
+ box = TTY::Box.frame(width: 30, height: 10)
91
+ ```
92
+
93
+ which when printed will prodcue the following output in your terminal:
94
+
95
+ ```ruby
96
+ print box
97
+ # =>
98
+ # ┌────────────────────────────┐
99
+ # │ │
100
+ # │ │
101
+ # │ │
102
+ # │ │
103
+ # │ │
104
+ # │ │
105
+ # │ │
106
+ # │ │
107
+ # └────────────────────────────┘
108
+ ```
109
+
110
+ Then you can use [tty-cursor](https://github.com/piotrmurach/tty-cursor) to directly manipulate content to be displayed inside the box.
111
+
112
+ Alternatively, you can also pass a block to provide a content for the box:
113
+
114
+ ```ruby
115
+ box = TTY::Box.frame(width: 30, height: 10) do
116
+ "Drawin a box in terminal emulator"
117
+ end
118
+ ```
119
+
120
+ which when printed will produce the following output in your terminal:
121
+
122
+ ```ruby
123
+ print box
124
+ # =>
125
+ # ┌────────────────────────────┐
126
+ # │Drawing a box in terminal │
127
+ # │emulator │
128
+ # │ │
129
+ # │ │
130
+ # │ │
131
+ # │ │
132
+ # │ │
133
+ # │ │
134
+ # └────────────────────────────┘
135
+ ```
136
+
137
+ ### 2.2 position
138
+
139
+ By default, a box will be positioned in the top left corner of the terminal emulator. Use `:top` and `:left` keyword arguments to change this:
140
+
141
+ ```ruby
142
+ TTY::Box.frame(top: 5, left: 10)
143
+ ```
144
+
145
+ If you wish to center your box within the terminal window then consider using [tty-screen](https://github.com/piotrmurach/tty-screen) for gathering terminal screen size information.
146
+
147
+ ### 2.3 dimension
148
+
149
+ At the very minimum a box requires to be given size by using two keyword arguments `:width` and `:height`:
150
+
151
+ ```ruby
152
+ TTY::Box.frame(width: 30, height: 10)
153
+ ```
154
+
155
+ ### 2.4 title
156
+
157
+ You can specify titles using the `:title` keyword and a hash value that contains one of the `:top_left`, `:top_center`, `:top_right`, `:bottom_left`, `:bottom_center`, `:bottom_right` keys and actual title as value. For example, to add titles to top left and bottom right of the frame do:
158
+
159
+
160
+ ```ruby
161
+ box = TTY::Box.frame(width: 30, height: 10, title: {top_left: 'TITLE', bottom_right: 'v1.0'})
162
+ ```
163
+
164
+ which when printed in console will render the following:
165
+
166
+ ```ruby
167
+ print box
168
+ # =>
169
+ # ┌TITLE───────────────────────┐
170
+ # │ │
171
+ # │ │
172
+ # │ │
173
+ # │ │
174
+ # │ │
175
+ # │ │
176
+ # │ │
177
+ # │ │
178
+ # └──────────────────────(v1.0)┘
179
+ ```
180
+
181
+ ### 2.5 border
182
+
183
+ There are two types of border `:light` and `:thick`. By default the `:light` border is used. This can be changed using the `:border` keyword:
184
+
185
+ ```ruby
186
+ box = TTY::Box.new(width 30, height: 10, border: :thick)
187
+ ```
188
+
189
+ and printing the box out to console will produce:
190
+
191
+ ```ruby
192
+ print box
193
+ # =>
194
+ # ╔════════════════════════════╗
195
+ # ║ ║
196
+ # ║ ║
197
+ # ║ ║
198
+ # ║ ║
199
+ # ║ ║
200
+ # ║ ║
201
+ # ║ ║
202
+ # ║ ║
203
+ # ╚════════════════════════════╝
204
+ ```
205
+
206
+ You can also selectively switch off border sides by passing options hash. For example to remove bottom border do:
207
+
208
+ ```ruby
209
+ TTY::Box.new(width: 30, height: 10, border: {type: :thick, bottom: false})
210
+ ```
211
+
212
+ ### 2.6 styling
213
+
214
+ By default drawing a box doesn't apply any styling. You can change this using the `:style` keyword with foreground `:fg` and background `:bg` keys for both the main content and the border:
215
+
216
+ ```ruby
217
+ style: {
218
+ fg: :bright_yellow,
219
+ bg: :blue,
220
+ border: {
221
+ fg: :bright_yellow,
222
+ bg: :blue
223
+ }
224
+ }
225
+ ```
226
+
227
+ The above style configuration will produce the result similar to the top demo, a MS-DOS look & feel window.
228
+
229
+ ### 2.7 formatting
230
+
231
+ You can use `:align` keyword to format content either to be `:left`, `:center` or `:right` aligned:
232
+
233
+ ```ruby
234
+ box = TTY::Box.frame(width: 30, height: 10, align: :center) do
235
+ "Drawing a box in terminal emulator"
236
+ end
237
+ ```
238
+
239
+ The above will create the following output in your terminal:
240
+
241
+ ```ruby
242
+ print box
243
+ # =>
244
+ # ┌────────────────────────────┐
245
+ # │ Drawing a box in terminal │
246
+ # │ emulator │
247
+ # │ │
248
+ # │ │
249
+ # │ │
250
+ # │ │
251
+ # │ │
252
+ # │ │
253
+ # └────────────────────────────┘
254
+ ```
255
+
256
+ You can also use `:padding` keyword to further format the content using the following values:
257
+
258
+ ```ruby
259
+ [1,3,1,3] # => pad content left & right with 3 spaces and add 1 line above & below
260
+ [1,3] # => pad content left & right with 3 spaces and add 1 line above & below
261
+ 1 # => shorthand for [1,1,1,1]
262
+ ```
263
+
264
+ For example, if you wish to pad content all around do:
265
+
266
+ ```ruby
267
+ box = TTY::Box.frame(width: 30, height: 10, align: :center, padding: 3) do
268
+ "Drawing a box in terminal emulator"
269
+ end
270
+ ```
271
+
272
+ Here's an example output:
273
+
274
+ ```ruby
275
+ print box
276
+ # =>
277
+ # ┌────────────────────────────┐
278
+ # │ │
279
+ # │ │
280
+ # │ │
281
+ # │ Drawing a box in │
282
+ # │ terminal emulator │
283
+ # │ │
284
+ # │ │
285
+ # │ │
286
+ # └────────────────────────────┘
287
+ #
288
+ ```
289
+
290
+ ## Development
291
+
292
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
293
+
294
+ 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).
295
+
296
+ ## Contributing
297
+
298
+ Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/tty-box. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
299
+
300
+ ## License
301
+
302
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
303
+
304
+ ## Code of Conduct
305
+
306
+ Everyone interacting in the TTY::Box project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/piotrmurach/tty-box/blob/master/CODE_OF_CONDUCT.md).
307
+
308
+ ## Copyright
309
+
310
+ Copyright (c) 2018 Piotr Murach. See LICENSE for further details.
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ FileList['tasks/**/*.rake'].each(&method(:import))
4
+
5
+ desc 'Run all specs'
6
+ task ci: %w[ spec ]
7
+
8
+ task default: :spec
@@ -0,0 +1,23 @@
1
+ ---
2
+ install:
3
+ - SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
4
+ - ruby --version
5
+ - gem --version
6
+ - bundle install
7
+ build: off
8
+ test_script:
9
+ - bundle exec rake ci
10
+ environment:
11
+ matrix:
12
+ - ruby_version: "200"
13
+ - ruby_version: "200-x64"
14
+ - ruby_version: "21"
15
+ - ruby_version: "21-x64"
16
+ - ruby_version: "22"
17
+ - ruby_version: "22-x64"
18
+ - ruby_version: "23"
19
+ - ruby_version: "23-x64"
20
+ - ruby_version: "24"
21
+ - ruby_version: "24-x64"
22
+ - ruby_version: "25"
23
+ - ruby_version: "25-x64"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tty/box"
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,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,52 @@
1
+ require 'tty-box'
2
+
3
+ print TTY::Cursor.clear_screen
4
+
5
+ box_1 = TTY::Box.frame(
6
+ top: 2,
7
+ left: 10,
8
+ width: 30,
9
+ height: 10,
10
+ border: :thick,
11
+ align: :center,
12
+ padding: 3,
13
+ title: {
14
+ top_left: ' file1 '
15
+ },
16
+ style: {
17
+ fg: :bright_yellow,
18
+ bg: :blue,
19
+ border: {
20
+ fg: :bright_yellow,
21
+ bg: :blue
22
+ }
23
+ }
24
+ ) do
25
+ "Drawing a box in terminal emulator"
26
+ end
27
+
28
+ box_2 = TTY::Box.frame(
29
+ top: 8,
30
+ left: 34,
31
+ width: 30,
32
+ height: 10,
33
+ border: :thick,
34
+ align: :center,
35
+ padding: 3,
36
+ title: {
37
+ top_left: ' file2 '
38
+ },
39
+ style: {
40
+ fg: :bright_yellow,
41
+ bg: :blue,
42
+ border: {
43
+ fg: :bright_yellow,
44
+ bg: :blue
45
+ }
46
+ }
47
+ ) do
48
+ "Drawing a box in terminal emulator"
49
+ end
50
+
51
+ puts box_1 + box_2
52
+ print "\n" * 5
@@ -0,0 +1 @@
1
+ require_relative 'tty/box'
@@ -0,0 +1,198 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'strings'
4
+ require 'pastel'
5
+ require 'tty-cursor'
6
+
7
+ require_relative 'box/border'
8
+ require_relative 'box/version'
9
+
10
+ module TTY
11
+ module Box
12
+ module_function
13
+
14
+ BOX_CHARS = {
15
+ light: %w[┘ ┐ ┌ └ ┤ ┴ ┬ ├ ─ │ ┼],
16
+ thick: %w[╝ ╗ ╔ ╚ ╣ ╩ ╦ ╠ ═ ║ ╬]
17
+ }.freeze
18
+
19
+ def line_char(border = :light)
20
+ BOX_CHARS[border][8]
21
+ end
22
+
23
+ def pipe_char(border = :light)
24
+ BOX_CHARS[border][9]
25
+ end
26
+
27
+ def cross_char(border = :light)
28
+ BOX_CHARS[border][10]
29
+ end
30
+
31
+ def left_divider_char(border = :light)
32
+ BOX_CHARS[border][7]
33
+ end
34
+
35
+ def right_divider_char(border = :light)
36
+ BOX_CHARS[border][4]
37
+ end
38
+
39
+ def top_left_char(border = :light)
40
+ BOX_CHARS[border][2]
41
+ end
42
+
43
+ def top_divider_char(border = :light)
44
+ BOX_CHARS[border][6]
45
+ end
46
+
47
+ def top_right_char(border = :light)
48
+ BOX_CHARS[border][1]
49
+ end
50
+
51
+ def bottom_left_char(border = :light)
52
+ BOX_CHARS[border][3]
53
+ end
54
+
55
+ def bottom_divider_char(border = :light)
56
+ BOX_CHARS[border][5]
57
+ end
58
+
59
+ def bottom_right_char(border = :light)
60
+ BOX_CHARS[border][0]
61
+ end
62
+
63
+ def cursor
64
+ TTY::Cursor
65
+ end
66
+
67
+ def color
68
+ @color ||= Pastel.new
69
+ end
70
+
71
+ # Create a frame
72
+ #
73
+ # @api public
74
+ def frame(top: 0, left: 0, width: 35, height: 3, align: :left, padding: 0,
75
+ title: {}, border: :light, style: {})
76
+ output = []
77
+ content = []
78
+
79
+ border = Border.parse(border)
80
+
81
+ if block_given?
82
+ content = format(yield, width, padding, align)
83
+ end
84
+
85
+ fg, bg = *extract_style(style)
86
+ border_fg, border_bg = *extract_style(style[:border] || {})
87
+
88
+ if border.top?
89
+ output << cursor.move_to(left, top)
90
+ output << top_border(title, width, border.type, style)
91
+ end
92
+ (height - 2).times do |i|
93
+ if border.left?
94
+ output << cursor.move_to(left, top + i + 1)
95
+ output << border_bg.(border_fg.(pipe_char(border.type)))
96
+ end
97
+ if content[i].nil?
98
+ output << bg.(fg.(' ' * (width - 2))) if style[:fg] || style[:bg]
99
+ else
100
+ output << bg.(fg.(content[i]))
101
+ if style[:fg] || style[:bg]
102
+ output << bg.(fg.(' ' * (width - 2 - content[i].size)))
103
+ end
104
+ end
105
+ if border.right?
106
+ output << cursor.move_to(left + width - 1, top + i + 1)
107
+ output << border_bg.(border_fg.(pipe_char(border.type)))
108
+ end
109
+ end
110
+ if border.bottom?
111
+ output << cursor.move_to(left, top + height - 1)
112
+ output << bottom_border(title, width, border.type, style)
113
+ end
114
+
115
+ output.join
116
+ end
117
+
118
+ # Format content
119
+ #
120
+ # @return [Array[String]]
121
+ #
122
+ # @api private
123
+ def format(content, width, padding, align)
124
+ pad = Strings::Padder.parse(padding)
125
+ total_width = width - 2 - (pad.left + pad.right)
126
+
127
+ wrapped = Strings.wrap(content, total_width)
128
+ aligned = Strings.align(wrapped, total_width, direction: align)
129
+ padded = Strings.pad(aligned, padding)
130
+ padded.split("\n")
131
+ end
132
+
133
+ # Convert style keywords into styling
134
+ #
135
+ # @return [Array[Proc, Proc]]
136
+ #
137
+ # @api private
138
+ def extract_style(style)
139
+ fg = style[:fg] ? color.send(style[:fg]).detach : -> (c) { c }
140
+ bg = style[:bg] ? color.send(:"on_#{style[:bg]}").detach : -> (c) { c }
141
+ [fg, bg]
142
+ end
143
+
144
+ # Top border
145
+ #
146
+ # @return [String]
147
+ #
148
+ # @api private
149
+ def top_border(title, width, border, style)
150
+ top_titles_size = title[:top_left].to_s.size +
151
+ title[:top_center].to_s.size +
152
+ title[:top_right].to_s.size
153
+ fg, bg = *extract_style(style[:border] || {})
154
+
155
+ top_space_left = width - top_titles_size -
156
+ top_left_char.size - top_right_char.size
157
+ top_space_before = top_space_left / 2
158
+ top_space_after = top_space_left / 2 + top_space_left % 2
159
+
160
+ [
161
+ bg.(fg.(top_left_char(border))),
162
+ bg.(title[:top_left].to_s),
163
+ bg.(fg.(line_char(border) * top_space_before)),
164
+ bg.(title[:top_center].to_s),
165
+ bg.(fg.(line_char(border) * top_space_after)),
166
+ bg.(title[:top_right].to_s),
167
+ bg.(fg.(top_right_char(border)))
168
+ ].join('')
169
+ end
170
+
171
+ # Bottom border
172
+ #
173
+ # @return [String]
174
+ #
175
+ # @api private
176
+ def bottom_border(title, width, border, style)
177
+ bottom_titles_size = title[:bottom_left].to_s.size +
178
+ title[:bottom_center].to_s.size +
179
+ title[:bottom_right].to_s.size
180
+ fg, bg = *extract_style(style[:border] || {})
181
+
182
+ bottom_space_left = width - bottom_titles_size -
183
+ bottom_left_char.size - bottom_right_char.size
184
+ bottom_space_before = bottom_space_left / 2
185
+ bottom_space_after = bottom_space_left / 2 + bottom_space_left % 2
186
+
187
+ [
188
+ bg.(fg.(bottom_left_char(border))),
189
+ bg.(title[:bottom_left].to_s),
190
+ bg.(fg.(line_char(border) * bottom_space_before)),
191
+ bg.(title[:bottom_center].to_s),
192
+ bg.(fg.(line_char(border) * bottom_space_after)),
193
+ bg.(title[:bottom_right].to_s),
194
+ bg.(fg.(bottom_right_char(border)))
195
+ ].join('')
196
+ end
197
+ end # TTY
198
+ end # Box
@@ -0,0 +1,40 @@
1
+ module TTY
2
+ module Box
3
+ # A class reponsible for retrieving border options
4
+ #
5
+ # @api private
6
+ class Border
7
+ def self.parse(border)
8
+ case border
9
+ when Hash
10
+ new(border.fetch(:type, :light),
11
+ border.fetch(:top, true),
12
+ border.fetch(:left, true),
13
+ border.fetch(:right, true),
14
+ border.fetch(:bottom, true))
15
+ when *TTY::Box::BOX_CHARS.keys
16
+ new(border, true, true, true, true)
17
+ else
18
+ raise ArgumentError,
19
+ "Wrong value `#{border}` for :border configuration option"
20
+ end
21
+ end
22
+
23
+ attr_reader :type, :top, :left, :right, :bottom
24
+
25
+ alias top? top
26
+ alias left? left
27
+ alias right? right
28
+ alias bottom? bottom
29
+
30
+ def initialize(type, top, left, right, bottom)
31
+ @type = type
32
+ @top = top
33
+ @left = left
34
+ @right = right
35
+ @bottom = bottom
36
+ end
37
+ end # Border
38
+ end # Box
39
+ end # TTY
40
+
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TTY
4
+ module Box
5
+ VERSION = "0.1.0"
6
+ end # Box
7
+ end # TTY
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ desc 'Load gem inside irb console'
4
+ task :console do
5
+ require 'irb'
6
+ require 'irb/completion'
7
+ require File.join(__FILE__, '../../lib/tty-box')
8
+ ARGV.clear
9
+ IRB.start
10
+ end
11
+ task c: %w[ console ]
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ desc 'Measure code coverage'
4
+ task :coverage do
5
+ begin
6
+ original, ENV['COVERAGE'] = ENV['COVERAGE'], 'true'
7
+ Rake::Task['spec'].invoke
8
+ ensure
9
+ ENV['COVERAGE'] = original
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc 'Run all specs'
7
+ RSpec::Core::RakeTask.new(:spec) do |task|
8
+ task.pattern = 'spec/{unit,integration}{,/*/**}/*_spec.rb'
9
+ end
10
+
11
+ namespace :spec do
12
+ desc 'Run unit specs'
13
+ RSpec::Core::RakeTask.new(:unit) do |task|
14
+ task.pattern = 'spec/unit{,/*/**}/*_spec.rb'
15
+ end
16
+
17
+ desc 'Run integration specs'
18
+ RSpec::Core::RakeTask.new(:integration) do |task|
19
+ task.pattern = 'spec/integration{,/*/**}/*_spec.rb'
20
+ end
21
+ end
22
+
23
+ rescue LoadError
24
+ %w[spec spec:unit spec:integration].each do |name|
25
+ task name do
26
+ $stderr.puts "In order to run #{name}, do `gem install rspec`"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "tty/box/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "tty-box"
7
+ spec.version = TTY::Box::VERSION
8
+ spec.authors = ["Piotr Murach"]
9
+ spec.email = [""]
10
+
11
+ spec.summary = %q{Draw various frames and boxes in your terminal interface.}
12
+ spec.description = %q{Draw various frames and boxes in your terminal interface.}
13
+ spec.homepage = "https://piotrmurach.github.io/tty"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency 'pastel', '~> 0.7.2'
24
+ spec.add_dependency 'tty-cursor', '~> 0.6.0'
25
+ spec.add_dependency 'strings', '~> 0.1.1'
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.16"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tty-box
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Murach
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pastel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.7.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: tty-cursor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.6.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: strings
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.16'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: Draw various frames and boxes in your terminal interface.
98
+ email:
99
+ - ''
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - CHANGELOG.md
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - appveyor.yml
114
+ - assets/tty-box-drawing.png
115
+ - bin/console
116
+ - bin/setup
117
+ - examples/commander.rb
118
+ - lib/tty-box.rb
119
+ - lib/tty/box.rb
120
+ - lib/tty/box/border.rb
121
+ - lib/tty/box/version.rb
122
+ - tasks/console.rake
123
+ - tasks/coverage.rake
124
+ - tasks/spec.rake
125
+ - tty-box.gemspec
126
+ homepage: https://piotrmurach.github.io/tty
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.7.3
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Draw various frames and boxes in your terminal interface.
150
+ test_files: []