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

Commit 532a21b

Browse files
committed
lib/bro.rb: use functions
1 parent 326dc53 commit 532a21b

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

lib/bro.rb

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -286,33 +286,19 @@ def require_relative(path)
286286
lines.each_with_index {|line, index|
287287
line.strip!
288288
unless line.length == 0
289-
if /^s?#/.match(line)
290-
# Line starts with a hashtag
289+
if starts_with_hashtag(line)
291290
if index == 0
292-
# Consider it a header if it's the first line
293-
line = line.upcase.colored.yellow.sub /#\s?/, "#{i}. "
291+
line = line_header(line, i)
294292
header_found = true
295293
else
296-
# Otherwise, it's a comment
297-
line = "\t#{line.colored.green}"
294+
line = line_comment(line)
298295
end
299296
else
300-
# Line doesn't start with a hashtag
301-
if line.index cmd or line[0] == '$'
302-
# Line contains the search keyword, or starts with a $
303-
# Consider it a shell command
304-
if line[0] != '$'
305-
# If the line doesn't start with a $, add it
306-
line = "\t$ #{line}"
307-
else
308-
# Otherwise, just indent the line
309-
line = "\t#{line}"
310-
end
311-
# Highlight the search query
312-
line.gsub! cmd, cmd.important
297+
if contains_query(line, cmd) or starts_with_dollar(line)
298+
line = line_cmd(line, cmd, !starts_with_dollar(line))
313299
else
314300
# Last resort - assume it's a comment
315-
line = "\t# #{line}".colored.green
301+
line = line_comment(line)
316302
end
317303
end
318304
else
@@ -324,7 +310,7 @@ def require_relative(path)
324310
}
325311

326312
if !header_found
327-
body.unshift "#{i}. UNTITLED".colored.yellow
313+
body.unshift line_header("# Untitled", i)
328314
end
329315

330316
puts "\n" + body.join("\n") + "\n"
@@ -334,3 +320,31 @@ def require_relative(path)
334320
end
335321
end
336322
end
323+
324+
325+
def contains_query(str, query)
326+
return str.index query
327+
end
328+
329+
def starts_with_dollar(str)
330+
return /^\s?$/.match(str)
331+
end
332+
333+
def starts_with_hashtag(str)
334+
return /^\s?#/.match(str)
335+
end
336+
337+
def line_comment(str)
338+
return "\t# #{str.sub('#', '')}".colored.green
339+
end
340+
341+
def line_cmd(str, highlight, prefix=false)
342+
if prefix
343+
str = "$ #{str}"
344+
end
345+
return "\t#{str}".gsub highlight, highlight.important
346+
end
347+
348+
def line_header(str, i)
349+
return str.upcase.colored.yellow.sub /#\s?/, "#{i}. "
350+
end

0 commit comments

Comments
 (0)