xonecas.io
5119
Lh

lazy-history / commit 4cead04

chore: better debugging tools

Sean2025-05-12 4cead041e68173884b8e9bf73f58f1c30d43d889 parent aeca3b6

2 files changed

cmd/main.go+11−5
@@ -9,12 +9,17 @@ import (
99 "strings"
1010
1111 tea "github.com/charmbracelet/bubbletea"
12+ "github.com/sacenox/lazy-history/internal/debug"
1213 internal_list "github.com/sacenox/lazy-history/internal/list"
1314 "github.com/sacenox/lazy-history/internal/search"
1415 )
1516
1617 func main() {
17- if os.Getenv("DEBUG") == "true" {
18+ if debug.IsDebug {
19+ // Clear the debug.log file
20+ os.Truncate("debug.log", 0)
21+
22+ // Log to the debug.log file
1823 f, err := tea.LogToFile("debug.log", "debug")
1924 if err != nil {
2025 log.Fatalf("Failed to log to file: %v", err)
@@ -31,6 +36,7 @@ func main() {
3136 filepath.Join(os.Getenv("HOME"), ".bash_history"),
3237 }
3338
39+ // TODO: This is not working, we need to do it in an interactive shell owner by the user.
3440 // Run `history -a` to append the history to the files
3541 exec.Command("history", "-a").Run()
3642
@@ -60,18 +66,18 @@ func main() {
6066 historyFile.Close()
6167 }
6268
63- log.Printf("History length: %d", len(history))
69+ debug.Debugf("History length: %d", len(history))
6470
6571 // Join all args into a single string
6672 query := strings.Join(os.Args[1:], " ")
6773
68- log.Printf("Searching for %s", query)
74+ debug.Debugf("Searching for %s", query)
6975
7076 // Search the history output for the query
7177 results := search.Search(history, query)
7278
73- log.Printf("Found %d results", len(results))
74- log.Printf("Results: %v", results)
79+ debug.Debugf("Found %d results", len(results))
80+ debug.Debugf("Results: %v", results)
7581
7682 ui := internal_list.New(results)
7783
internal/debug/debug.go+15−0
@@ -0,0 +1,15 @@
1+package debug
2+
3+import (
4+ "log"
5+ "os"
6+)
7+
8+var IsDebug = os.Getenv("DEBUG") == "true"
9+
10+// wrap logging functions with a conditional to check if the debug flag is set
11+func Debugf(message string, args ...interface{}) {
12+ if IsDebug {
13+ log.Printf(message, args...)
14+ }
15+}