<commit_before>package commands

import (
	"context"
	"fmt"
	"io"

	"golang.org/x/sys/windows"

	"github.com/dustin/go-humanize"

	"github.com/zetamatta/nyagos/dos"
)

func driveType(rootPathName string) string {
	_rootPathName, err := windows.UTF16PtrFromString(rootPathName)
	if err != nil {
		return "UNKNOWN"
	}

	t := windows.GetDriveType(_rootPathName)
	switch t {
	case windows.DRIVE_REMOVABLE:
		return "REMOVABLE"
	case windows.DRIVE_FIXED:
		return "FIXED"
	case windows.DRIVE_REMOTE:
		return "REMOTE"
	case windows.DRIVE_CDROM:
		return "CDROM"
	case windows.DRIVE_RAMDISK:
		return "RAMDISK"
	default:
		return "UNKNOWN"
	}
}

func df(rootPathName string, w io.Writer) error {
	label, fs, err := dos.VolumeName(rootPathName)
	if err != nil {
		return err
	}
	free, total, totalFree, err := dos.GetDiskFreeSpace(rootPathName)
	if err != nil {
		return err
	}
	fmt.Fprintf(w, "%s %16s %16s %16s %3d%% \"%s\" (%s/%s)\n",
		rootPathName,
		humanize.Comma(int64(free)),
		humanize.Comma(int64(total)),
		humanize.Comma(int64(totalFree)),
		100*(total-free)/total,
		label,
		fs,
		driveType(rootPathName))
	return nil
}

func cmdDiskFree(_ context.Context, cmd Param) (int, error) {
	bits, err := windows.GetLogicalDrives()
	if err != nil {
		return 0, err
	}
	fmt.Fprintf(cmd.Out(), "   %16s %16s %16s Use%%\n",
		"Available",
		"TotalNumber",
		"TotalNumberOfFree")

	count := 0
	for _, arg1 := range cmd.Args()[1:] {
		if err := df(arg1, cmd.Out()); err != nil {
			return 0, err
		}
		count++
	}
	if count <= 0 {
		for d := 'A'; d <= 'Z'; d++ {
			if (bits & 1) != 0 {
				rootPathName := fmt.Sprintf("%c:\\", d)
				if err := df(rootPathName, cmd.Out()); err != nil {
					fmt.Fprintln(cmd.Err(), err)
				}<commit_msg>diskfree: error shows rootpath<commit_after>package commands

import (
	"context"
	"fmt"
	"io"

	"golang.org/x/sys/windows"

	"github.com/dustin/go-humanize"

	"github.com/zetamatta/nyagos/dos"
)

func driveType(rootPathName string) string {
	_rootPathName, err := windows.UTF16PtrFromString(rootPathName)
	if err != nil {
		return "UNKNOWN"
	}

	t := windows.GetDriveType(_rootPathName)
	switch t {
	case windows.DRIVE_REMOVABLE:
		return "REMOVABLE"
	case windows.DRIVE_FIXED:
		return "FIXED"
	case windows.DRIVE_REMOTE:
		return "REMOTE"
	case windows.DRIVE_CDROM:
		return "CDROM"
	case windows.DRIVE_RAMDISK:
		return "RAMDISK"
	default:
		return "UNKNOWN"
	}
}

func df(rootPathName string, w io.Writer) error {
	label, fs, err := dos.VolumeName(rootPathName)
	if err != nil {
		return fmt.Errorf("%s: %s", rootPathName, err)
	}
	free, total, totalFree, err := dos.GetDiskFreeSpace(rootPathName)
	if err != nil {
		return fmt.Errorf("%s: %s", rootPathName, err)
	}
	fmt.Fprintf(w, "%s %16s %16s %16s %3d%% \"%s\" (%s/%s)\n",
		rootPathName,
		humanize.Comma(int64(free)),
		humanize.Comma(int64(total)),
		humanize.Comma(int64(totalFree)),
		100*(total-free)/total,
		label,
		fs,
		driveType(rootPathName))
	return nil
}

func cmdDiskFree(_ context.Context, cmd Param) (int, error) {
	bits, err := windows.GetLogicalDrives()
	if err != nil {
		return 0, err
	}
	fmt.Fprintf(cmd.Out(), "   %16s %16s %16s Use%%\n",
		"Available",
		"TotalNumber",
		"TotalNumberOfFree")

	count := 0
	for _, arg1 := range cmd.Args()[1:] {
		if err := df(arg1, cmd.Out()); err != nil {
			fmt.Fprintln(cmd.Err(), err)
		}
		count++
	}
	if count <= 0 {
		for d := 'A'; d <= 'Z'; d++ {
			if (bits & 1) != 0 {
				rootPathName := fmt.Sprintf("%c:\\", d)
				if err := df(rootPathName, cmd.Out()); err != nil {
					fmt.Fprintln(cmd.Err(), err)
				}
{'fraction_non_alphanumeric': 0.16676008968609865, 'fraction_numerical': 0.01821748878923767, 'mean_word_length': 3.790604026845638, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2844749', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1416, 'n_words': 318}