#!/usr/bin/env ruby -W1 # frozen_string_literal: true $LOAD_PATH.unshift File.join(__dir__, '..', 'lib') require 'snibbets' module Snibbets class << self attr_reader :query def run options = Snibbets.options arguments = Snibbets.arguments optparse = OptionParser.new do |opts| opts.banner = "Usage: #{File.basename(__FILE__)} [options] query" opts.on('-a', '--all', 'If a file contains multiple snippets, output all of them (no menu)') do options[:all] = true end opts.on('-c', '--[no-]copy', 'Copy the output to the clibpoard (also displays on STDOUT)') do |v| options[:copy] = v end opts.on('-e', '--edit', 'Open the selected snippet in your configured editor') do arguments[:edit_snippet] = true end opts.on('-n', '--[no-]name-only', 'Only search file names, not content') do |v| options[:name_only] = v end opts.on('-o', '--output FORMAT', 'Output format (json|launchbar|*raw)') do |outformat| valid = %w[json launchbar lb raw] if outformat.downcase =~ /(launchbar|lb)/ options[:launchbar] = true options[:interactive] = false elsif valid.include?(outformat.downcase) options[:output] = outformat.downcase end end opts.on('-p', '--paste', '--new', 'Interactively create a new snippet from clipboard contents (Mac only)') do arguments[:paste_snippet] = true end opts.on('-q', '--quiet', 'Skip menus and display first match') do options[:interactive] = false options[:launchbar] = false end opts.on('-s', '--source FOLDER', 'Snippets folder to search') do |folder| options[:source] = File.expand_path(folder) end opts.on('--configure', 'Open the configuration file in your default editor') do arguments[:edit_config] = true end opts.on('--[no-]blockquotes', 'Include block quotes in output') do |v| options[:include_blockquotes] = v end opts.on('--[no-]highlight', 'Use pygments or skylighting to syntax highlight (if installed)') do |v| options[:highlight] = v end opts.on('--save', 'Save the current command line options to the YAML configuration') do arguments[:save_config] = true end opts.on('-h', '--help', 'Display this screen') do puts "Snibbets v#{VERSION}" puts puts optparse Process.exit 0 end opts.on('-v', '--version', 'Display version information') do puts "Snibbets v#{VERSION}" Process.exit 0 end end optparse.parse! if arguments[:save_config] config = Snibbets::Config.new config.write_config puts "Configuration saved to #{config.config_file}" end if arguments[:edit_config] config = Snibbets::Config.new config.write_config open_snippet_in_editor(config.config_file) Process.exit 0 end unless File.directory?(options[:source]) puts 'The Snippets folder doesn\'t exist, please configure it.' puts 'Run `snibbets --configure` to open the config file for editing.' Process.exit 1 end if arguments[:paste_snippet] Snibbets.new_snippet_from_clipboard Process.exit 0 end @query = '' if options[:launchbar] @query = if $stdin.stat.size.positive? $stdin.read.force_encoding('utf-8') else ARGV.join(' ') end elsif ARGV.length @query = ARGV.join(' ') end @query = CGI.unescape(@query) if @query.strip.empty? if arguments[:save_config] Process.exit 0 else puts 'No search query' puts optparse Process.exit 1 end end handle_results(search(try: 0)) end end end Snibbets.run