-
Notifications
You must be signed in to change notification settings - Fork 0
/
filetype.go
57 lines (51 loc) · 1.25 KB
/
filetype.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
package nyne
import (
"fmt"
"strings"
)
// Command contains options for executing a given command against an
// acme window
type Command struct {
Exec string
Args []string
PrintsToStdout bool
}
// Filetype contains the formatting specification for a given file extension
type Filetype struct {
Name string
Extensions []string
Tabwidth int
Tabexpand bool
Comment string
Commands []Command
}
// Extension parses the file extension given a file name
func Extension(in string, def string) string {
filename := Filename(in)
if !strings.Contains(filename, ".") {
return filename
}
pts := strings.Split(filename, ".")
if len(pts) == len(in) {
return def
}
return "." + pts[len(pts)-1]
}
// Filename takes the absolute path to a file and returns just the name
// of the file
func Filename(in string) string {
path := strings.Split(in, "/")
return path[len(path)-1]
}
// FillFiletypes updates the filetypes in the given map
func FillFiletypes(dst map[string]Filetype, src []Filetype) error {
for _, ft := range src {
for _, ext := range ft.Extensions {
if ft2, ok := dst[ext]; ok {
return fmt.Errorf("duplicate extension for filetype: original=%+v new=%+v", ft2, ft)
}
dst[ext] = ft
}
}
return nil
}