-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
file.go
160 lines (151 loc) · 3.89 KB
/
file.go
1
2
3
4
5
6
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// This file is part of go-getoptions.
//
// Copyright (C) 2015-2024 David Gamba Rios
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package completion
import (
"os"
"path/filepath"
"sort"
"strings"
)
// readDirNoSort - Same as ioutil/ReadDir but doesn't sort results.
//
// Taken from https://golang.org/src/io/ioutil/ioutil.go
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
func readDirNoSort(dirname string) ([]os.FileInfo, error) {
f, err := os.Open(dirname)
if err != nil {
return nil, err
}
list, err := f.Readdir(-1)
f.Close()
if err != nil {
return nil, err
}
return list, nil
}
// trimLeftDots - Given a string it trims the leading dots (".") and returns a count of how many were removed.
func trimLeftDots(s string) (int, string) {
charFound := false
count := 0
return count, strings.TrimLeftFunc(s, func(r rune) bool {
if !charFound && r == '.' {
count++
return true
}
return false
})
}
// trimLeftDashes - Given a string it trims the leading dashes ("-") and returns a count of how many were removed.
func trimLeftDashes(s string) (int, string) {
charFound := false
count := 0
return count, strings.TrimLeftFunc(s, func(r rune) bool {
if !charFound && r == '-' {
count++
return true
}
return false
})
}
// sortForCompletion - Places hidden files in the same sort position as their non hidden counterparts.
// Also used for sorting options in the same fashion.
// Example:
//
// file.txt
// .file.txt.~
// .hidden.txt
// ..hidden.txt.~
//
// -d
// --debug
// -h
// --help
func sortForCompletion(list []string) {
sort.Slice(list,
func(i, j int) bool {
var a, b string
if filepath.Dir(list[i]) == filepath.Dir(list[j]) {
a = filepath.Base(list[i])
b = filepath.Base(list[j])
} else {
a = list[i]
b = list[j]
}
// . always is less
if filepath.Base(list[i]) == "." {
return true
}
if filepath.Base(list[j]) == "." {
return false
}
// .. is always less in any other case
if filepath.Base(list[i]) == ".." {
return true
}
if filepath.Base(list[j]) == ".." {
return false
}
an, a := trimLeftDots(a)
bn, b := trimLeftDots(b)
if a == b {
return an < bn
}
an, a = trimLeftDashes(a)
bn, b = trimLeftDashes(b)
if a == b {
return an < bn
}
return a < b
})
}
// listDir - Given a dir and a prefix returns a list of files in the dir filtered by their prefix.
// NOTE: dot (".") is a valid dirname.
func listDir(dirname string, prefix string) ([]string, error) {
filenames := []string{}
usedDirname := dirname
dir := ""
if strings.Contains(prefix, "/") {
dir = filepath.Dir(prefix) + string(os.PathSeparator)
prefix = strings.TrimPrefix(prefix, dir)
usedDirname = filepath.Join(dirname, dir) + string(os.PathSeparator)
}
switch prefix {
case ".":
filenames = append(filenames, dir+"./")
filenames = append(filenames, dir+"../")
case "..":
filenames = append(filenames, dir+"../")
}
fileInfoList, err := readDirNoSort(usedDirname)
if err != nil {
Debug.Printf("listDir - dirname %s, prefix %s > files %v\n", dirname, prefix, filenames)
return filenames, err
}
for _, fi := range fileInfoList {
name := fi.Name()
if !strings.HasPrefix(name, prefix) {
continue
}
if dirname != usedDirname {
name = filepath.Join(dir, name)
}
if fi.IsDir() {
filenames = append(filenames, name+"/")
} else {
filenames = append(filenames, name)
}
}
sortForCompletion(filenames)
if len(filenames) == 1 && strings.HasSuffix(filenames[0], "/") {
filenames = append(filenames, filenames[0]+" ")
}
Debug.Printf("listDir - dirname %s, prefix %s > files %v\n", dirname, prefix, filenames)
return filenames, err
}