Created
March 29, 2019 20:58
-
-
Save ttscoff/eeaf9369565719850340e66addcd39b3 to your computer and use it in GitHub Desktop.
A little tool for parsing build notes in my projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
require 'optparse' | |
filename = nil | |
# Check for a build note file in the current folder. Filename must start | |
# with "build" and have an extension of txt, md, or markdown. | |
Dir.glob('*.{txt,md,markdown}').each {|f| | |
if f.downcase =~ /^build/ | |
filename = f | |
break | |
end | |
} | |
unless filename | |
puts "No build notes file found" | |
Process.exit 1 | |
end | |
# Make a fancy title line for the section | |
def format_header(title) | |
cols = `tput cols`.strip.to_i | |
title = "==[ \e[1;32m#{title}\e[0m ]" | |
tail = "="*(cols - title.length) | |
"#{title}#{tail}" | |
end | |
# Output a section with fancy title and bright white text. | |
def output_section(sects,key) | |
puts format_header(key) | |
puts | |
puts "\e[1;37m" + sects[key].strip | |
puts"\e[0m" | |
end | |
# Output a list of section titles | |
def show_sections(sects) | |
puts "Sections:\n\n" | |
sects.keys.each do |title| | |
puts "- #{title}" | |
end | |
end | |
# Read in the build notes file and output a hash of "Title" => contents | |
def read_help(filename) | |
help = IO.read(filename) | |
sections = {} | |
split = help.split(/##+/) | |
split.slice!(0) | |
split.each {|sect| | |
if sect.strip.length == 0 | |
next | |
end | |
lines = sect.split(/\n/) | |
title = lines.slice!(0).strip | |
sections[title] = lines.join("\n").strip | |
} | |
sections | |
end | |
options = {} | |
optparse = OptionParser.new do|opts| | |
opts.banner = "Usage: #{__FILE__} [OPTIONS] [SECTION]" | |
opts.separator "" | |
opts.separator "Show build notes for the current project (buildnotes.md). Include a section name to see just that section, or no argument to view all." | |
opts.separator "" | |
opts.separator "Options:" | |
opts.on( '-s', '--sections', 'Show available sections') do |output| | |
show_sections(read_help(filename)) | |
Process.exit(0) | |
end | |
opts.on( '-h', '--help', 'Display this screen' ) do | |
puts opts | |
exit | |
end | |
end | |
optparse.parse! | |
sections = read_help(filename) | |
# If there are arguments use those to search for a matching section | |
match = nil | |
if ARGV.length > 0 | |
search = ARGV.join(" ").downcase | |
sections.keys.each {|k| | |
if k.downcase =~ /^#{search}/ | |
match = k | |
break | |
end | |
} | |
end | |
if match | |
# If we found a match | |
output_section(sections,match) | |
else | |
# If there's no argument or no match found, output all | |
sections.keys.each {|k| | |
output_section(sections,k) | |
} | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment