diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2022-04-01 19:44:27 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2022-04-01 19:44:27 +0200 |
commit | b5b54feb70ba1dabc2ef425679f3c569b4e199fa (patch) | |
tree | e1f1605ab39455479d7d6c9c30e4e6f09badf729 | |
parent | Random works fine (diff) | |
download | go2work-b5b54feb70ba1dabc2ef425679f3c569b4e199fa.tar.gz go2work-b5b54feb70ba1dabc2ef425679f3c569b4e199fa.tar.bz2 go2work-b5b54feb70ba1dabc2ef425679f3c569b4e199fa.tar.xz go2work-b5b54feb70ba1dabc2ef425679f3c569b4e199fa.tar.zst go2work-b5b54feb70ba1dabc2ef425679f3c569b4e199fa.zip |
In progress
-rw-r--r-- | src/c_defs.go | 5 | ||||
-rw-r--r-- | src/c_go2work.go | 28 | ||||
-rw-r--r-- | src/p_options.go | 37 | ||||
-rw-r--r-- | src/u_checks.go | 11 | ||||
-rw-r--r-- | src/u_prints.go | 9 | ||||
-rw-r--r-- | src/u_utils.go | 6 |
6 files changed, 86 insertions, 10 deletions
diff --git a/src/c_defs.go b/src/c_defs.go index 36a20ba..d21fa8a 100644 --- a/src/c_defs.go +++ b/src/c_defs.go @@ -39,7 +39,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * go2work: src/c_defs.go - * Fri Apr 1 18:25:16 CEST 2022 + * Fri Apr 1 18:49:56 CEST 2022 * Joe * * Definitions. @@ -55,6 +55,7 @@ const ( SECS = 2 INTERVAL = 500 LOG_FORMAT = "bad time format" + OPTIONS_FILE = PROGNAME + "/" + PROGNAME + ".toml" DEF_MEDIA_PLAYER = "mpv" DEF_RANDOM = true DEF_USE_FORTUNE = true @@ -69,8 +70,8 @@ func DEF_FILES() []string { func DEF_PLAYER_OPTIONS() []string { return []string{ - "--loop", "--no-video", + "--loop", } } diff --git a/src/c_go2work.go b/src/c_go2work.go index 076309a..259263b 100644 --- a/src/c_go2work.go +++ b/src/c_go2work.go @@ -39,7 +39,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * go2work: src/c_go2work.go - * Fri Apr 1 18:26:36 CEST 2022 + * Fri Apr 1 18:36:54 CEST 2022 * Joe * * The main. @@ -48,6 +48,7 @@ package main import ( + "fmt" "log" "os" "time" @@ -90,8 +91,16 @@ func main() { dest_t[SECS] = 0 } options = parse_options() + fmt.Println("OPTIONS") + fmt.Println("=======") + fmt.Println(options.files) + fmt.Println(options.media_player) + fmt.Println(options.player_options) + fmt.Println(options.random) + fmt.Println(options.use_fortune) + fmt.Println("=======") if check_time_format(dest_t) == false { - log.Fatal(LOG_FORMAT) + log.Fatalln(LOG_FORMAT) return } if check_media_player(options.media_player) == false { @@ -105,7 +114,6 @@ func main() { print_time_left(curr_t, dest_t) ticker := time.NewTicker(INTERVAL * time.Millisecond) quit := make(chan struct{}) - file_id := choose_file(options) for { select { case <- ticker.C: @@ -114,7 +122,19 @@ func main() { if curr_t[HOURS] == dest_t[HOURS] && curr_t[MINS] == dest_t[MINS] && curr_t[SECS] == dest_t[SECS] { - args := append(options.player_options, options.files[file_id]) + file_id := choose_file(options) + var args []string + if file_id > 0 { + args = append( + options.player_options, + options.files[file_id], + ) + } else { + args = append( + options.player_options, + DEF_FILES()[0], + ) + } has_rang := false for { exec_player( diff --git a/src/p_options.go b/src/p_options.go index 8abd3c2..ea8f9f5 100644 --- a/src/p_options.go +++ b/src/p_options.go @@ -39,7 +39,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * go2work: src/p_options.go - * Fri Apr 1 17:16:04 CEST 2022 + * Fri Apr 1 19:44:09 CEST 2022 * Joe * * Options parsing. @@ -47,8 +47,18 @@ package main +import ( + "log" + "os" +) + func parse_options() Options { options := init_def_options() + options_file := find_options_file() + if options_file == "" { + return options + } + options = parse_toml_file(options_file, options) return options } @@ -62,3 +72,28 @@ func init_def_options() Options { } return options } + +func find_options_file() string { + val, exists := os.LookupEnv("XDG_CONFIG_HOME") + var file_path string + if exists == true { + file_path = val + "/" + OPTIONS_FILE + if check_file_exists(file_path) == true { + return file_path + } + } + dir, err := os.UserHomeDir() + if err != nil { + log.Fatal(err) + } + file_path = dir + "/.config/" + OPTIONS_FILE + if check_file_exists(file_path) == true { + return file_path + } + return "" +} + +func parse_toml_file(options_file string, def_options Options) Options { + options := def_options + return options +} diff --git a/src/u_checks.go b/src/u_checks.go index 1a33232..6188803 100644 --- a/src/u_checks.go +++ b/src/u_checks.go @@ -39,7 +39,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * go2work: src/u_checks.go - * Wed Mar 30 01:25:35 CEST 2022 + * Fri Apr 1 18:30:55 CEST 2022 * Joe * * Useful checks. @@ -48,6 +48,7 @@ package main import ( + "os" "os/exec" ) @@ -78,3 +79,11 @@ func check_time_format(time [3]byte) bool { } return true } + +func check_file_exists(file string) bool { + _, err := os.Stat(file) + if os.IsNotExist(err) == true { + return false + } + return true +} diff --git a/src/u_prints.go b/src/u_prints.go index c4faf4f..205f04b 100644 --- a/src/u_prints.go +++ b/src/u_prints.go @@ -39,7 +39,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * go2work: src/u_prints.go - * Tue Mar 29 22:41:23 CEST 2022 + * Fri Apr 1 18:49:41 CEST 2022 * Joe * * Stuff to print @@ -112,3 +112,10 @@ func print_version() { func print_fortune_not_found() { fmt.Println("Beware, fortune is set on but was not found") } + +func print_file_not_found(file string) { + fmt.Println("\n\nBeware, file \"" + + file + + "\" not found! Using default ringtone", + ) +} diff --git a/src/u_utils.go b/src/u_utils.go index 4a81b85..21d7da7 100644 --- a/src/u_utils.go +++ b/src/u_utils.go @@ -39,7 +39,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * go2work: src/u_utils.go - * Fri Apr 1 18:25:20 CEST 2022 + * Fri Apr 1 18:36:59 CEST 2022 * Joe */ @@ -95,5 +95,9 @@ func choose_file(options Options) int { } else { file_id = 0 } + if check_file_exists(options.files[file_id]) == false { + print_file_not_found(options.files[file_id]) + return -1 + } return file_id } |