1
0
forked from x/icebergs
icebergs/misc/git/count.go
2021-11-05 13:02:50 +08:00

70 lines
1.6 KiB
Go

package git
import (
"strings"
ice "shylinux.com/x/icebergs"
"shylinux.com/x/icebergs/base/nfs"
"shylinux.com/x/icebergs/core/code"
kit "shylinux.com/x/toolkits"
)
const COUNT = "count"
func init() {
Index.Merge(&ice.Context{Commands: map[string]*ice.Command{
COUNT: {Name: "count path auto count", Help: "代码行", Action: map[string]*ice.Action{
COUNT: {Name: "count", Help: "计数", Hand: func(m *ice.Message, arg ...string) {
if len(arg) == 0 || arg[0] == "usr/" {
m.Echo("to many file, please choice sub dir")
return
}
files := map[string]int{}
lines := map[string]int{}
m.Option(nfs.DIR_DEEP, ice.TRUE)
m.Option(nfs.DIR_TYPE, nfs.TYPE_CAT)
m.Cmdy(nfs.DIR, arg, func(file string) {
if strings.Contains(file, "var/") {
return
}
if strings.Contains(file, "bin/") {
return
}
if !strings.Contains(file, ice.PT) {
return
}
switch kit.Ext(file) {
case "sum", "log":
return
}
files[kit.MDB_TOTAL]++
files[kit.Ext(file)]++
m.Cmdy(nfs.CAT, file, func(text string, line int) {
if kit.Ext(file) == code.GO {
switch {
case strings.HasPrefix(text, "func"):
lines["_func"]++
case strings.HasPrefix(text, "type"):
lines["_type"]++
}
}
lines[kit.MDB_TOTAL]++
lines[kit.Ext(file)]++
})
})
for k := range lines {
m.Push(kit.MDB_TYPE, k)
m.Push("files", files[k])
m.Push("lines", lines[k])
}
m.SortIntR("lines")
m.StatusTime()
}},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Cmdy(nfs.DIR, arg)
}},
}})
}