Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Commit

Permalink
lib/bro: styling
Browse files Browse the repository at this point in the history
  • Loading branch information
williamboman committed Mar 26, 2015
1 parent b2fbbc1 commit b042ee9
Showing 1 changed file with 46 additions and 24 deletions.
70 changes: 46 additions & 24 deletions lib/bro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,43 +269,65 @@ def require_relative(path)

say <<-QQQ.unindent
#{"#{list.length} entr#{s} for #{cmd_display}".status.underline} #{"-- submit your own example with \"bro add #{cmd_display}\"".colored.yellow}
QQQ

sep = ""
(HighLine::SystemExtensions.terminal_size[0] - 5).times { sep += "." }
sep += "\n"

i = 0
isDefault = true
list.each {|data|
i += 1

obj = {}
obj["#{i}"] = data['id']
state.write_state(obj)

days = (DateTime.now - DateTime.parse(data['updated_at'])).ceil.to_i

body = data['msg']

body = body.gsub(/^([^#][^\n]*)$/, "\\1".important)

say sep + "\n" if i > 1

say body + "\n\n"
lines = data['msg'].split("\n")

body = []
header_found = false
lines.each_with_index {|line, index|
line.strip!
unless line.length == 0
if /^s?#/.match(line)
# Line starts with a hashtag
if index == 0
# Consider it a header if it's the first line
line = line.upcase.colored.yellow.sub /#\s?/, "#{i}. "
header_found = true
else
# Otherwise, it's a comment
line = "\t#{line.colored.green}"
end
else
# Line doesn't start with a hashtag
if line.index cmd or line[0] == '$'
# Line contains the search keyword, or starts with a $
# Consider it a shell command
if line[0] != '$'
# If the line doesn't start with a $, add it
line = "\t$ #{line}"
else
# Otherwise, just indent the line
line = "\t#{line}"
end
# Highlight the search query
line.gsub! cmd, cmd.important
else
# Last resort - assume it's a comment
line = "\t# #{line}".colored.green
end
end
else
# Feed a new line
line = ""
end

upstr = "bro thanks"
upstr += " #{i}" unless isDefault
downstr = "bro ...no"
downstr += " #{i}" unless isDefault
body.push line
}

msg = "\t#{upstr.colored.green}\tto upvote (#{data['up']})\n\t#{downstr.colored.red}\tto downvote (#{data['down']})"
if days > 0
#msg += "\tlast updated\t#{days} days ago"
if !header_found
body.unshift "#{i}. UNTITLED".colored.yellow
end
say msg + "\n\n"
isDefault = false

puts "\n" + body.join("\n") + "\n"
}

end
Expand Down

0 comments on commit b042ee9

Please sign in to comment.