Added ability to format directory names too

This commit is contained in:
bytedream 2022-03-20 17:15:44 +01:00
parent dafa2f4d70
commit e9fdd8fd43

View file

@ -167,23 +167,7 @@ func terminalWidth() int {
return 60 return 60
} }
func generateFilename(name, directory string) string { func generateFilename(name string) string {
if runtime.GOOS != "windows" {
for _, char := range invalidNotWindowsChars {
name = strings.ReplaceAll(name, char, "")
}
out.Debug("Replaced invalid characters (not windows)")
} else {
for _, char := range invalidWindowsChars {
name = strings.ReplaceAll(name, char, "")
}
out.Debug("Replaced invalid characters (windows)")
}
if directory != "" {
name = filepath.Join(directory, name)
}
filename, changed := freeFileName(name) filename, changed := freeFileName(name)
if changed { if changed {
out.Info("File %s already exists, changing name to %s", name, filename) out.Info("File %s already exists, changing name to %s", name, filename)
@ -292,7 +276,7 @@ type formatInformation struct {
Subtitle crunchyroll.LOCALE `json:"subtitle"` Subtitle crunchyroll.LOCALE `json:"subtitle"`
} }
func (fi formatInformation) Format(source string) string { func (fi formatInformation) Format(source string, removeInvalidChars bool) string {
fields := reflect.TypeOf(fi) fields := reflect.TypeOf(fi)
values := reflect.ValueOf(fi) values := reflect.ValueOf(fi)
@ -311,6 +295,19 @@ func (fi formatInformation) Format(source string) string {
valueAsString = "no " + valueAsString valueAsString = "no " + valueAsString
} }
} }
if removeInvalidChars {
if runtime.GOOS != "windows" {
for _, char := range invalidNotWindowsChars {
valueAsString = strings.ReplaceAll(valueAsString, char, "")
}
} else {
for _, char := range invalidWindowsChars {
valueAsString = strings.ReplaceAll(valueAsString, char, "")
}
}
}
source = strings.ReplaceAll(source, "{"+fields.Field(i).Tag.Get("json")+"}", valueAsString) source = strings.ReplaceAll(source, "{"+fields.Field(i).Tag.Get("json")+"}", valueAsString)
} }