-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature to convert whole directories added | cyberpunk,githubLight,ni…
…ght-owl themes added
- Loading branch information
Showing
6 changed files
with
232 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |