Module: EasyCreds::Views::ProjectPicker

Defined in:
lib/easy_creds/views/project_picker.rb

Class Method Summary collapse

Class Method Details

.ask(prompt, registry) ⇒ Object

Returns a Projects::Project, :init_here, :templates, :settings, or :quit



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/easy_creds/views/project_picker.rb', line 7

def self.ask(prompt, registry)
  choices = []

  if registry.projects.any?
    choices << { name: Theme.dim('  ── Registered projects ──────────────────────'),
                 value: nil, disabled: '' }
    registry.projects
            .sort_by { |pr| -(pr.last_seen_at&.to_i || 0) }
            .each { |pr| choices << { name: project_row(pr), value: pr } }
  end

  choices << { name: Theme.dim('  ── Actions ──────────────────────────────────'),
               value: nil, disabled: '' }
  choices << { name: "  #{Theme.bold('init here')}     #{Theme.dim('')} register current directory as a project",
               value: :init_here }
  choices << { name: "  #{Theme.bold('templates')}     #{Theme.dim('')} manage global template library",
               value: :templates }
  choices << { name: "  #{Theme.bold('settings')}      #{Theme.dim('')} edit global easy_creds config",
               value: :settings }
  choices << { name: "  #{Theme.dim('quit')}", value: :quit }

  prompt.select(
    "\nWhat would you like to do?",
    choices,
    cycle: true,
    filter: false,
    per_page: [choices.size, 20].min
  )
end

.project_row(pr) ⇒ Object



37
38
39
40
41
42
# File 'lib/easy_creds/views/project_picker.rb', line 37

def self.project_row(pr)
  miss = pr.exists_on_disk? ? '' : "  #{Theme.error('(missing)')}"
  kind = pr.rails? ? Theme.ok('rails') : Theme.accent('standalone')
  seen = relative_time(pr.last_seen_at)
  "  #{Theme.bold(pr.name.ljust(20))} #{kind}  #{Theme.dim(pr.display_path)}  #{Theme.dim(seen)}#{miss}"
end

.relative_time(t) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/easy_creds/views/project_picker.rb', line 44

def self.relative_time(t)
  return '' unless t

  secs = Time.now.utc - t.to_time.utc
  return 'just now' if secs < 60
  return "#{(secs / 60).to_i}m ago"  if secs < 3_600
  return "#{(secs / 3_600).to_i}h ago" if secs < 86_400

  "#{(secs / 86_400).to_i}d ago"
end