#!/usr/bin/env ruby require 'gm/notepad' require 'optparse' defaults = Gm::Notepad::Config config = {} 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} filename \n" \ "\t$ echo '{name}' | #{command_name}\n\n" \ "Options:\n" options.on_head("-l", "--list_tables", "List tables loaded and exit (Default: #{defaults.list_tables.inspect})") do |list_tables| config[:list_tables] = list_tables end options.on("-r", "--report_config", "Dump the configuration data (Default: #{defaults.report_config.inspect})") do |report_config| config[:report_config] = report_config end options.on("-pPATH", "--path=PATH", String, "Path(s) for {table_name}. files (Default: #{defaults.paths.inspect})") do |path| config[:paths] ||= [] config[:paths] << path end options.on("-fDIR", "--filesystem_directory=DIR", String, "Path to dump tables (Default: #{defaults.filesystem_directory.inspect})") do |filesystem_directory| # Should we guard that this exists? config[:filesystem_directory] = filesystem_directory end options.on("-xEXT", "--table_extension=EXT", String, "Extension to use for selecting tables (Default: #{defaults.table_extension.inspect})") do |table_extension| config[:table_extension] = table_extension end options.on("-lTTL", "--time_to_live=TTL", Integer, "Per line of input, how many times to allow text expansion (Default: #{defaults.time_to_live.inspect})") do |time_to_live| config[:time_to_live] = time_to_live end options.on("-dDELIM", "--delimiter=DELIM", String, "Default column delimiter for tables (Default: #{defaults.column_delimiter.inspect})") do |column_delimiter| map = { "t" => "\t" } config[:column_delimiter] = map.fetch(column_delimiter) { column_delimiter } end options.separator("") options.separator("Output options:") options.on("-t", "--timestamp", "Append a timestamp to the note (Default: #{defaults.with_timestamp.inspect})") do |timestamp| config[:with_timestamp] = timestamp end options.separator("") options.separator("Color options:") options.on("-i", "--skip-interactive-color", "Disable color rendering for interactive buffer (Default: #{(!defaults.interactive_color).inspect})") do |interactive_color| config[:interactive_color] = !interactive_color end options.on("-o", "--with-output-color", "Enable color rendering for output buffer (Default: #{(!defaults.output_color).inspect})") do |output_color| config[:output_color] = output_color end options.separator("") 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(finalize: true, **config.merge(report_config: true)) notepad.process(text: "+") exit(1) end begin @notepad = Gm::Notepad.new(finalize: true, **config) if Gm::Notepad::Config.skip_readlines input_getter = ARGF.method(:gets) else require 'gm/notepad/readline' input_getter = Gm::Notepad::Readline.input_getter(**config) end while input = input_getter.call # # Keep reading lines of input as long as they're coming. input.each_line do |text| begin @notepad.process(text: text) rescue Errno::EPIPE @notepad.close! # sysexits(3) specifies that exit code 74 represent an IO error, # which is the likely situation exit(74) end end end rescue => e $stderr.puts e ensure @notepad.close! end