#!/usr/bin/env ruby require 'gm/notepad' require 'optparse' config = { config_reporting: false, defer_output: true, interactive_buffer: $stderr, output_buffer: $stdout, paths: ['.'], table_extension: '.txt', with_timestamp: false } command_name = File.basename(__FILE__) OptionParser.new do |options| # This banner is the first line of your help documentation. options.set_banner "Usage: #{command_name} [options] [files]\n" \ "Note taking tool with random table expansion.\n\n" \ "Examples:\n" \ "\t$ #{command_name}\n" \ "\t$ #{command_name} rolls.txt\n" \ "\t$ echo '{name}' | #{command_name}" # Separator just adds a new line with the specified text. options.separator "" options.separator "Specific options:" options.on("-t", "--timestamp", "Append a timestamp to the note (Default: #{config[:with_timestamp].inspect})") do |timestamp| config[:with_timestamp] = timestamp end options.on("-c", "--config_reporting", "Dump the configuration data (Default: #{config[:config_reporting].inspect})") do |config_reporting| config[:config_reporting] = config_reporting end options.on("-d", "--defer_output", "Defer output until system close (Default: #{config[:defer_output].inspect})") do |defer_output| config[:defer_output] = defer_output end options.on("-pPATH", "--path=PATH", String, "Path for {table_name}. files (Default: #{config[:paths].inspect})") do |path| config[:paths] << path end options.on("-tTABLE_EXTENSION", "--table_extension=TABLE_EXTENSION", String, "Path for {table_name}. files (Default: #{config[:table_extension].inspect})") do |table_extension| config[:table_extension] = table_extension end options.on("-l", "--list_tables", "List tables loaded (Default: #{config[:list_tables].inspect})") do |list_tables| config[:list_tables] = list_tables end options.on_tail("-h", "--help", "You're looking at it!") do $stderr.puts options exit 1 end end.parse! if config[:list_tables] notepad = Gm::Notepad.new(config.merge(config_reporting: true)) notepad.process(input: "+") exit(1) end begin @notepad = Gm::Notepad.new(**config) # Keep reading lines of input as long as they're coming. while input = ARGF.gets input.each_line do |input| begin @notepad.process(input: input) rescue Errno::EPIPE # sysexits(3) specifies that exit code 74 represent an IO error, # which is the likely situation @notepad.close! exit(74) end end end ensure @notepad.close! end