Class: CodeSnippet::Commands::ShowSnippet

Inherits:
Object
  • Object
show all
Defined in:
lib/code_snippet/commands/show_snippet.rb

Overview

Shows snippet in STDOUT

Instance Method Summary collapse

Constructor Details

#initialize(manager, options = Hashie::Mash.new) ⇒ ShowSnippet

Returns a new instance of ShowSnippet.



8
9
10
11
12
# File 'lib/code_snippet/commands/show_snippet.rb', line 8

def initialize(manager, options = Hashie::Mash.new)
  @manager = manager
  @options = options
  @prompt = TTY::Prompt.new
end

Instance Method Details

#find_snippets(name) ⇒ Object



35
36
37
38
39
40
# File 'lib/code_snippet/commands/show_snippet.rb', line 35

def find_snippets(name)
  snips = @manager.find(name)
  raise "unable to find #{name}" if snips.empty?

  snips
end

#pick_snippet(snips) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/code_snippet/commands/show_snippet.rb', line 42

def pick_snippet(snips)
  snip = snips.first
  if snips.length > 1
    snip_name = @prompt.select(
      'Multiple snippets found, which one would you like to view?',
      snips.map(&:name)
    )

    snip = @manager.find(snip_name).first
  end

  snip
end

#runObject

Prints snippet path



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/code_snippet/commands/show_snippet.rb', line 17

def run
  name = @options.name
  name ||= @prompt.select(
    'Please choose snippet',
    @manager.snippet_names
  )

  snips = find_snippets(name)
  snip = pick_snippet(snips)

  if @options.copy
    Clipboard.copy(snip.content)
    puts "COPIED: #{snip.path}"
  end

  puts(snip.content)
end