From 326dc5371f3657aa1f117ab8797058693f0e722b Mon Sep 17 00:00:00 2001 From: William Boman Date: Thu, 26 Mar 2015 17:27:53 +0100 Subject: [PATCH 1/2] lib/bro.rb: styling --- lib/bro.rb | 70 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/lib/bro.rb b/lib/bro.rb index 99f5fac..35d0dcf 100755 --- a/lib/bro.rb +++ b/lib/bro.rb @@ -269,15 +269,9 @@ 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 @@ -285,27 +279,55 @@ def require_relative(path) 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 From 532a21b5c6f633c4fe1bde22981d3e657649b60e Mon Sep 17 00:00:00 2001 From: William Boman Date: Thu, 26 Mar 2015 20:01:02 +0100 Subject: [PATCH 2/2] lib/bro.rb: use functions --- lib/bro.rb | 56 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/lib/bro.rb b/lib/bro.rb index 35d0dcf..7ca8025 100755 --- a/lib/bro.rb +++ b/lib/bro.rb @@ -286,33 +286,19 @@ def require_relative(path) lines.each_with_index {|line, index| line.strip! unless line.length == 0 - if /^s?#/.match(line) - # Line starts with a hashtag + if starts_with_hashtag(line) if index == 0 - # Consider it a header if it's the first line - line = line.upcase.colored.yellow.sub /#\s?/, "#{i}. " + line = line_header(line, i) header_found = true else - # Otherwise, it's a comment - line = "\t#{line.colored.green}" + line = line_comment(line) 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 + if contains_query(line, cmd) or starts_with_dollar(line) + line = line_cmd(line, cmd, !starts_with_dollar(line)) else # Last resort - assume it's a comment - line = "\t# #{line}".colored.green + line = line_comment(line) end end else @@ -324,7 +310,7 @@ def require_relative(path) } if !header_found - body.unshift "#{i}. UNTITLED".colored.yellow + body.unshift line_header("# Untitled", i) end puts "\n" + body.join("\n") + "\n" @@ -334,3 +320,31 @@ def require_relative(path) end end end + + +def contains_query(str, query) + return str.index query +end + +def starts_with_dollar(str) + return /^\s?$/.match(str) +end + +def starts_with_hashtag(str) + return /^\s?#/.match(str) +end + +def line_comment(str) + return "\t# #{str.sub('#', '')}".colored.green +end + +def line_cmd(str, highlight, prefix=false) + if prefix + str = "$ #{str}" + end + return "\t#{str}".gsub highlight, highlight.important +end + +def line_header(str, i) + return str.upcase.colored.yellow.sub /#\s?/, "#{i}. " +end