Skip to content

Commit

Permalink
Feature to convert whole directories added | cyberpunk,githubLight,ni…
Browse files Browse the repository at this point in the history
…ght-owl themes added
  • Loading branch information
Achno committed Aug 1, 2024
1 parent 29150e4 commit b99a2b1
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 22 deletions.
13 changes: 13 additions & 0 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"fmt"
"strings"

"github.com/Achno/gowall/internal/image"
"github.com/Achno/gowall/utils"
Expand All @@ -29,6 +30,18 @@ var convertCmd = &cobra.Command{
processor := &image.ThemeConverter{}
expandedFiles := utils.ExpandHomeDirectory(batchFiles)
image.ProcessBatchImgs(expandedFiles, theme, processor)

case strings.HasSuffix(args[0],"#") :
fmt.Println("Processing directory...")
processor := &image.ThemeConverter{}
path := utils.DiscardLastCharacter(args[0])
files ,err := utils.ExpandHashtag(path)

if err != nil {
fmt.Printf("Error ExpandingHashTag: %s\n",err)
return
}
image.ProcessBatchImgs(files,theme,processor)

case len(args) > 0:
fmt.Println("Processing single image...")
Expand Down
14 changes: 14 additions & 0 deletions cmd/invert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"fmt"
"strings"

"github.com/Achno/gowall/internal/image"
"github.com/Achno/gowall/utils"
Expand All @@ -28,6 +29,19 @@ var invertCmd = &cobra.Command{
expandedFiles := utils.ExpandHomeDirectory(batchFiles)
image.ProcessBatchImgs(expandedFiles,theme,processor)

case strings.HasSuffix(args[0],"#") :
fmt.Println("Processing directory...")
processor := &image.Inverter{}
path := utils.DiscardLastCharacter(args[0])
files ,err := utils.ExpandHashtag(path)

if err != nil {
fmt.Printf("Error ExpandingHashTag: %s\n",err)
return
}
image.ProcessBatchImgs(files,theme,processor)


case len(args) > 0:
fmt.Println("Processing single image...")
processor := &image.Inverter{}
Expand Down
85 changes: 85 additions & 0 deletions internal/image/themes.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ var themes = map[string]Theme{
"sunset-aurant": SunsetAurant,
"sunset-saffron": SunsetSaffron,
"sunset-tangerine": SunsetTangerine,
"cyberpunk": Cyberpunk,
"night-owl": NightOwl,
"github-light": GitHubLight,
}

func init() {
Expand Down Expand Up @@ -703,5 +706,87 @@ var (
},
}

Cyberpunk = Theme{
Name: "Cyber-punk",
Colors: []color.Color{
color.RGBA{R: 0, G: 0, B: 0, A: 255},
color.RGBA{R: 255, G: 0, B: 255, A: 255},
color.RGBA{R: 255, G: 255, B: 0, A: 255},
color.RGBA{R: 0, G: 255, B: 255, A: 255},
color.RGBA{R: 0, G: 255, B: 0, A: 255},
color.RGBA{R: 255, G: 0, B: 0, A: 255},
color.RGBA{R: 0, G: 0, B: 255, A: 255},
color.RGBA{R: 255, G: 165, B: 0, A: 255},
color.RGBA{R: 75, G: 0, B: 130, A: 255},
color.RGBA{R: 238, G: 130, B: 238, A: 255},
color.RGBA{R: 135, G: 206, B: 235, A: 255},
color.RGBA{R: 255, G: 105, B: 180, A: 255},
color.RGBA{R: 139, G: 0, B: 255, A: 255},
color.RGBA{R: 255, G: 20, B: 147, A: 255},
color.RGBA{R: 0, G: 128, B: 128, A: 255},
color.RGBA{R: 255, G: 0, B: 255, A: 255},
color.RGBA{R: 0, G: 0, B: 139, A: 255},
color.RGBA{R: 255, G: 69, B: 0, A: 255},
color.RGBA{R: 64, G: 224, B: 208, A: 255},
color.RGBA{R: 186, G: 85, B: 211, A: 255},
color.RGBA{R: 255, G: 182, B: 193, A: 255},
},
}


NightOwl = Theme{
Name: "Night-owl",
Colors: []color.Color{
color.RGBA{R: 0, G: 43, B: 54, A: 255},
color.RGBA{R: 7, G: 54, B: 66, A: 255},
color.RGBA{R: 88, G: 110, B: 117, A: 255},
color.RGBA{R: 101, G: 123, B: 131, A: 255},
color.RGBA{R: 147, G: 161, B: 161, A: 255},
color.RGBA{R: 203, G: 75, B: 22, A: 255},
color.RGBA{R: 88, G: 110, B: 117, A: 255},
color.RGBA{R: 39, G: 150, B: 135, A: 255},
color.RGBA{R: 0, G: 113, B: 133, A: 255},
color.RGBA{R: 211, G: 54, B: 130, A: 255},
color.RGBA{R: 131, G: 148, B: 150, A: 255},
color.RGBA{R: 52, G: 101, B: 36, A: 255},
color.RGBA{R: 229, G: 229, B: 229, A: 255},
color.RGBA{R: 191, G: 97, B: 106, A: 255},
color.RGBA{R: 236, G: 139, B: 67, A: 255},
color.RGBA{R: 85, G: 139, B: 47, A: 255},
color.RGBA{R: 102, G: 120, B: 105, A: 255},
color.RGBA{R: 0, G: 128, B: 128, A: 255},
color.RGBA{R: 240, G: 232, B: 196, A: 255},
color.RGBA{R: 124, G: 45, B: 75, A: 255},
},
}

GitHubLight = Theme{
Name: "GitHub-Light",
Colors: []color.Color{
color.RGBA{R: 255, G: 255, B: 255, A: 255},
color.RGBA{R: 243, G: 243, B: 243, A: 255},
color.RGBA{R: 235, G: 235, B: 235, A: 255},
color.RGBA{R: 189, G: 189, B: 189, A: 255},
color.RGBA{R: 102, G: 102, B: 102, A: 255},
color.RGBA{R: 81, G: 81, B: 81, A: 255},
color.RGBA{R: 0, G: 0, B: 0, A: 255},
color.RGBA{R: 69, G: 69, B: 69, A: 255},
color.RGBA{R: 238, G: 0, B: 0, A: 255},
color.RGBA{R: 255, G: 153, B: 51, A: 255},
color.RGBA{R: 34, G: 139, B: 34, A: 255},
color.RGBA{R: 0, G: 0, B: 255, A: 255},
color.RGBA{R: 148, G: 0, B: 211, A: 255},
color.RGBA{R: 75, G: 0, B: 130, A: 255},
color.RGBA{R: 102, G: 51, B: 153, A: 255},
color.RGBA{R: 204, G: 204, B: 204, A: 255},
color.RGBA{R: 170, G: 170, B: 170, A: 255},
color.RGBA{R: 120, G: 120, B: 120, A: 255},
color.RGBA{R: 170, G: 119, B: 204, A: 255},
color.RGBA{R: 255, G: 69, B: 0, A: 255},
color.RGBA{R: 255, G: 105, B: 180, A: 255},
color.RGBA{R: 153, G: 204, B: 255, A: 255},
},
}


)
72 changes: 72 additions & 0 deletions utils/expand.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package utils

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

// Function to expand the tilde (~) to the full home directory path
// @Example ~/Pictures/flowers.png --> /home/username/Pictures/flowers.png
func ExpandHomeDirectory(paths []string) []string {
var expandedPaths []string
homeDir, _ := os.UserHomeDir()

for _, path := range paths {
if strings.HasPrefix(path, "~") {
path = filepath.Join(homeDir, path[1:])
}
expandedPaths = append(expandedPaths, path)
}
return expandedPaths
}

// Function to expand the delimeter '#' to every file under that directory
// Example "~/Pictures/#" -->["Pictures/img1.png","~/Pictures/img2.png","~/Pictures/img3.png"]
func ExpandHashtag(pathWithHashtag string) ([]string, error) {

path := DiscardLastCharacter(pathWithHashtag)

ImgPaths, err := expandToImgFiles(path)

if err != nil{
return nil,fmt.Errorf("error expanding to image files: %w",err)
}

return ImgPaths,nil

}

// Expands a directory to only image files of type .png .jpeg .jpg .webp
// Example "~/Pictures/" -->["Pictures/img1.png","~/Pictures/img2.png","~/Pictures/img3.png"]
func expandToImgFiles(path string) ( []string, error){

filePaths , err := os.ReadDir(path)

if err != nil{
return nil,err
}

images,err := filterImages(filePaths)

if err != nil{
return nil,err
}

if len(images) == 0 {
return nil,fmt.Errorf("no image files in directory")
}

contents := make([]string,len(images))

for i, img := range images{
fullPath := filepath.Join(path,img)
contents[i] = fullPath
// fmt.Println(fullPath) debugging
}

return contents,nil
}


22 changes: 0 additions & 22 deletions utils/expandToHomeDirectory.go

This file was deleted.

48 changes: 48 additions & 0 deletions utils/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package utils

import (
"fmt"
"io/fs"
"path/filepath"
"strings"
"unicode/utf8"
)

// filters out all files other than .png .jpeg .jpg .webp in a directory
func filterImages(entries []fs.DirEntry) ([]string, error){

if len(entries) == 0{
return nil,fmt.Errorf("directory is empty")
}

var imageFiles []string

supportedExtensions := map[string]bool{
".png": true,
".jpeg": true,
".jpg": true,
".webp": true,
}

for _ , entry := range entries{

if !entry.IsDir() && supportedExtensions[strings.ToLower(filepath.Ext(entry.Name()))]{
imageFiles = append(imageFiles,entry.Name())
}
}

return imageFiles,nil

}

func DiscardLastCharacter(s string) string {
if len(s) == 0 {
return s
}

// Decode the last rune
_, size := utf8.DecodeLastRuneInString(s)

// exclude the last character
return s[:len(s)-size]
}

0 comments on commit b99a2b1

Please sign in to comment.