internal/curd.go

71 lines · 1788 bytes

 1package internal
 2
 3import (
 4	"encoding/json"
 5	"fmt"
 6	"os"
 7	"runtime"
 8	"time"
 9	// "log"
10)
11
12type curdConfig struct {
13	player string
14	historyFile string
15	subs_language string
16	sub_or_dub string
17	percentageToMarkComplete int
18	nextEpisodePrompt bool
19	skipOp bool
20	skipEd bool
21	skipFiller bool
22	skipRecap bool
23	scoreOnCompletion bool
24	saveMpvSpeed bool
25	discordPresence bool
26}
27
28// LogData logs the input data into a specified log file with the format [LOG] time lineNumber: logData
29func Log(data interface{}, logFile string) error {
30	// Open or create the log file
31	file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
32	if err != nil {
33		return err
34	}
35	defer file.Close() // Ensure the file is closed when done
36
37	// Attempt to marshal the data into JSON
38	jsonData, err := json.Marshal(data)
39	if err != nil {
40		return err
41	}
42
43	// Get the caller information
44	_, filename, lineNumber, ok := runtime.Caller(1) // Caller 1 gives the caller of LogData
45	if !ok {
46		return fmt.Errorf("unable to get caller information")
47	}
48
49	// Log the current time and the JSON representation along with caller info
50	currentTime := time.Now().Format("2006/01/02 15:04:05")
51	logMessage := fmt.Sprintf("[LOG] %s %s %d: %s\n", currentTime, filename, lineNumber, jsonData)
52	_, err = fmt.Fprint(file, logMessage) // Write to the file
53	if err != nil {
54		return err
55	}
56
57	return nil
58}
59
60
61// ClearScreen clears the terminal screen and saves the state
62func ClearScreen() {
63    fmt.Print("\033[?1049h") // Switch to alternate screen buffer
64    fmt.Print("\033[2J")     // Clear the entire screen
65    fmt.Print("\033[H")      // Move cursor to the top left
66}
67
68// RestoreScreen restores the original terminal state
69func RestoreScreen() {
70    fmt.Print("\033[?1049l") // Switch back to the main screen buffer
71}