wrapomatic 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -5
- data/lib/wrapomatic/line/processor/base.rb +48 -0
- data/lib/wrapomatic/line/processor/primary.rb +38 -0
- data/lib/wrapomatic/line/processor/remainder.rb +22 -0
- data/lib/wrapomatic/line/processor.rb +32 -0
- data/lib/wrapomatic/line.rb +58 -0
- data/lib/wrapomatic/version.rb +2 -1
- data/lib/wrapomatic/wrapper.rb +35 -30
- data/lib/wrapomatic.rb +19 -3
- data/wrapomatic.gemspec +1 -0
- metadata +22 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c73d088e719445e4d5a9393b58b2ee9d328ff0e
|
4
|
+
data.tar.gz: b492f8d020442d589fb291275d308ee1a2be7774
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9abe5effd94b0ee85444827b618cc64fed6053802ec1c064248ae3b95334f5b84b9c2b7bd65343af95e30e9ffc7bf97197d00fb953f4c663413d7b81ec342e45
|
7
|
+
data.tar.gz: 58fc933525133cd6d9e6a2a6f2a37637dba97487dd66880e8a5969e9c0aa4f2ae2821f62d541c7d6ed99789c78df420b72bb13f319264227031711ec1ab31394
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# Wrapomatic
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Wrapomatic is a naive text line wrapper that also supports indentation for the wrapped text.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,10 +20,24 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
```ruby
|
24
|
+
require 'wrapomatic'
|
25
|
+
|
26
|
+
text = <<-EOT
|
27
|
+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
28
|
+
|
29
|
+
And THAT was a lot of text on one line!
|
30
|
+
EOT
|
31
|
+
|
32
|
+
Wrapomatic.wrap(text, indents: 1, columns: 40)
|
33
|
+
|
34
|
+
# => " Lorem ipsum dolor sit amet,\n consectetur adipiscing elit, sed do\n eiusmod tempor incididunt ut labore et\n dolore magna aliqua. Ut enim ad minim\n veniam, quis nostrud exercitation\n ullamco laboris nisi ut aliquip ex ea\n commodo consequat. Duis aute irure\n dolor in reprehenderit in voluptate\n velit esse cillum dolore eu fugiat\n nulla pariatur. Excepteur sint\n occaecat cupidatat non proident, sunt\n in culpa qui officia deserunt mollit\n anim id est laborum.\n \n And THAT was a lot of text on one\n line!"
|
35
|
+
```
|
26
36
|
|
27
37
|
## Development
|
28
38
|
|
39
|
+
Branches and releases for this project are managed by [git-flow](https://github.com/nvie/gitflow).
|
40
|
+
|
29
41
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
42
|
|
31
43
|
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
@@ -33,7 +45,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
33
45
|
## Contributing
|
34
46
|
|
35
47
|
1. Fork it ( https://github.com/[my-github-username]/wrapomatic/fork )
|
36
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
48
|
+
2. Create your feature branch off of develop (`git checkout develop ; git checkout -b my-new-feature`)
|
37
49
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
50
|
4. Push to the branch (`git push origin my-new-feature`)
|
39
51
|
5. Create a new Pull Request
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Wrapomatic
|
2
|
+
class Line
|
3
|
+
module Processor
|
4
|
+
|
5
|
+
# The base processor from which all other processors inherit
|
6
|
+
#
|
7
|
+
# @note To create a new Line processor, inherit from this class and
|
8
|
+
# implement the private method `processed`, returning a String
|
9
|
+
#
|
10
|
+
# @example Creating A New Line Processor
|
11
|
+
# class MyProcessor < Wrapomatic::Line::Processor::Base
|
12
|
+
# private
|
13
|
+
# def processed
|
14
|
+
# "the conent that we want to pass back after processing"
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# @!attribute [r] text
|
19
|
+
# @return [String] the text being processed
|
20
|
+
#
|
21
|
+
# @!attribute [r] columns
|
22
|
+
# @return [Integer] the maximum number of characters in the result
|
23
|
+
class Base
|
24
|
+
attr_reader :text, :columns
|
25
|
+
|
26
|
+
# @param text [String] the text to process
|
27
|
+
#
|
28
|
+
# @param columns [Integer] the column cutoff at which the line should
|
29
|
+
# be wrapped
|
30
|
+
def initialize(text, columns)
|
31
|
+
@text, @columns = text, columns
|
32
|
+
end
|
33
|
+
|
34
|
+
# The content after processing
|
35
|
+
#
|
36
|
+
# @return [String] the line part resulting from processing
|
37
|
+
def content
|
38
|
+
processed
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def processed
|
43
|
+
text
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'wrapomatic/line/processor/base'
|
2
|
+
|
3
|
+
module Wrapomatic
|
4
|
+
class Line
|
5
|
+
module Processor
|
6
|
+
|
7
|
+
# A line processor that extracts the primary line, which is all parts of
|
8
|
+
# the line before the column cutoff, inclusive.
|
9
|
+
class Primary < Base
|
10
|
+
private
|
11
|
+
def processed
|
12
|
+
@processed ||= primary_part
|
13
|
+
end
|
14
|
+
|
15
|
+
def primary_part
|
16
|
+
text.slice(0..line_break).rstrip
|
17
|
+
end
|
18
|
+
|
19
|
+
def last_leading_whitespace
|
20
|
+
[0, text.index(/[^\s]/).to_i - 1].max
|
21
|
+
end
|
22
|
+
|
23
|
+
def line_break
|
24
|
+
case breaking_whitespace > last_leading_whitespace
|
25
|
+
when true
|
26
|
+
[columns, breaking_whitespace].min
|
27
|
+
else
|
28
|
+
columns
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def breaking_whitespace
|
33
|
+
text.rindex(/(\s|-)/, columns).to_i
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'wrapomatic/line/processor/base'
|
2
|
+
require 'wrapomatic/line/processor/primary'
|
3
|
+
|
4
|
+
module Wrapomatic
|
5
|
+
class Line
|
6
|
+
module Processor
|
7
|
+
|
8
|
+
# A line processor that extracts the line remainder, which is what is left
|
9
|
+
# after the primary line is extracted.
|
10
|
+
class Remainder < Base
|
11
|
+
private
|
12
|
+
def processed
|
13
|
+
@processed ||= text.gsub(/^#{primary}/, '').lstrip
|
14
|
+
end
|
15
|
+
|
16
|
+
def primary
|
17
|
+
@primary ||= Primary.new(text, columns).content
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'wrapomatic/line/processor/primary'
|
2
|
+
require 'wrapomatic/line/processor/remainder'
|
3
|
+
|
4
|
+
module Wrapomatic
|
5
|
+
class Line
|
6
|
+
|
7
|
+
# A deep and dark module that really shouldn't be used directly
|
8
|
+
#
|
9
|
+
# @api private
|
10
|
+
module Processor
|
11
|
+
|
12
|
+
# Process a line
|
13
|
+
#
|
14
|
+
# @param [Line] a Line object to process
|
15
|
+
#
|
16
|
+
# @return [Array<String>] the processed line, broken into individually
|
17
|
+
# wrapped lines
|
18
|
+
def self.process(line)
|
19
|
+
columns = line.columns
|
20
|
+
indents = line.indents
|
21
|
+
text = line.indented
|
22
|
+
|
23
|
+
[Primary.new(text, columns).content] +
|
24
|
+
line.class.new(
|
25
|
+
Remainder.new(text, columns).content,
|
26
|
+
indents,
|
27
|
+
columns
|
28
|
+
).wrapped
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'wrapomatic/line/processor'
|
2
|
+
|
3
|
+
module Wrapomatic
|
4
|
+
|
5
|
+
# Wrap a single line
|
6
|
+
#
|
7
|
+
# @!attribute [r] columns
|
8
|
+
# @return [Integer] the columns cutoff
|
9
|
+
#
|
10
|
+
# @!attribute [r] indents
|
11
|
+
# @return [Integer] the indentation level
|
12
|
+
#
|
13
|
+
# @!attribute [r] original
|
14
|
+
# @return [String] the original text
|
15
|
+
class Line
|
16
|
+
# An indentation in Wrapomatic is two spaces
|
17
|
+
INDENTATION = ' '
|
18
|
+
|
19
|
+
attr_reader :original, :indents, :columns
|
20
|
+
|
21
|
+
# @param original [String] a single line of text
|
22
|
+
#
|
23
|
+
# @param indents [Integer] the number of indentation levels to which the
|
24
|
+
# line should be wrapped
|
25
|
+
#
|
26
|
+
# @param columns [Integer] the number of columns to which the line should be
|
27
|
+
# wrapped
|
28
|
+
#
|
29
|
+
# @raise [ArgumentError] if the provided text contains newline characters
|
30
|
+
def initialize(original, indents, columns)
|
31
|
+
@original, @indents, @columns = original, indents, columns
|
32
|
+
raise ArgumentError.new('original may not contain newline') if contains_newline?
|
33
|
+
end
|
34
|
+
|
35
|
+
# The result of wrapping the text to the provided indentation level and
|
36
|
+
# columns
|
37
|
+
#
|
38
|
+
# @return [Array<String>] the original line broken into several lines that
|
39
|
+
# adhere to the column cutoff and indentation level
|
40
|
+
def wrapped
|
41
|
+
@wrapped ||= indented.length <= columns ?
|
42
|
+
[indented] :
|
43
|
+
Processor.process(self)
|
44
|
+
end
|
45
|
+
|
46
|
+
# The indented line before wrapping
|
47
|
+
#
|
48
|
+
# @return [String] the line indented to the indentation level
|
49
|
+
def indented
|
50
|
+
"#{INDENTATION * indents}#{original}"
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
def contains_newline?
|
55
|
+
original.chars.include?("\n")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/wrapomatic/version.rb
CHANGED
data/lib/wrapomatic/wrapper.rb
CHANGED
@@ -1,47 +1,52 @@
|
|
1
|
+
require 'wrapomatic/line'
|
2
|
+
|
1
3
|
module Wrapomatic
|
4
|
+
|
5
|
+
# Wrap several lines
|
6
|
+
#
|
7
|
+
# @!attribute [r] text
|
8
|
+
# @return [String] the text to wrap
|
9
|
+
#
|
10
|
+
# @!attribute [r] lines
|
11
|
+
# @return [Array<String>] the text after wrapping as individual lines
|
12
|
+
#
|
13
|
+
# @!attribute [r] indents
|
14
|
+
# @return [Integer] the indentation level
|
15
|
+
#
|
16
|
+
# @!attribute [r] columns
|
17
|
+
# @return [Integer] the column cutoff
|
2
18
|
class Wrapper
|
3
19
|
attr_reader :text, :lines, :indents, :columns
|
4
20
|
|
21
|
+
# @param text [String] the text to wrap
|
22
|
+
#
|
23
|
+
# @param indents [Integer] the level to which each line should be
|
24
|
+
# indented
|
25
|
+
#
|
26
|
+
# @param columns [Integer] the column to which each line should be
|
27
|
+
# wrapped
|
5
28
|
def initialize(text, indents = 0, columns = 80)
|
6
|
-
@text = text
|
7
|
-
@indents = indents
|
8
|
-
@columns = columns
|
29
|
+
@text, @indents, @columns = text, indents, columns
|
9
30
|
@lines = []
|
10
|
-
|
31
|
+
spit_some_mad_fire
|
11
32
|
end
|
12
33
|
|
34
|
+
# The text wrapped to the desired indentation level and column cutoff
|
35
|
+
#
|
36
|
+
# @return [String] the wrapped text, joined by newlines
|
13
37
|
def wrapped
|
14
|
-
lines.join("\n")
|
38
|
+
@lines.join("\n")
|
15
39
|
end
|
16
40
|
|
17
41
|
private
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def next_line
|
26
|
-
return nil if text.length == 0
|
27
|
-
offset + text_up_to(space_before_location(columns - offset.length - 1))
|
28
|
-
end
|
29
|
-
|
30
|
-
def space_before_location(start)
|
31
|
-
return start if start > text.length
|
32
|
-
text.rindex(/(\s|-)/, start) || start
|
33
|
-
end
|
34
|
-
|
35
|
-
def text_up_to(count)
|
36
|
-
text.slice!(0..count)
|
37
|
-
end
|
38
|
-
|
39
|
-
def indentation
|
40
|
-
' '
|
42
|
+
def spit_some_mad_fire
|
43
|
+
@lines = unwrapped_lines.map {|line|
|
44
|
+
Line.new(line, indents, columns).wrapped
|
45
|
+
}.flatten
|
41
46
|
end
|
42
47
|
|
43
|
-
def
|
44
|
-
|
48
|
+
def unwrapped_lines
|
49
|
+
text.split("\n")
|
45
50
|
end
|
46
51
|
end
|
47
52
|
end
|
data/lib/wrapomatic.rb
CHANGED
@@ -1,10 +1,26 @@
|
|
1
1
|
require 'wrapomatic/version'
|
2
|
+
require 'wrapomatic/line'
|
2
3
|
require 'wrapomatic/wrapper'
|
3
4
|
|
5
|
+
# Let's line wrap some strings!
|
4
6
|
module Wrapomatic
|
7
|
+
|
8
|
+
# Wraps the given text with optional indentation and width
|
9
|
+
#
|
10
|
+
# @param text [String] the text blob to wrap
|
11
|
+
#
|
12
|
+
# @param [Hash] options the options with which to wrap the text
|
13
|
+
#
|
14
|
+
# @option options [Integer] :indents (0) the number of indentation levels to
|
15
|
+
# use for wrapping the text
|
16
|
+
#
|
17
|
+
# @option options [Integer] :columns (80) the number of columes to which the
|
18
|
+
# text should be wrapped
|
19
|
+
#
|
20
|
+
# @return [String] the wrapped text
|
5
21
|
def self.wrap(text, options = {})
|
6
|
-
options[:indents]
|
7
|
-
options[:columns]
|
8
|
-
Wrapper.new(text,
|
22
|
+
indents = options[:indents] || 0
|
23
|
+
columns = options[:columns] || 80
|
24
|
+
Wrapper.new(text, indents, columns).wrapped
|
9
25
|
end
|
10
26
|
end
|
data/wrapomatic.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wrapomatic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dennis Walters
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0.10'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: yard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.8.7
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.8.7
|
69
83
|
description:
|
70
84
|
email:
|
71
85
|
- dwalters@engineyard.com
|
@@ -85,6 +99,11 @@ files:
|
|
85
99
|
- bin/console
|
86
100
|
- bin/setup
|
87
101
|
- lib/wrapomatic.rb
|
102
|
+
- lib/wrapomatic/line.rb
|
103
|
+
- lib/wrapomatic/line/processor.rb
|
104
|
+
- lib/wrapomatic/line/processor/base.rb
|
105
|
+
- lib/wrapomatic/line/processor/primary.rb
|
106
|
+
- lib/wrapomatic/line/processor/remainder.rb
|
88
107
|
- lib/wrapomatic/version.rb
|
89
108
|
- lib/wrapomatic/wrapper.rb
|
90
109
|
- wrapomatic.gemspec
|
@@ -113,3 +132,4 @@ signing_key:
|
|
113
132
|
specification_version: 4
|
114
133
|
summary: Naive text wrapping for terminal output
|
115
134
|
test_files: []
|
135
|
+
has_rdoc:
|