#!/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 display_changelog changelog = File.expand_path(File.join(__dir__, '..', 'CHANGELOG.md')) pagers = [ 'mdless', 'mdcat -p', 'bat', ENV['PAGER'], 'less -FXr', ENV['GIT_PAGER'], 'more -r' ] pager = pagers.find { |cmd| TTY::Which.exist?(cmd.split.first) } system %(#{TTY::Which.which(pager)} "#{changelog}") end def run options = Snibbets.options arguments = Snibbets.arguments Color.coloring = $stdout.isatty 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('--[no-]notes', 'Display the full content of the snippet') do |v| options[:all_notes] = 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('--nvultra', 'Open the result in nvUltra') do arguments[:nvultra] = true 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 "{by}Snibbets {w}v#{VERSION}".x puts puts optparse Process.exit 0 end opts.on('-v', '--version', 'Display version information') do puts "{by}Snibbets {w}v#{VERSION}".x Process.exit 0 end opts.on('--changes', 'Display the changelog (release notes)') do display_changelog Process.exit 0 end end optparse.parse! if arguments[:save_config] config = Snibbets::Config.new config.write_config puts "{bg}Configuration saved to {w}#{config.config_file}".x 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 '{br}The Snippets folder doesn\'t exist, please configure it.'.x puts '{bg}Run `{bw}snibbets --configure{bg}` to open the config file for editing.'.x 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 '{br}No search query'.x puts optparse Process.exit 1 end end handle_results(search(try: 0)) end end end Snibbets.run