Skip to content

Commit

Permalink
update timer and make colon can change color
Browse files Browse the repository at this point in the history
  • Loading branch information
yorukot committed Mar 2, 2024
1 parent 43f373b commit 9a4328c
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 92 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ tmlshock stopwatch

## Setting

### Color
## **Color**

You can use color codes (down below) or use color names

Expand Down Expand Up @@ -70,33 +70,33 @@ tmlshock clock -c red
```
![red-clock](https://github.com/MHNightCat/tmlshock/blob/main/img/red-clock.png)

### Disable-second
## **Disable-second**

To disable the second just enter `-s false`

Example
**Example**
```sh
tmlshock clock -s false
```

![no-sec-clock](https://github.com/MHNightCat/tmlshock/blob/main/img/no-sec-clock.png)

### Enable-date
## **Enable-date**

To enable the date just enter `-d true`

Example
**Example**
```sh
tmlshock clock -d true
```

![date-clock](https://github.com/MHNightCat/tmlshock/blob/main/img/date-clock.png)

### Date-formate
## **Date-formate**

To use a custom date format just enter `-df 2006/02/01`(YYYY/MM/DD)

Example
**Example**
```sh
tmlshock clock -d true -df 2006/01/02
```
Expand Down
62 changes: 60 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
&cli.StringFlag{
Name: "color",
Aliases: []string{"c"},
Usage: "Set the string color see full color in()",
Usage: "Set the string color see full color in(https://github.com/MHNightCat/tmlshock?tab=readme-ov-file#color)",
},
},
Commands: []*cli.Command{
Expand All @@ -27,7 +27,60 @@ func main() {
&cli.StringFlag{
Name: "color",
Aliases: []string{"c"},
Usage: "Set the string color",
Usage: "Set the string color (https://github.com/MHNightCat/tmlshock?tab=readme-ov-file#color)",
},
&cli.StringFlag{
Name: "disable-hour",
Aliases: []string{"dh"},
Usage: "Disable hour(true or false)",
},
&cli.StringFlag{
Name: "colon-color",
Aliases: []string{"cc"},
Usage: "Set the colon color (https://github.com/MHNightCat/tmlshock?tab=readme-ov-file#color)",
},
},
},
{
Name: "timer",
Aliases: []string{"t"},
Usage: "Start a timer",
Action: util.Timer,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "color",
Aliases: []string{"c"},
Usage: "Set the string color (https://github.com/MHNightCat/tmlshock?tab=readme-ov-file#color)",
},
&cli.StringFlag{
Name: "hour",
Aliases: []string{"hr"},
Usage: "Enter how many hours you want to count down",
},
&cli.StringFlag{
Name: "minute",
Aliases: []string{"m", "min"},
Usage: "Enter how many minunts you want to count down",
},
&cli.StringFlag{
Name: "second",
Aliases: []string{"s", "sec"},
Usage: "Enter how many seconds you want to count down",
},
&cli.StringFlag{
Name: "timerTime",
Aliases: []string{"t"},
Usage: "Enter how many time you want to count down(format: 00:00:00)",
},
&cli.StringFlag{
Name: "disable-hour",
Aliases: []string{"dh"},
Usage: "Disable hour(true or false)",
},
&cli.StringFlag{
Name: "colon-color",
Aliases: []string{"cc"},
Usage: "Set the colon color (https://github.com/MHNightCat/tmlshock?tab=readme-ov-file#color)",
},
},
},
Expand Down Expand Up @@ -61,6 +114,11 @@ func main() {
Value: "2006/02/01",
Usage: "Set the clock date formate",
},
&cli.StringFlag{
Name: "colon-color",
Aliases: []string{"cc"},
Usage: "Set the colon color (https://github.com/MHNightCat/tmlshock?tab=readme-ov-file#color)",
},
},
},
},
Expand Down
29 changes: 29 additions & 0 deletions util/SmallNumber.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,32 @@ func SmallZero(termWidth int, termHeight int, color termbox.Attribute, diff int)

drawString(termWidth/2+diff, termHeight/2+2, " ", color, color)
}

func CaseNumber(termWidth int, termHeight int, color termbox.Attribute, Nowtime byte, diff int){
switch Nowtime {
case '0':
SmallZero(termWidth, termHeight, color, diff)
case '1':
SmallOne(termWidth, termHeight, color, diff)
case '2':
SmallTwo(termWidth, termHeight, color, diff)
case '3':
SmallThree(termWidth, termHeight, color, diff)
case '4':
SmallFour(termWidth, termHeight, color, diff)
case '5':
SmallFive(termWidth, termHeight, color, diff)
case '6':
SmallSix(termWidth, termHeight, color, diff)
case '7':
SmallSeven(termWidth, termHeight, color, diff)
case '8':
SmallEight(termWidth, termHeight, color, diff)
case '9':
SmallNine(termWidth, termHeight, color, diff)
case ':':
SmallColon(termWidth, termHeight, color, diff)
case '.':
SmallDot(termWidth, termHeight, color, diff)
}
}
37 changes: 10 additions & 27 deletions util/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ func Clock(cCtx *cli.Context) error {
defer termbox.Close()
termbox.SetOutputMode(termbox.Output256)
color := FlagColor(cCtx.String("color"))
colonColor := FlagColor(cCtx.String("colon-color"))

if cCtx.String("colon-color") == "" && cCtx.String("color") != "" {
colonColor = FlagColor(cCtx.String("color"))
}
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)

go func() {
Expand All @@ -45,37 +49,16 @@ func Clock(cCtx *cli.Context) error {
if i != 0 {
diff = diff + SmallStopWatchDiff[i-1]
}
switch NowTime[i] {
case '0':
SmallZero(termWidth, termHeight, color, diff)
case '1':
SmallOne(termWidth, termHeight, color, diff)
case '2':
SmallTwo(termWidth, termHeight, color, diff)
case '3':
SmallThree(termWidth, termHeight, color, diff)
case '4':
SmallFour(termWidth, termHeight, color, diff)
case '5':
SmallFive(termWidth, termHeight, color, diff)
case '6':
SmallSix(termWidth, termHeight, color, diff)
case '7':
SmallSeven(termWidth, termHeight, color, diff)
case '8':
SmallEight(termWidth, termHeight, color, diff)
case '9':
SmallNine(termWidth, termHeight, color, diff)
case ':':
SmallColon(termWidth, termHeight, color, diff)
case '.':
SmallDot(termWidth, termHeight, color, diff)
if i == 2 || i == 5 {
CaseNumber(termWidth, termHeight, colonColor, NowTime[i], diff)
} else {
CaseNumber(termWidth, termHeight, color, NowTime[i], diff)
}
}

if cCtx.String("date") == "true" {
dateString := current.Format(cCtx.String("dateformate"))
drawString(termWidth/2-(len(dateString)/2), termHeight/2+4, dateString, color, termbox.ColorDefault)
dateString := current.Format(cCtx.String("dateformate"))
drawString(termWidth/2-(len(dateString)/2), termHeight/2+4, dateString, color, termbox.ColorDefault)
}
termbox.Flush()
time.Sleep(time.Second)
Expand Down
54 changes: 26 additions & 28 deletions util/stopwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

var SmallStopWatchDiff = [11]int{7, 6, 6, 7, 6, 6, 7, 6, 4, 7, 7}
var SmallStopWatchWithoutHourDiff = [8]int{7, 6, 6, 7, 6, 4, 7, 7}

func Stopwatch(cCtx *cli.Context) error {
err := termbox.Init()
Expand All @@ -19,7 +20,11 @@ func Stopwatch(cCtx *cli.Context) error {
defer termbox.Close()
termbox.SetOutputMode(termbox.Output256)
color := FlagColor(cCtx.String("color"))
colonColor := FlagColor(cCtx.String("colon-color"))

if cCtx.String("colon-color") == "" && cCtx.String("color") != "" {
colonColor = FlagColor(cCtx.String("color"))
}
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)

go func() {
Expand All @@ -28,37 +33,30 @@ func Stopwatch(cCtx *cli.Context) error {
termWidth, termHeight := termbox.Size()
current := time.Since(start).Round(time.Millisecond)
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
NowTime := StopwatchFormatTime(current)
NowTime := ""
if cCtx.String("disable-hour") == "true" {
NowTime = StopwatchFormatTimeWihtoutHour(current)
}else{
NowTime = StopwatchFormatTime(current)
}
diff := -38
for i := 0; i < 12; i++ {
totalString := 12
if cCtx.String("disable-hour") == "true" {
totalString = 9
diff = -29
}
for i := 0; i < totalString; i++ {
if i != 0 {
diff = diff + SmallStopWatchDiff[i-1]
if cCtx.String("disable-hour") == "true" {
diff = diff + SmallStopWatchWithoutHourDiff[i-1]
} else {
diff = diff + SmallStopWatchDiff[i-1]
}
}
switch NowTime[i] {
case '0':
SmallZero(termWidth, termHeight, color, diff)
case '1':
SmallOne(termWidth, termHeight, color, diff)
case '2':
SmallTwo(termWidth, termHeight, color, diff)
case '3':
SmallThree(termWidth, termHeight, color, diff)
case '4':
SmallFour(termWidth, termHeight, color, diff)
case '5':
SmallFive(termWidth, termHeight, color, diff)
case '6':
SmallSix(termWidth, termHeight, color, diff)
case '7':
SmallSeven(termWidth, termHeight, color, diff)
case '8':
SmallEight(termWidth, termHeight, color, diff)
case '9':
SmallNine(termWidth, termHeight, color, diff)
case ':':
SmallColon(termWidth, termHeight, color, diff)
case '.':
SmallDot(termWidth, termHeight, color, diff)
if i == 2 || i == 5 {
CaseNumber(termWidth, termHeight, colonColor, NowTime[i], diff)
} else {
CaseNumber(termWidth, termHeight, color, NowTime[i], diff)
}
}

Expand Down
95 changes: 95 additions & 0 deletions util/timer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package util

import (
"fmt"
"github.com/nsf/termbox-go"
"github.com/urfave/cli/v2"
"os"
"strconv"
"time"
)

var SmallTimerDiff = [11]int{7, 6, 6, 7, 6, 6, 7, 6, 4, 7, 7}
var SmallTimerWithoutHourDiff = [11]int{7, 6, 6, 7, 6, 4, 7, 7}

func Timer(cCtx *cli.Context) error {
err := termbox.Init()
if err != nil {
fmt.Println("failed to initialize termbox:", err)
return err
}
defer termbox.Close()
termbox.SetOutputMode(termbox.Output256)
color := FlagColor(cCtx.String("color"))

min, _ := strconv.Atoi(cCtx.String("minute"))
sec, _ := strconv.Atoi(cCtx.String("second"))
hour, _ := strconv.Atoi(cCtx.String("hour"))

total := hour*60*60 + min*60 + sec*60
if total == 0 {
total = 300
}
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
colonColor := FlagColor(cCtx.String("colon-color"))

if cCtx.String("colon-color") == "" && cCtx.String("color") != "" {
colonColor = FlagColor(cCtx.String("color"))
}
go func() {
duration := time.Duration(total) * time.Second
end := time.Now().Add(duration)
for {

termWidth, termHeight := termbox.Size()
current := time.Until(end)
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
NowTime := ""
if cCtx.String("disable-hour") == "true" {
NowTime = StopwatchFormatTimeWihtoutHour(current)
} else {
NowTime = StopwatchFormatTime(current)
}
diff := -38
totalString := 12
if cCtx.String("disable-hour") == "true" {
totalString = 9
diff = -29
}
for i := 0; i < totalString; i++ {
if i != 0 {
if cCtx.String("disable-hour") == "true" {
diff = diff + SmallTimerWithoutHourDiff[i-1]
} else {
diff = diff + SmallTimerDiff[i-1]
}
}
if i == 2 || i == 5 {
CaseNumber(termWidth, termHeight, colonColor, NowTime[i], diff)
} else {
CaseNumber(termWidth, termHeight, color, NowTime[i], diff)
}
}

termbox.Flush()
time.Sleep(time.Millisecond)
}
}()

mainLoop:
for {
ev := termbox.PollEvent()
switch ev.Type {
case termbox.EventKey:
switch {
case ev.Key == termbox.KeyEsc || ev.Ch == 'q' || ev.Ch == 'Q':
break mainLoop
case ev.Key == termbox.KeyCtrlC:
break mainLoop
}
case termbox.EventError:
os.Exit(1)
}
}
return nil
}
Loading

0 comments on commit 9a4328c

Please sign in to comment.