diff --git a/cdhist.go b/cdhist.go index d24053b..6df798f 100644 --- a/cdhist.go +++ b/cdhist.go @@ -74,6 +74,18 @@ func histList(histFile string) error { return nil } +func histListNoPrompt(histFile string) error { + paths, err := ReadLines(histFile) + if err != nil { + return err + } + for _, p := range(paths) { + os.Stdout.WriteString(p) + os.Stdout.WriteString("\n") + } + return nil +} + func DirAliases(aliasFile string) map[string]string { d, err := ReadLines(aliasFile) if err != nil { @@ -100,7 +112,7 @@ func cd(histFile, aliasFile, outString, path string) { } if err != nil { log.Printf("%s: %s", path, err) - os.Exit(2) + os.Exit(3) } histAdd(histFile, cleanPath) fmt.Printf("%s %s", outString, cleanPath) @@ -137,20 +149,24 @@ func ReadLines(fileName string) ([]string, error) { func printBashFunc(opts BashFuncOpts) { f := `{{.FuncName}}() { local d + local r d=$({{.Command}} "$@") - if [ $? -eq 0 ]; then + r=$? + if [[ $r == 0 ]]; then builtin cd $d + elif [[ $r == 2 ]]; then + echo -e "$d" fi }` t, err := template.New("t").Parse(f) if err != nil { log.Printf("Error parsing template, %s", err) - os.Exit(2) + os.Exit(3) } - err = t.Execute(os.Stderr, opts) + err = t.Execute(os.Stdout, opts) if err != nil { log.Printf("Error executing template, %s", err) - os.Exit(2) + os.Exit(3) } return } diff --git a/main.go b/main.go index e5555a8..331f99a 100644 --- a/main.go +++ b/main.go @@ -41,6 +41,7 @@ func main() { } flagInit := flag.Bool("i", false, "Creates function to be sourced by bash") + flagList := flag.Bool("l", false, "List cd history without prompting") flagL := flag.Bool("L", false, "Passed to cd") flagP := flag.Bool("P", false, "Passed to cd") flage := flag.Bool("e", false, "Passed to cd") @@ -55,6 +56,11 @@ func main() { } printBashFunc(BashFuncOpts{FuncName: name, Command: os.Args[0]}) os.Exit(1) + case *flagList: + if err := histListNoPrompt(histFile); err != nil { + log.Printf("Error reading cd history file: %s", err) + } + os.Exit(2) case *flagL: outString = fmt.Sprintf("%s -L", outString) case *flagP: