21
21
config Config
22
22
appdataDir string
23
23
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
+ }
24
38
)
25
39
26
40
func extractFFmpegExe (zipPath , destDir string ) error {
@@ -61,10 +75,16 @@ type hotkeyConfig struct {
61
75
}
62
76
63
77
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"`
68
88
}
69
89
70
90
func initConfig () {
@@ -76,13 +96,7 @@ func initConfig() {
76
96
appdataDir = filepath .Join (appdataDir , "captr" )
77
97
configFilePath = filepath .Join (appdataDir , ".captr_config.json" )
78
98
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
86
100
data , err := json .MarshalIndent (config , "" , " " )
87
101
if err != nil {
88
102
panic (err )
@@ -94,24 +108,59 @@ func initConfig() {
94
108
if err != nil {
95
109
panic (err )
96
110
}
97
- if err := json .Unmarshal (data , & config ); err != nil {
111
+ var loadedConfig Config
112
+ if err := json .Unmarshal (data , & loadedConfig ); err != nil {
98
113
panic (err )
99
114
}
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 )
100
122
}
101
123
}
102
124
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
+
103
154
func initDownloads () {
104
155
dwnPath := filepath .Join (appdataDir , "bin" )
105
156
if _ , err := os .Stat (filepath .Join (dwnPath , "ffmpeg.exe" )); err == nil {
106
- mergeRecordingDefaults ()
107
157
return
108
158
}
109
159
if ! config .RecordFunc {
110
160
return
111
161
}
112
162
cmd := exec .Command ("ffmpeg" , "-version" )
113
163
if err := cmd .Run (); err == nil {
114
- mergeRecordingDefaults ()
115
164
return
116
165
}
117
166
fmt .Println ("Captr requires ffmpeg to record videos. However, the screenshotting functionality is not affected." )
@@ -153,7 +202,6 @@ func initDownloads() {
153
202
}
154
203
extractFFmpegExe (filepath .Join (os .TempDir (), "ffmpeg_captr.zip" ), dwnPath )
155
204
fmt .Printf ("FFMPEG has been downloaded to %s" , dwnPath )
156
- mergeRecordingDefaults ()
157
205
} else {
158
206
setConfig ("record_func_enabled" , false )
159
207
}
@@ -180,8 +228,9 @@ func init() {
180
228
// Declaring again for safety. Even if anything fails, atleast it won't delete your entire appdata directory
181
229
appdata , _ := os .UserConfigDir ()
182
230
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 ,
185
234
}
186
235
_ , err := prompt .Run ()
187
236
if err != nil {
@@ -199,9 +248,9 @@ func init() {
199
248
if * hotkeyConfigMode {
200
249
mods , finalkey := RegisterHotkey ()
201
250
hotkeyConfig := hotkeyConfig {
202
- Modkeys : mods ,
251
+ Modkeys : mods ,
203
252
Finalkey : finalkey ,
204
- Note : HOTKEY_WARNING ,
253
+ Note : HOTKEY_WARNING ,
205
254
}
206
255
setConfig ("hotkey_config" , hotkeyConfig )
207
256
fmt .Println ("Hotkeys have been registered successfully" )
0 commit comments