Skip to content

Commit

Permalink
Change to allow changing corner values in ref issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Oct 4, 2018
1 parent b620bcb commit 1db2d10
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
40 changes: 22 additions & 18 deletions lib/tty/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ module Box
thick: %w[ ]
}.freeze

def bottom_right_char(border = :light)
def corner_bottom_right_char(border = :light)
BOX_CHARS[border][0]
end

def top_right_char(border = :light)
def corner_top_right_char(border = :light)
BOX_CHARS[border][1]
end

def top_left_char(border = :light)
def corner_top_left_char(border = :light)
BOX_CHARS[border][2]
end

def bottom_left_char(border = :light)
def corner_bottom_left_char(border = :light)
BOX_CHARS[border][3]
end

Expand Down Expand Up @@ -88,7 +88,7 @@ def frame(top: nil, left: nil, width: 35, height: 3, align: :left, padding: 0,

if border.top?
output << cursor.move_to(left, top) if position
output << top_border(title, width, border.type, style)
output << top_border(title, width, border, style)
output << "\n" unless position
end
(height - 2).times do |i|
Expand All @@ -112,7 +112,7 @@ def frame(top: nil, left: nil, width: 35, height: 3, align: :left, padding: 0,
end
if border.bottom?
output << cursor.move_to(left, top + height - 1) if position
output << bottom_border(title, width, border.type, style)
output << bottom_border(title, width, border, style)
output << "\n" unless position
end

Expand Down Expand Up @@ -156,19 +156,21 @@ def top_border(title, width, border, style)
title[:top_right].to_s.size
fg, bg = *extract_style(style[:border] || {})

top_space_left = width - top_titles_size -
top_left_char.size - top_right_char.size
top_left = send(:"#{border.top_left}_char", border.type)
top_right = send(:"#{border.top_right}_char", border.type)

top_space_left = width - top_titles_size - top_left.size - top_right.size
top_space_before = top_space_left / 2
top_space_after = top_space_left / 2 + top_space_left % 2
top_space_after = top_space_left / 2 + top_space_left % 2

[
bg.(fg.(top_left_char(border))),
bg.(fg.(top_left)),
bg.(title[:top_left].to_s),
bg.(fg.(line_char(border) * top_space_before)),
bg.(fg.(line_char(border.type) * top_space_before)),
bg.(title[:top_center].to_s),
bg.(fg.(line_char(border) * top_space_after)),
bg.(fg.(line_char(border.type) * top_space_after)),
bg.(title[:top_right].to_s),
bg.(fg.(top_right_char(border)))
bg.(fg.(top_right))
].join('')
end

Expand All @@ -182,20 +184,22 @@ def bottom_border(title, width, border, style)
title[:bottom_center].to_s.size +
title[:bottom_right].to_s.size
fg, bg = *extract_style(style[:border] || {})
bottom_left = send(:"#{border.bottom_left}_char", border.type)
bottom_right = send(:"#{border.bottom_right}_char", border.type)

bottom_space_left = width - bottom_titles_size -
bottom_left_char.size - bottom_right_char.size
bottom_left.size - bottom_right.size
bottom_space_before = bottom_space_left / 2
bottom_space_after = bottom_space_left / 2 + bottom_space_left % 2

[
bg.(fg.(bottom_left_char(border))),
bg.(fg.(bottom_left)),
bg.(title[:bottom_left].to_s),
bg.(fg.(line_char(border) * bottom_space_before)),
bg.(fg.(line_char(border.type) * bottom_space_before)),
bg.(title[:bottom_center].to_s),
bg.(fg.(line_char(border) * bottom_space_after)),
bg.(fg.(line_char(border.type) * bottom_space_after)),
bg.(title[:bottom_right].to_s),
bg.(fg.(bottom_right_char(border)))
bg.(fg.(bottom_right))
].join('')
end
end # TTY
Expand Down
21 changes: 21 additions & 0 deletions spec/unit/border_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@
].join)
end

it "creates frame with all corners changed to cross" do
box = TTY::Box.frame(
width: 10, height: 4,
border: {
top: :line,
top_left: :cross,
top_right: :cross,
bottom: :line,
bottom_left: :cross,
bottom_right: :cross
}
)

expect(box).to eq([
"┼────────┼\n",
"│ │\n",
"│ │\n",
"┼────────┼\n"
].join)
end

it "fails to recognise border option" do
expect {
TTY::Box.frame(width: 35, height: 4, border: [:unknown])
Expand Down

0 comments on commit 1db2d10

Please sign in to comment.