Skip to content

Commit 777607a

Browse files
Fixed the errors and made the config have default values.
1 parent e5619ac commit 777607a

File tree

2 files changed

+87
-104
lines changed

2 files changed

+87
-104
lines changed

main.go

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ var (
2121
config Config
2222
appdataDir string
2323
configFilePath string
24+
defaultConfig = Config{
25+
SaveLocation: filepath.Join(os.Getenv("USERPROFILE"), "Desktop"),
26+
RecordFunc: true,
27+
RecordingOpts: RecordingOptions{
28+
FPS: 30,
29+
CaptureMouse: true,
30+
AudioDevice: "",
31+
},
32+
HotkeyConfig: hotkeyConfig{
33+
Modkeys: []string{"ctrl", "shift"},
34+
Finalkey: "3",
35+
Note: "DO NOT CHANGE ANYTHING MANUALLY IN THIS SECTION UNLESS YOU KNOW WHAT YOU'RE DOING. ALWAYS CHANGE THE HOTKEY VIA THE DEFAULT --hotkey FLAG",
36+
},
37+
}
2438
)
2539

2640
func extractFFmpegExe(zipPath, destDir string) error {
@@ -61,10 +75,16 @@ type hotkeyConfig struct {
6175
}
6276

6377
type Config struct {
64-
SaveLocation string `json:"save_location"`
65-
RecordFunc bool `json:"record_func_enabled"`
66-
RecordingOpts *RecordingOptions `json:"recording_options,omitempty"`
67-
HotkeyConfig *hotkeyConfig `json:"hotkey_config"`
78+
SaveLocation string `json:"save_location"`
79+
RecordFunc bool `json:"record_func_enabled"`
80+
RecordingOpts RecordingOptions `json:"recording_options,omitempty"`
81+
HotkeyConfig hotkeyConfig `json:"hotkey_config"`
82+
}
83+
84+
type RecordingOptions struct {
85+
FPS int `json:"fps"`
86+
CaptureMouse bool `json:"capture_mouse"`
87+
AudioDevice string `json:"audio_device"`
6888
}
6989

7090
func initConfig() {
@@ -76,13 +96,7 @@ func initConfig() {
7696
appdataDir = filepath.Join(appdataDir, "captr")
7797
configFilePath = filepath.Join(appdataDir, ".captr_config.json")
7898
if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
79-
home, _ := os.UserHomeDir()
80-
config = Config{
81-
SaveLocation: filepath.Join(home, "Desktop"),
82-
RecordFunc: true,
83-
RecordingOpts: nil,
84-
HotkeyConfig: nil,
85-
}
99+
config = defaultConfig
86100
data, err := json.MarshalIndent(config, "", " ")
87101
if err != nil {
88102
panic(err)
@@ -94,24 +108,59 @@ func initConfig() {
94108
if err != nil {
95109
panic(err)
96110
}
97-
if err := json.Unmarshal(data, &config); err != nil {
111+
var loadedConfig Config
112+
if err := json.Unmarshal(data, &loadedConfig); err != nil {
98113
panic(err)
99114
}
115+
116+
config = mergeConfig(defaultConfig, loadedConfig)
117+
mergedData, err := json.MarshalIndent(config, "", " ")
118+
if err != nil {
119+
panic(err)
120+
}
121+
os.WriteFile(configFilePath, mergedData, 0644)
100122
}
101123
}
102124

125+
func mergeConfig(defaultConfig, loadedConfig Config) Config {
126+
if loadedConfig.SaveLocation == "" {
127+
loadedConfig.SaveLocation = defaultConfig.SaveLocation
128+
}
129+
if !loadedConfig.RecordFunc {
130+
loadedConfig.RecordFunc = defaultConfig.RecordFunc
131+
}
132+
if loadedConfig.RecordingOpts.FPS == 0 {
133+
loadedConfig.RecordingOpts.FPS = defaultConfig.RecordingOpts.FPS
134+
}
135+
if !loadedConfig.RecordingOpts.CaptureMouse {
136+
loadedConfig.RecordingOpts.CaptureMouse = defaultConfig.RecordingOpts.CaptureMouse
137+
}
138+
if loadedConfig.RecordingOpts.AudioDevice == "" {
139+
loadedConfig.RecordingOpts.AudioDevice = defaultConfig.RecordingOpts.AudioDevice
140+
}
141+
if len(loadedConfig.HotkeyConfig.Modkeys) == 0 {
142+
loadedConfig.HotkeyConfig.Modkeys = defaultConfig.HotkeyConfig.Modkeys
143+
}
144+
if loadedConfig.HotkeyConfig.Finalkey == "" {
145+
loadedConfig.HotkeyConfig.Finalkey = defaultConfig.HotkeyConfig.Finalkey
146+
}
147+
if loadedConfig.HotkeyConfig.Note == "" {
148+
loadedConfig.HotkeyConfig.Note = defaultConfig.HotkeyConfig.Note
149+
}
150+
151+
return loadedConfig
152+
}
153+
103154
func initDownloads() {
104155
dwnPath := filepath.Join(appdataDir, "bin")
105156
if _, err := os.Stat(filepath.Join(dwnPath, "ffmpeg.exe")); err == nil {
106-
mergeRecordingDefaults()
107157
return
108158
}
109159
if !config.RecordFunc {
110160
return
111161
}
112162
cmd := exec.Command("ffmpeg", "-version")
113163
if err := cmd.Run(); err == nil {
114-
mergeRecordingDefaults()
115164
return
116165
}
117166
fmt.Println("Captr requires ffmpeg to record videos. However, the screenshotting functionality is not affected.")
@@ -153,7 +202,6 @@ func initDownloads() {
153202
}
154203
extractFFmpegExe(filepath.Join(os.TempDir(), "ffmpeg_captr.zip"), dwnPath)
155204
fmt.Printf("FFMPEG has been downloaded to %s", dwnPath)
156-
mergeRecordingDefaults()
157205
} else {
158206
setConfig("record_func_enabled", false)
159207
}
@@ -180,8 +228,9 @@ func init() {
180228
// Declaring again for safety. Even if anything fails, atleast it won't delete your entire appdata directory
181229
appdata, _ := os.UserConfigDir()
182230
prompt := promptui.Prompt{
183-
Label: "Are you sure you want to reset Captr",
184-
IsConfirm: true,
231+
Label: "Are you sure you want to reset Captr",
232+
IsConfirm: true,
233+
HideEntered: true,
185234
}
186235
_, err := prompt.Run()
187236
if err != nil {
@@ -199,9 +248,9 @@ func init() {
199248
if *hotkeyConfigMode {
200249
mods, finalkey := RegisterHotkey()
201250
hotkeyConfig := hotkeyConfig{
202-
Modkeys: mods,
251+
Modkeys: mods,
203252
Finalkey: finalkey,
204-
Note: HOTKEY_WARNING,
253+
Note: HOTKEY_WARNING,
205254
}
206255
setConfig("hotkey_config", hotkeyConfig)
207256
fmt.Println("Hotkeys have been registered successfully")

recscreen.go

Lines changed: 19 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package main
22

33
import (
4-
"encoding/json"
54
"fmt"
6-
"os"
75
"os/exec"
86
"path/filepath"
97
"strconv"
@@ -16,18 +14,6 @@ import (
1614
"golang.design/x/hotkey"
1715
)
1816

19-
type RecordingOptions struct {
20-
FPS int `json:"fps"`
21-
CaptureMouse bool `json:"capture_mouse"`
22-
AudioDevice string `json:"audio_device"`
23-
}
24-
25-
var defaultOpts = map[string]any{
26-
"fps": 30,
27-
"capture_mouse": true,
28-
"audio_device": "",
29-
}
30-
3117
var (
3218
keys = map[string]hotkey.Key{
3319
"a": hotkey.KeyA,
@@ -69,53 +55,7 @@ var (
6955
}
7056
)
7157

72-
func mergeRecordingDefaults() {
73-
data, err := os.ReadFile(configFilePath)
74-
if err != nil {
75-
initConfig()
76-
return
77-
}
78-
79-
var config map[string]any
80-
if err := json.Unmarshal(data, &config); err != nil {
81-
fmt.Println("Error reading config:", err)
82-
return
83-
}
84-
85-
recRaw, ok := config["recording_options"]
86-
if !ok {
87-
config["recording_options"] = defaultOpts
88-
} else {
89-
recMap, ok := recRaw.(map[string]any)
90-
if !ok {
91-
fmt.Println("Error reading recording options")
92-
return
93-
}
94-
95-
changed := false
96-
for k, v := range defaultOpts {
97-
if _, exists := recMap[k]; !exists {
98-
recMap[k] = v
99-
changed = true
100-
}
101-
}
102-
103-
if changed {
104-
config["recording_options"] = recMap
105-
} else {
106-
return
107-
}
108-
}
109-
110-
out, _ := json.MarshalIndent(config, "", " ")
111-
os.WriteFile(configFilePath, out, 0644)
112-
}
113-
11458
func RecordDisplay() {
115-
if config.HotkeyConfig != nil && (len(config.HotkeyConfig.Modkeys) == 0 || config.HotkeyConfig.Finalkey == "") {
116-
fmt.Println("Invalid hotkey configured. Please reset Captr.")
117-
os.Exit(1)
118-
}
11959
active_displays := robotgo.DisplaysNum()
12060
displays := []string{"Display 1 (Primary)"}
12161
for i := 2; i < active_displays; i++ {
@@ -161,35 +101,29 @@ func RecordDisplay() {
161101
}
162102

163103
var modkeys []hotkey.Modifier
164-
func() {
165-
if config.HotkeyConfig == nil {
166-
modkeys = []hotkey.Modifier{hotkey.ModCtrl, hotkey.ModAlt}
167-
} else {
168-
pressedKeys := map[string]bool{}
169-
for _, key := range config.HotkeyConfig.Modkeys {
170-
pressedKeys[key] = true
171-
}
172-
for _, mod := range []string{"ctrl", "alt", "shift"} {
173-
if pressedKeys[mod] {
174-
switch mod {
175-
case "ctrl":
176-
modkeys = append(modkeys, hotkey.ModCtrl)
177-
case "alt":
178-
modkeys = append(modkeys, hotkey.ModAlt)
179-
case "shift":
180-
modkeys = append(modkeys, hotkey.ModShift)
181-
}
182-
}
104+
pressedKeys := map[string]bool{}
105+
for _, key := range config.HotkeyConfig.Modkeys {
106+
pressedKeys[key] = true
107+
}
108+
for _, mod := range []string{"ctrl", "alt", "shift"} {
109+
if pressedKeys[mod] {
110+
switch mod {
111+
case "ctrl":
112+
modkeys = append(modkeys, hotkey.ModCtrl)
113+
case "alt":
114+
modkeys = append(modkeys, hotkey.ModAlt)
115+
case "shift":
116+
modkeys = append(modkeys, hotkey.ModShift)
183117
}
184118
}
185-
}()
186-
fmt.Printf("Recording started. Press %s to stop\n", ternary(config.HotkeyConfig == nil, strings.Join([]string{"ctrl", "alt", "3"}, "+"), strings.Join(append(config.HotkeyConfig.Modkeys, config.HotkeyConfig.Finalkey), "+")))
119+
}
120+
fmt.Printf("Recording started. Press %s to stop\n", strings.Join(append(config.HotkeyConfig.Modkeys, config.HotkeyConfig.Finalkey), "+"))
187121
tickStop := make(chan struct{})
188122
ticker := time.NewTicker(time.Second)
189123
defer ticker.Stop()
190124
i := 0
191125
last := []string{"🔴", "⚫"}
192-
func() {
126+
go func() {
193127
for {
194128
select {
195129
case <-ticker.C:
@@ -199,7 +133,7 @@ func RecordDisplay() {
199133
}
200134
}
201135
}()
202-
hk := hotkey.New(modkeys, ternary(config.HotkeyConfig == nil, hotkey.Key3, keys[config.HotkeyConfig.Finalkey]))
136+
hk := hotkey.New(modkeys, keys[config.HotkeyConfig.Finalkey])
203137
err = hk.Register()
204138
if err != nil {
205139
fmt.Println("Error registering hotkey:", err)
@@ -208,10 +142,10 @@ func RecordDisplay() {
208142
defer hk.Unregister()
209143
keyChan := hk.Keydown()
210144
for range keyChan {
211-
fmt.Println("\nStopping recording...")
145+
tickStop <- struct{}{}
212146
stdin.Write([]byte("q"))
213147
stdin.Close()
214-
tickStop <- struct{}{}
148+
fmt.Println("\nStopping recording...")
215149
notification := toast.Notification{
216150
AppID: "Captr",
217151
Title: "Recording Stopped",

0 commit comments

Comments
 (0)