From 08cff13e69b5747d961a3a8d4ade7971bb4ae28b Mon Sep 17 00:00:00 2001 From: Beta Pictoris Date: Sat, 26 Feb 2022 01:19:26 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Added=20`--shift`=20option=20Reques?= =?UTF-8?q?t=20from=20Reddit,=20thanks=20u/HoboWarZ!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/main.go b/src/main.go index 11730e6..92ac6e5 100644 --- a/src/main.go +++ b/src/main.go @@ -3,6 +3,8 @@ package main import ( "fmt" "os" + "os/exec" + "strconv" "strings" "time" @@ -16,12 +18,42 @@ const ( ) func main() { + var mode = "shift" // Mode can be 'day' or 'shift' + var hour = time.Now().Hour() var minute = time.Now().Minute() - var minutesInADay = 1440.00 + var length = 1440. + + var shiftL = 8.0 + + // Check if --shift is in args + if len(os.Args) >= 2 { + if "--shift" == os.Args[1] { + if len(os.Args) >= 3 { + shiftL, _ = strconv.ParseFloat(os.Args[2], 64) + } + mode = "shift" + } else { + fmt.Println("Usage: timeleft [--shift [Shift length]]") + os.Exit(1) + } + } + + if mode == "shift" { + c, b := exec.Command("uptime"), new(strings.Builder) + c.Stdout = b + c.Run() + + var time = strings.Split(b.String(), " ")[1] + + hour, _ = strconv.Atoi(strings.Split(time, ":")[0]) + minute, _ = strconv.Atoi(strings.Split(time, ":")[1]) + + length = (shiftL * 60) + } var time = float64(((hour * 60) + minute)) - var per = float64(time / minutesInADay) + var per = float64(time / length) prog(int(time), per) }