コード例 #1
0
ファイル: info.go プロジェクト: logtcn/TMSU
func showUsage(store *storage.Storage, tx *storage.Tx, colour bool) error {
	tagUsages, err := store.TagUsage(tx)
	if err != nil {
		return fmt.Errorf("could not retrieve tag usage: %v", err)
	}

	maxLength := 0
	maxCountWidth := 0

	for _, tagUsage := range tagUsages {
		countWidth := int(math.Log(float64(tagUsage.FileCount)))
		if countWidth > maxCountWidth {
			maxCountWidth = countWidth
		}
		if len(tagUsage.Name) > maxLength {
			maxLength = len(tagUsage.Name)
		}
	}

	fmt.Println()
	for _, tagUsage := range tagUsages {
		fileCount := strconv.FormatUint(uint64(tagUsage.FileCount), 10)
		if colour {
			fileCount = ansi.Yellow(fileCount)
		}

		fmt.Printf("  %*s %*v\n", -maxLength, tagUsage.Name, maxCountWidth, fileCount)
	}

	return nil
}