diff --git a/bytechan_writer.go b/bytechan_writer.go index 5c34760..d61edc5 100644 --- a/bytechan_writer.go +++ b/bytechan_writer.go @@ -1,6 +1,8 @@ package main -import "io" +import ( + "io" +) func NewWriterFromByteChan(ch chan byte) *ByteChanWriter { return &ByteChanWriter{ch} diff --git a/filehandle.go b/filehandle.go new file mode 100644 index 0000000..f5d0475 --- /dev/null +++ b/filehandle.go @@ -0,0 +1,25 @@ +package main + +import ( + "io" +) + +type FileHandle struct { + File string + Chan chan byte + Writer io.Writer +} + +func (fh *FileHandle) Close() error { + close(fh.Chan) + return nil +} + +func NewFileHandle(path string) *FileHandle { + out := make(chan byte, 4096) + return &FileHandle{ + File: path, + Chan: out, + Writer: NewWriterFromByteChan(out), + } +} diff --git a/handle_file.go b/handle_file.go index 9482f6b..22c49ff 100644 --- a/handle_file.go +++ b/handle_file.go @@ -14,21 +14,27 @@ import ( "github.com/fatih/color" ) -func HandleFile(path string) <-chan byte { - out := make(chan byte, 1024) - defer close(out) - w := NewWriterFromByteChan(out) +func HandleFile(fh *FileHandle) { + defer fh.Close() + path := fh.File + w := fh.Writer fmt.Fprint(w, color.MagentaString("%s file found: %s\n", time.Now().Format("2006-01-02 15:04:05"), path)) - for _, parser := range parsers.Parsers { + fmt.Fprint(w, "\ttry parsers: ") + for i, parser := range parsers.Parsers { parsedFile, ok := parser.FileParser(&parser, path) if !ok { - fmt.Fprint(w, color.YellowString("\tnot parsable with parser '%s'\n", parser.Identity)) + fmt.Fprint(w, color.YellowString(parser.Identity)) + if i < len(parsers.Parsers)-1 { + fmt.Fprint(w, ", ") + } else { + fmt.Fprintln(w, color.RedString("\n\tfile ignored")) + } continue } - fmt.Fprint(w, color.GreenString("\tparsable with parser '%s'\n", parser.Identity)) + fmt.Fprintln(w, color.GreenString(parser.Identity)) anime, err := logic.SearchAnimeByTitle(parsedFile.OriginalAnimeTitle) if err != nil { @@ -40,8 +46,6 @@ func HandleFile(path string) <-chan byte { HandleParsedFile(w, parsedFile) break } - - return out } func HandleParsedFile(w io.Writer, parsedFile *model.ParsedFile) { @@ -50,12 +54,12 @@ func HandleParsedFile(w io.Writer, parsedFile *model.ParsedFile) { // debug output if animeEpNotExistLocally { - fmt.Fprint(w, color.YellowString("\tfile exists locally\n")) - fmt.Fprint(w, color.YellowString("\t local file: %s\n", FilePrio2Str(oldFilePrio))) - fmt.Fprint(w, color.YellowString("\t new file: %s\n", FilePrio2Str(newFilePrio))) + fmt.Fprintln(w, "\tfile exists locally") + fmt.Fprintf(w, "\t local file: %s\n", FilePrio2Str(oldFilePrio)) + fmt.Fprintf(w, "\t new file: %s\n", FilePrio2Str(newFilePrio)) if newFilePrio.Priority > oldFilePrio.Priority { fmt.Fprint(w, color.GreenString("\t overwrite local file\n")) - } else { + } else if !DeleteLowPriorityFiles { fmt.Fprint(w, color.YellowString("\t ignore new file\n")) } } @@ -87,7 +91,7 @@ func OrganizeAnimeEpisode(w io.Writer, parsedFile *model.ParsedFile) error { newFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), filepath.Ext(parsedFile.File)) lockFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), "lock") - fmt.Fprint(w, color.BlueString("\tmove file\n\t from: '%s'\n\t to: '%s'\n", oldFile, newFile)) + fmt.Fprintf(w, "\tmove file\n\t from: '%s'\n\t to: '%s'\n", oldFile, newFile) if err := os.MkdirAll(filepath.Dir(newFile), os.ModePerm); err != nil { return err @@ -130,7 +134,7 @@ func OrganizeAnimeEpisode(w io.Writer, parsedFile *model.ParsedFile) error { return err } - fmt.Fprint(w, color.BlueString("\t done (copied %s in %s)\n", FormatBytes(written), time.Since(start).Truncate(100*time.Millisecond))) + fmt.Fprintf(w, "\t done (copied %s in %s)\n", FormatBytes(written), time.Since(start).Truncate(100*time.Millisecond)) return nil } diff --git a/main.go b/main.go index 83bd4a8..d4cecae 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "os" "os/exec" "git.milar.in/milarin/channel" @@ -9,6 +8,8 @@ import ( "github.com/fsnotify/fsnotify" ) +var Runner = InitializeRunner() + func main() { // check for ffprobe in PATH if _, err := exec.LookPath("ffprobe"); err != nil { @@ -22,18 +23,18 @@ func main() { logic.PrintPriorityTables() - fsChan, err := WatchDirectory(fsnotify.Create, DownloadPath) + fileChan, err := WatchDirectory(fsnotify.Create, DownloadPath) if err != nil { panic(err) // TODO error handling } - outputChan := channel.MapWithRunner(fsChan, InitializeRunner(), HandleFile) - channel.Each(outputChan, PrintByteChanFunc(os.Stdout)) + fileHandleChan := channel.Map(fileChan, NewFileHandle) + workChan, logChan := channel.Tee(fileHandleChan) + + go channel.Each(workChan, HandleFileInRunner) + channel.Each(logChan, PrintFileHandle) } -func InitializeRunner() channel.Runner { - if ThreadCount <= 0 { - return channel.NewUnlimitedRunner() - } - return channel.NewLimitedRunner(ThreadCount) +func HandleFileInRunner(fh *FileHandle) { + Runner.Run(func() { HandleFile(fh) }) } diff --git a/utils.go b/utils.go index 3d3df28..8f254ef 100644 --- a/utils.go +++ b/utils.go @@ -3,10 +3,19 @@ package main import ( "fmt" "io" + "os" + "git.milar.in/milarin/channel" "git.milar.in/milarin/gmath" ) +func InitializeRunner() channel.Runner { + if ThreadCount <= 0 { + return channel.NewUnlimitedRunner() + } + return channel.NewLimitedRunner(ThreadCount) +} + func FormatBytes[T gmath.Integer](bytes T) string { value := float64(bytes) @@ -34,10 +43,8 @@ func PrintByteChan(w io.Writer, ch <-chan byte) error { return nil } -func PrintByteChanFunc(w io.Writer) func(ch <-chan byte) { - return func(ch <-chan byte) { - if err := PrintByteChan(w, ch); err != nil { - panic(err) // TODO error handling - } +func PrintFileHandle(fh *FileHandle) { + if err := PrintByteChan(os.Stdout, fh.Chan); err != nil { + panic(err) // TODO error handling } }