Module: Clack::Note

Defined in:
lib/clack/note.rb

Overview

Renders a bordered note box with optional title in the terminal.

Class Method Summary collapse

Class Method Details

.render(message = "", title: nil, output: $stdout) ⇒ void

This method returns an undefined value.

Render a note box to the output stream.

Parameters:

  • message (String) (defaults to: "")

    the note content

  • title (String, nil) (defaults to: nil)

    optional title displayed above the box

  • output (IO) (defaults to: $stdout)

    output stream (default: $stdout)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/clack/note.rb', line 13

def render(message = "", title: nil, output: $stdout)
  lines = message.to_s.lines.map(&:chomp)
  # Add empty lines at start and end like original
  lines = ["", *lines, ""]
  title_len = Clack::Utils.visible_length(title)
  width = calculate_width(lines, title_len)

  output.puts Colors.gray(Symbols::S_BAR)
  output.puts build_top_border(title, title_len, width)

  lines.each do |line|
    pad = width - Clack::Utils.visible_length(line)
    padded = pad.positive? ? line + (" " * pad) : line
    output.puts "#{Colors.gray(Symbols::S_BAR)}  #{Colors.dim(padded)}#{Colors.gray(Symbols::S_BAR)}"
  end

  output.puts build_bottom_border(width)
end