Skip to content

Commit

Permalink
Added holoscene processing
Browse files Browse the repository at this point in the history
Signed-off-by: Sandy <sandy@sandyuraz.com>
  • Loading branch information
thecsw committed Jan 6, 2023
1 parent dd2f035 commit bc41046
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 6 deletions.
12 changes: 10 additions & 2 deletions ichika/galleries.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,19 @@ func resizeAndBlur(img image.Image) (*image.NRGBA, error) {
return blurred, nil
}

func dryRemove(val string) error {
return nil
}

// removeGalleryFiles removes all generate gallery previews.
func removeGalleryFiles() {
func removeGalleryFiles(dryRun bool) {
removeFunc := os.Remove
if dryRun {
removeFunc = dryRemove
}
for _, galleryFile := range getGalleryFiles() {
newFile := emilia.GalleryPreview(galleryFile)
if err := os.Remove(string(newFile)); err != nil && !os.IsNotExist(err) {
if err := removeFunc(string(newFile)); err != nil && !os.IsNotExist(err) {
fmt.Println("Couldn't delete", newFile, "| reason:", err.Error())
}
}
Expand Down
100 changes: 100 additions & 0 deletions ichika/holoscene.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package ichika

import (
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/thecsw/darkness/emilia"
)

const (
holosceneTitlesTempDir = "temp-holoscene"
)

func updateHolosceneTitles(dryRun bool) {
if dryRun {
if err := os.Mkdir(holosceneTitlesTempDir, 0755); err != nil {
fmt.Printf("Failed to create temp dir: %s", err)
os.Exit(1)
}
}

inputs := emilia.FindFilesByExtSimple(emilia.Config.Project.Input)
outputs := make([]string, len(inputs))
for i, v := range inputs {
outputs[i] = emilia.InputFilenameToOutput(v)
}

actuallyFound := make([]*os.File, 0, len(outputs))
for _, v := range outputs {
file, err := os.Open(v)
if err != nil {
fmt.Printf("Couldn't open %s: %s\n", v, err)
continue
}
actuallyFound = append(actuallyFound, file)
}

fmt.Printf("Adding holoscene titles to %d output files\n",
len(actuallyFound))

for _, foundOutput := range actuallyFound {
filename := foundOutput.Name()
output, err := io.ReadAll(foundOutput)
if err := foundOutput.Close(); err != nil {
fmt.Printf("Failed to close %s: %s\n",
filename, err)
}
if err != nil {
fmt.Printf("Couldn't read %s: %s\n",
filename, err)
continue
}

newOutput := emilia.AddHolosceneTitles(string(output), -1)
var file *os.File
if dryRun {
file, err = os.CreateTemp(holosceneTitlesTempDir,
filepath.Base(filename))
} else {
file, err = os.Create(filename)
}
if err != nil {
fmt.Printf("Failed to overwrite %s: %s\n",
filename, err)
continue
}

if len(newOutput) == len(string(output)) {
fmt.Printf("Skipping %s (no changes)\n", filename)
file.Close()
continue
}

written, err := io.Copy(file, strings.NewReader(newOutput))
if err := file.Close(); err != nil {
fmt.Printf("Failed to close (2) %s: %s\n",
file.Name(), err)
}
if err != nil {
fmt.Printf("Failed to write %s: %s\n", file.Name(), err)
continue
}

fmt.Printf("Wrote %d bytes to %s", written, file.Name())
if dryRun {
fmt.Printf(": %s",
strings.TrimPrefix(filename, emilia.Config.WorkDir))
}
fmt.Println()
}

if dryRun {
if err := os.RemoveAll(holosceneTitlesTempDir); err != nil {
fmt.Printf("Failed to clear temp dir: %s\n", err)
}
}
}
12 changes: 8 additions & 4 deletions ichika/misa.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func MisaCommandFunc() {

buildGalleryPreviews := misaCmd.Bool("gallery-previews", false, "build gallery previews")
removeGalleryPreviews := misaCmd.Bool("no-gallery-previews", false, "delete gallery previews")
addHolosceneTitles := misaCmd.Bool("holoscene-titles", false, "add holoscene titles")
dryRun := misaCmd.Bool("dry-run", false, "skip writing files (but do the reading)")

options := getEmiliaOptions(misaCmd)
Expand All @@ -21,12 +22,15 @@ func MisaCommandFunc() {

if *buildGalleryPreviews {
buildGalleryFiles(*dryRun)
return
}
if *removeGalleryPreviews {
removeGalleryFiles()
return
removeGalleryFiles(*dryRun)
}
if *addHolosceneTitles {
updateHolosceneTitles(*dryRun)
}

fmt.Println("I don't know what you want me to do, see -help")
if misaCmd.NFlag() == 0 {
fmt.Println("I don't know what you want me to do, see -help")
}
}

0 comments on commit bc41046

Please sign in to comment.