Skip to content

Commit

Permalink
Add alignment and ansi check to top level
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Jan 6, 2018
1 parent 9e24f2b commit b847363
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
50 changes: 50 additions & 0 deletions lib/strings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,56 @@ module Strings
SPACE_RE = %r{\s+}mo.freeze
NEWLINE_RE = %r{\n}o.freeze

# Align text within the width.
#
# @see Strings::Align#align
#
# @api public
def align(*args)
Align.align(*args)
end
module_function :align

# Align text left within the width.
#
# @see Strings::Align#align_left
#
# @api public
def align_left(*args)
Align.align_left(*args)
end
module_function :align_left

# Align text with the width.
#
# @see Strings::Align#align
#
# @api public
def align_center(*args)
Align.align_center(*args)
end
module_function :align_center

# Align text with the width.
#
# @see Strings::Align#align
#
# @api public
def align_right(*args)
Align.align_right(*args)
end
module_function :align_right

# Check if string contains ANSI codes
#
# @see Strings::ANSI#ansi?
#
# @api public
def ansi?(string)
ANSI.ansi?(string)
end
module_function :ansi?

# Remove any line break characters from the text
#
# @see Strings::Fold#fold
Expand Down
18 changes: 18 additions & 0 deletions spec/unit/align_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

RSpec.describe Strings, '#align' do
it "aligns text" do
text = "the madness of men"
expect(Strings.align(text, 22, direction: :center)).to eq(" the madness of men ")
end

it "aligns text to left" do
text = "the madness of men"
expect(Strings.align_left(text, 22)).to eq("the madness of men ")
end

it "aligns text to right" do
text = "the madness of men"
expect(Strings.align_right(text, 22)).to eq(" the madness of men")
end
end
7 changes: 7 additions & 0 deletions spec/unit/ansi_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

RSpec.describe Strings, '#ansi?' do
it "removes ansi codes" do
expect(Strings.ansi?("\e[33mfoo\e[0m")).to eq(true)
end
end

0 comments on commit b847363

Please sign in to comment.