Class: Worktree::TabCompletion::ProjectDirCompletion

Inherits:
Object
  • Object
show all
Defined in:
lib/worktree/tab_completion/project_dir_completion.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_compl) ⇒ ProjectDirCompletion

Returns a new instance of ProjectDirCompletion.



6
7
8
9
10
11
12
# File 'lib/worktree/tab_completion/project_dir_completion.rb', line 6

def initialize(base_compl)
  @base_compl = base_compl.to_s.strip
  if @base_compl.starts_with?('~')
    @replace_home = true
    @base_compl = "#{ENV['HOME']}/#{@base_compl[1..-1]}"
  end
end

Instance Method Details

#listObject



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
# File 'lib/worktree/tab_completion/project_dir_completion.rb', line 14

def list
  if File.directory?(@base_compl)
    base_dir = @base_compl
  else
    # remove tail
    if @base_compl.starts_with?('/')
      if @base_compl.split('/').size == 2
        base_dir = '/'
      else
        base_dir = @base_compl.split('/')[0..-2].join('/')
      end
    else
      base_dir = @base_compl.split('/')[0..-2].join('/')
    end
  end

  base_dir = base_dir.presence || '.'

  # select only folders
  Dir.entries(base_dir.presence).
    select { |f| File.directory? "#{base_dir.chomp('/')}/#{f}" }.
    reject { |d| d == '.' || d == '..' }.
    map do |d|
      if @replace_home
        b = "~#{base_dir[ENV['HOME'].size+1..-1]}"
        "#{b.chomp('/')}/#{d}"
      elsif base_dir == '.'
        d
      else
        b = base_dir
        "#{b.chomp('/')}/#{d}"
      end
    end
end