Class: Rbnotes::Commands::Pick

Inherits:
Command
  • Object
show all
Defined in:
lib/rbnotes/commands/pick.rb

Overview

Picks a timestamp with a picker program, like ‘fzf`.

Constant Summary collapse

DEFAULT_BEHAVIOR =

:nodoc:

"today"

Instance Method Summary collapse

Instance Method Details

#descriptionObject

:nodoc:



8
9
10
# File 'lib/rbnotes/commands/pick.rb', line 8

def description             # :nodoc:
  "Pick a timestamp with a picker program"
end

#execute(args, conf) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rbnotes/commands/pick.rb', line 14

def execute(args, conf)
  @opts = {}
  parse_opts(args)

  if args.empty?
    default_behavior = conf[:list_default] || DEFAULT_BEHAVIOR
    args << default_behavior
  end

  utils = Rbnotes.utils
  patterns = utils.read_timestamp_patterns(args, enum_week: @opts[:enum_week])

  repo = Textrepo.init(conf)

  num_of_notes = utils.specified_recent?(args) ? conf[:number_of_recent_notes] : 0
  stamps = utils.find_notes(patterns, repo, num_of_notes)
  return if stamps.empty?

  list = []
  stamps.each { |timestamp|
    list << utils.make_headline(timestamp, repo.read(timestamp))
  }

  picker = conf[:picker]
  unless picker.nil?
    picker_opts = conf[:picker_option]
    cmds = [picker]
    cmds.concat(picker_opts.split) unless picker_opts.nil?

    require 'open3'
    result = Open3.pipeline_rw(cmds) { |stdin, stdout, _|
      stdin.puts list
      stdin.close
      stdout.read
    }
    puts result
  else
    puts list
  end
end

#helpObject

:nodoc:



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rbnotes/commands/pick.rb', line 55

def help                    # :nodoc:
  puts <<HELP
usage:
#{Rbnotes::NAME} pick

Pick a timestamp with a picker program, like `fzf`.  This command
refers the configuration setting of ":picker".  If no picker program
is specified, it will behave as same as "list" command.

HELP
end