Add untracked files

This commit is contained in:
Jan Bader 2023-12-09 15:10:35 +01:00
parent c667707704
commit 43d2ced820
2 changed files with 95 additions and 0 deletions

46
clustering.go Normal file
View File

@ -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
}

49
ui.go Normal file
View File

@ -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)
}
}
}