diff --git a/clustering.go b/clustering.go new file mode 100644 index 0000000..4c02dbb --- /dev/null +++ b/clustering.go @@ -0,0 +1,46 @@ +package main + +import ( + "slices" + + "github.com/steakknife/hamming" +) + +type imageCluster struct { + images []similarImage +} + +type similarImage struct { + path string + distance int +} + +func (fm *FilesMap) getImageClusters() []imageCluster { + var clusters []imageCluster + + for len(fm.Images) > 0 { + file := fm.Images[0] + fm.Images = slices.Delete(fm.Images, 0, 1) + + var currentCluster []similarImage + currentCluster = append(currentCluster, similarImage{path: file.path}) + for otherIndex := len(fm.Images) - 1; otherIndex >= 0; otherIndex-- { + otherFile := fm.Images[otherIndex] + var distance = hamming.Uint64(file.imageHash, otherFile.imageHash) + if distance > 5 { + continue + } + + fm.Images = slices.Delete(fm.Images, otherIndex, otherIndex+1) + currentCluster = append(currentCluster, similarImage{path: otherFile.path, distance: distance}) + } + + if len(currentCluster) > 1 { + continue + } + + clusters = append(clusters, imageCluster{images: currentCluster}) + } + + return clusters +} diff --git a/ui.go b/ui.go new file mode 100644 index 0000000..d3b62a0 --- /dev/null +++ b/ui.go @@ -0,0 +1,49 @@ +package main + +import ( + "bufio" + "fmt" + "strconv" + "strings" +) + +func promptForDeletion(reader *bufio.Reader, files []string) { + fmt.Print("\033[H\033[2J") + for i, file := range files { + fmt.Println(i+1, file) + } + fmt.Println(0, "Keep all") + + fmt.Printf("Which file to keep? ") + input, err := reader.ReadString('\n') + if err != nil { + fmt.Println("Invalid input") + return + } + + input = strings.TrimRight(input, "\n\r") + intInput, err := strconv.Atoi(input) + if err != nil { + fmt.Println("Invalid input") + return + } + + if intInput == 0 { + return + } + + if intInput > len(files) || intInput < 1 { + fmt.Println("Invalid input") + return + } + + for i, file := range files { + if i+1 == intInput { + continue + } + + if *force { + remove(file) + } + } +}