Skip to content

Instantly share code, notes, and snippets.

@radixdev
Created June 8, 2017 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radixdev/ac356de5ce3c802ef9c791db0b41a2c1 to your computer and use it in GitHub Desktop.
Save radixdev/ac356de5ce3c802ef9c791db0b41a2c1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# take some text, make it into a big block of warning text
# like this:
# ***********************************************************************************************
# ** !! WARNING !! **
# ** InAppMessageEvent was published, but no subscribers were found. **
# ** This is likely an integration error. Please ensure that the AppboyInAppMessageManager is **
# ** registered as early as possible. Additionally, be sure to call **
# ** AppboyInAppMessageManager.ensureSubscribedToInAppMessageEvents(Context) in your **
# ** Application onCreate() to avoid losing any in-app messages in the future. **
# ***********************************************************************************************
input = ARGV[0]
MAX_LINE_LENGTH = ARGV[1].to_i
def get_lines(input)
words = input.gsub(/\s+/m, ' ').strip.split(" ")
target_lines = input.length / MAX_LINE_LENGTH
puts target_lines
lines = Array.new
lines.push("!! WARNING !!")
current_line = ""
words.each do |word|
if (current_line.length > 0)
current_line = current_line + " " + word
else
# don't add a space
current_line = word
end
if (current_line.length > MAX_LINE_LENGTH)
#make a new line
lines.push(current_line)
current_line = ""
end
end
if (current_line.length > 0)
# add to the array
lines.push(current_line)
end
lines
end
# now's lets format
def format_lines(input_lines, max_length)
result = lines = Array.new
input_lines.each do |line|
# puts line
num_spaces_to_add = (max_length - line.length)/2
num_spaces_to_add = [0, num_spaces_to_add].max
num_left_spaces = num_spaces_to_add
num_right_spaces = num_spaces_to_add
if (line.length % 2 == 1)
num_right_spaces +=1
end
# add spaces
formatted_line = " " * num_left_spaces + line + " " * num_right_spaces
# add asterisks
formatted_line = "** " + formatted_line + " **"
# puts formatted_line
result.push(formatted_line)
end
result
end
def get_longest_line(input_lines)
line_len = input_lines.max_by(&:length).length
line_len
end
my_lines = get_lines(input)
longest_line = get_longest_line(my_lines)
fmt_lines = format_lines(my_lines, longest_line)
puts "*" * (6 + longest_line)
puts fmt_lines
puts "*" * (6 + longest_line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment