mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-07-12 04:09:48 +00:00
cli: mode=byte by default + fix file reading
Signed-off-by: Alexander Bezzubov <bzz@apache.org>
This commit is contained in:
@ -30,7 +30,7 @@ func main() {
|
|||||||
jsonFlag := flag.Bool("json", false, "")
|
jsonFlag := flag.Bool("json", false, "")
|
||||||
showVersion := flag.Bool("version", false, "Show the enry version information")
|
showVersion := flag.Bool("version", false, "Show the enry version information")
|
||||||
allLangs := flag.Bool("all", false, "Show not only the files with programming languages (default) but all languages instead")
|
allLangs := flag.Bool("all", false, "Show not only the files with programming languages (default) but all languages instead")
|
||||||
countMode := flag.String("mode", "file", "the method used to count file size. Available options are: file, line and byte")
|
countMode := flag.String("mode", "byte", "the method used to count file size. Available options are: file, line and byte")
|
||||||
limitKB := flag.Int64("limit", 16*1024, "Analyse first N KB of the file (-1 means no limit)")
|
limitKB := flag.Int64("limit", 16*1024, "Analyse first N KB of the file (-1 means no limit)")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
limit := (*limitKB) * 1024
|
limit := (*limitKB) * 1024
|
||||||
@ -136,11 +136,11 @@ func main() {
|
|||||||
case *jsonFlag && *breakdownFlag:
|
case *jsonFlag && *breakdownFlag:
|
||||||
printBreakDown(out, &buf)
|
printBreakDown(out, &buf)
|
||||||
case *breakdownFlag:
|
case *breakdownFlag:
|
||||||
printPercents(out, &buf, *countMode)
|
printPercents(root, out, &buf, *countMode)
|
||||||
buf.WriteByte('\n')
|
buf.WriteByte('\n')
|
||||||
printBreakDown(out, &buf)
|
printBreakDown(out, &buf)
|
||||||
default:
|
default:
|
||||||
printPercents(out, &buf, *countMode)
|
printPercents(root, out, &buf, *countMode)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Print(buf.String())
|
fmt.Print(buf.String())
|
||||||
@ -182,9 +182,9 @@ func (e filelistError) Error() string {
|
|||||||
return fmt.Sprintf("Could not process the following files:\n%s", strings.Join(e, "\n"))
|
return fmt.Sprintf("Could not process the following files:\n%s", strings.Join(e, "\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func printPercents(fSummary map[string][]string, buff *bytes.Buffer, mode string) {
|
func printPercents(root string, fSummary map[string][]string, buff *bytes.Buffer, mode string) {
|
||||||
// Select the way we quantify 'amount' of code.
|
// Select the way we quantify 'amount' of code.
|
||||||
var reducer func([]string) (float64, filelistError)
|
var reducer func(string, []string) (float64, filelistError)
|
||||||
switch mode {
|
switch mode {
|
||||||
case "file":
|
case "file":
|
||||||
reducer = fileCountValues
|
reducer = fileCountValues
|
||||||
@ -204,7 +204,8 @@ func printPercents(fSummary map[string][]string, buff *bytes.Buffer, mode string
|
|||||||
fileValues = make(map[string]float64)
|
fileValues = make(map[string]float64)
|
||||||
)
|
)
|
||||||
for fType, files := range fSummary {
|
for fType, files := range fSummary {
|
||||||
val, err := reducer(files)
|
//FIXME(bzz): all files here have relative paths
|
||||||
|
val, err := reducer(root, files)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
unreadableFiles = append(unreadableFiles, err...)
|
unreadableFiles = append(unreadableFiles, err...)
|
||||||
}
|
}
|
||||||
@ -229,25 +230,25 @@ func printPercents(fSummary map[string][]string, buff *bytes.Buffer, mode string
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileCountValues(files []string) (float64, filelistError) {
|
func fileCountValues(_ string, files []string) (float64, filelistError) {
|
||||||
return float64(len(files)), nil
|
return float64(len(files)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func lineCountValues(files []string) (float64, filelistError) {
|
func lineCountValues(root string, files []string) (float64, filelistError) {
|
||||||
var filesErr filelistError
|
var filesErr filelistError
|
||||||
var t float64
|
var t float64
|
||||||
for _, fName := range files {
|
for _, fName := range files {
|
||||||
l, _ := getLines(fName, nil)
|
l, _ := getLines(filepath.Join(root, fName), nil)
|
||||||
t += float64(l)
|
t += float64(l)
|
||||||
}
|
}
|
||||||
return t, filesErr
|
return t, filesErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func byteCountValues(files []string) (float64, filelistError) {
|
func byteCountValues(root string, files []string) (float64, filelistError) {
|
||||||
var filesErr filelistError
|
var filesErr filelistError
|
||||||
var t float64
|
var t float64
|
||||||
for _, fName := range files {
|
for _, fName := range files {
|
||||||
f, err := os.Open(fName)
|
f, err := os.Open(filepath.Join(root, fName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
filesErr = append(filesErr, fName)
|
filesErr = append(filesErr, fName)
|
||||||
continue
|
continue
|
||||||
|
Reference in New Issue
Block a user