diff --git a/gif.go b/gif.go index b47c843..eec97fe 100644 --- a/gif.go +++ b/gif.go @@ -8,7 +8,7 @@ import ( "strings" ) -func generateGIF(outputPath, theme string, lines []string, cardCount int) error { +func generateGIF(outputPath, theme string, dir string, lines []string, cardCount int) error { // check vhs installed vhsPath, err := exec.LookPath("vhs") if err != nil { @@ -62,9 +62,14 @@ func generateGIF(outputPath, theme string, lines []string, cardCount int) error tape.WriteString("Set TypingSpeed 0\n") cmd := selfPath + // perhaps we need some better logic here? if theme != "default" { cmd += " --theme " + theme } + if dir != "" { + cmd += " " + dir + } + tape.WriteString(fmt.Sprintf("Type \"%s\"\n", cmd)) tape.WriteString("Enter\n") tape.WriteString(fmt.Sprintf("Sleep %ds\n", duration)) diff --git a/git.go b/git.go index 727ec28..49e3a58 100644 --- a/git.go +++ b/git.go @@ -25,12 +25,22 @@ type contributor struct { commits int } -func getRepoInfo() repoInfo { +func getRepoInfo(dir string) repoInfo { info := repoInfo{} + currentDir := "" + + if dir != "" { + if wd, err := os.Getwd(); err != nil { + fmt.Fprintf(os.Stderr, "Error while attempting check current working directory \"%s\": %v\n", dir, err) + os.Exit(1) + } else { + currentDir = wd + } - if dir, err := os.Getwd(); err == nil { - parts := strings.Split(dir, string(os.PathSeparator)) - info.name = parts[len(parts)-1] + if err := os.Chdir(dir); err != nil { + fmt.Fprintf(os.Stderr, "Error while attempting to navigate to directory \"%s\": %v\n", dir, err) + os.Exit(1) + } } if desc, err := os.ReadFile(".git/description"); err == nil { @@ -116,6 +126,14 @@ func getRepoInfo() repoInfo { } } + // go back to the dirrectory we were when the function was invoked + if currentDir != "" { + if err := os.Chdir(currentDir); err != nil { + fmt.Fprintf(os.Stderr, "Error while attempting to navigate back to start directory \"%s\": %v\n", dir, err) + os.Exit(1) + } + } + return info } diff --git a/main.go b/main.go index 0d5154d..d24fe60 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "strings" "os" tea "github.com/charmbracelet/bubbletea" @@ -17,6 +18,7 @@ func main() { // parse flags theme := "default" output := "" + dir := "" for i, arg := range os.Args[1:] { if arg == "--version" || arg == "-v" { fmt.Printf("gitcredits %s (%s)\n", version, commit) @@ -25,7 +27,10 @@ func main() { if arg == "--help" || arg == "-h" { fmt.Println("gitcredits - Turn your Git repo into movie-style rolling credits") fmt.Println() - fmt.Printf("Usage: gitcredits [options]\n\n") + fmt.Printf("Usage: gitcredits [options] \n\n") + fmt.Println("Arguments:") + fmt.Println(" Directory to look for a git repository (if not supplied, defaults to current directory)") + fmt.Println() fmt.Println("Options:") fmt.Println(" --theme Theme: default, matrix, spiderman") fmt.Println(" --output Export credits as GIF") @@ -41,7 +46,13 @@ func main() { } } - info := getRepoInfo() + if len(os.Args) > 1 { + if !strings.HasPrefix(os.Args[len(os.Args)-2], "--") { + dir = os.Args[len(os.Args)-1] + } + } + + info := getRepoInfo(dir) width := 80 height := 24 @@ -58,7 +69,7 @@ func main() { default: cards = buildMatrixCards(info, 80, 24) } - if err := generateGIF(output, theme, credits, len(cards)); err != nil { + if err := generateGIF(output, theme, dir, credits, len(cards)); err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err) os.Exit(1) }