From 09d5a1c8c49ea6a25c33e8f6991d91eccf8ccfeb Mon Sep 17 00:00:00 2001 From: yofan2408 Date: Wed, 25 Oct 2023 21:59:05 +0700 Subject: [PATCH] first commit --- .github/workflows/go.yml | 39 ++++++++++ .github/workflows/goreleaser.yml | 32 ++++++++ .gitignore | 3 + .golangci.yml | 26 +++++++ .goreleaser.yaml | 21 ++++++ LICENSE | 9 +++ Makefile | 21 ++++++ README.md | 19 +++++ font/drawer.go | 24 ++++++ font/font.go | 124 +++++++++++++++++++++++++++++++ go.mod | 25 +++++++ go.sum | 44 +++++++++++ main.go | 121 ++++++++++++++++++++++++++++++ mama.yaml | 21 ++++++ 14 files changed, 529 insertions(+) create mode 100644 .github/workflows/go.yml create mode 100644 .github/workflows/goreleaser.yml create mode 100644 .gitignore create mode 100644 .golangci.yml create mode 100644 .goreleaser.yaml create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 font/drawer.go create mode 100644 font/font.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 mama.yaml diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..34b9e15 --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,39 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Go + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: '1.19' + cache: false + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.54 + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: 1.19 + - name: Install dependencies + run: go get . + - name: Build + run: make build diff --git a/.github/workflows/goreleaser.yml b/.github/workflows/goreleaser.yml new file mode 100644 index 0000000..bef32e9 --- /dev/null +++ b/.github/workflows/goreleaser.yml @@ -0,0 +1,32 @@ +name: goreleaser + +on: + push: + # run only against tags + tags: + - "v[0-9]+.[0-9]+.[0-9]+*" + +permissions: + contents: write + # packages: write + # issues: write + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + - uses: actions/setup-go@v2 + with: + go-version: 1.19 + - run: go mod tidy + + - uses: goreleaser/goreleaser-action@v2 + if: success() && startsWith(github.ref, 'refs/tags/') + with: + version: latest + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..18fb994 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +dist +bin +dist/ diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..5f86d41 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,26 @@ +# Refer to golangci-lint's example config file for more options and information: +# https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml + +run: + timeout: 5m + modules-download-mode: readonly + +linters: + enable: + - errcheck + - goimports + - revive + - govet + - staticcheck + +linters-settings: + revive: + rules: + - name: unexported-return + disabled: true + + +issues: + exclude-use-default: false + max-issues-per-linter: 0 + max-same-issues: 0 diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..16da06e --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,21 @@ +# This is an example .goreleaser.yml file with some sensible defaults. +# Make sure to check the documentation at https://goreleaser.com + +# The lines bellow are called `modelines`. See `:help modeline` +# Feel free to remove those if you don't want/need to use them. +# yaml-language-server: $schema=https://goreleaser.com/static/schema.json +# vim: set ts=2 sw=2 tw=0 fo=cnqoj + + +project_name: countdown +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + goarch: + - amd64 + - arm64 + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8e7558a --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +The MIT License + +Copyright (c) 2023 Yofan Niki + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7f68f41 --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +.DIST_FOLDER := build + +fmt: + go fmt ./... +.PHONY:fmt + +lint: fmt + golangci-lint run ./... +.PHONY:lint + +vet: fmt + go vet ./... +.PHONY:vet + +build: vet + go build -o bin/countdown . +.PHONY:build + +release: vet + goreleaser release --snapshot --clean +.PHONY:release \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..916cc6c --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# countdown + +`countdown` is a simple timer that runs from the command line. + +# Usage + +``` +Usage of countdown: + + countdown -d duration + +The flags are: + + -d + The duration of the timer as a string of unsigned + decimal numbers, each with optional fraction and + a unit suffix, such as "300ms", "1.5h" or "2h45m". + Expected time units are "s", "m", "h". +``` diff --git a/font/drawer.go b/font/drawer.go new file mode 100644 index 0000000..b389a61 --- /dev/null +++ b/font/drawer.go @@ -0,0 +1,24 @@ +package font + +import ( + "strings" + + "github.com/charmbracelet/lipgloss" +) + +// DrawChar returns the argument character in bigger size. +func DrawChar(c rune) string { + b := strings.Builder{} + for _, row := range smallFonts[c] { + for _, char := range row { + s := lipgloss.NewStyle().SetString(" ") + if char == rune('#') { + s = s.Background(lipgloss.Color("#ffd670")) + } + b.WriteString(s.String()) + } + b.WriteRune('\n') + } + + return b.String() +} diff --git a/font/font.go b/font/font.go new file mode 100644 index 0000000..d846cd6 --- /dev/null +++ b/font/font.go @@ -0,0 +1,124 @@ +package font + +import "strings" + +// Define ASCII art representations of various characters. +var colon = ` +.. +#. +.. +#. +.. +` + +var zero = ` +######. +#....#. +#....#. +#....#. +######. +` + +var one = ` +.....#. +.....#. +.....#. +.....#. +.....#. +` + +var two = ` +######. +.....#. +######. +#...... +######. +` + +var three = ` +######. +.....#. +...###. +.....#. +######. +` + +var four = ` +#...... +#...... +#...#.. +######. +....#.. +` + +var five = ` +######. +#...... +######. +.....#. +######. +` + +var six = ` +######. +#...... +######. +#....#. +######. +` + +var seven = ` +######. +.....#. +.....#. +.....#. +.....#. +` + +var height = ` +######. +#....#. +######. +#....#. +######. +` + +var nine = ` +######. +#....#. +######. +.....#. +######. +` + +// smallFonts defines the font used to display characters on the terminal. +var smallFonts = map[rune][][]rune{ + ':': asArray(colon), + '1': asArray(one), + '2': asArray(two), + '3': asArray(three), + '4': asArray(four), + '5': asArray(five), + '6': asArray(six), + '7': asArray(seven), + '8': asArray(height), + '9': asArray(nine), + '0': asArray(zero), +} + +// asArray converts a string representation of characters to a 2D slice of runes. +func asArray(chars string) [][]rune { + result := [][]rune{} + line := []rune{} + str := strings.TrimPrefix(chars, "\n") + + for _, c := range str { + if c == '\n' { + result = append(result, line) + line = []rune{} + } else { + line = append(line, c) + } + } + return result +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..51d5cee --- /dev/null +++ b/go.mod @@ -0,0 +1,25 @@ +module github.com/yofan2408/countdown + +go 1.19 + +require ( + github.com/charmbracelet/bubbletea v0.23.1 + github.com/charmbracelet/lipgloss v0.6.0 +) + +require ( + github.com/aymanbagabas/go-osc52 v1.0.3 // indirect + github.com/containerd/console v1.0.3 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.16 // indirect + github.com/mattn/go-localereader v0.0.1 // indirect + github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect + github.com/muesli/cancelreader v0.2.2 // indirect + github.com/muesli/reflow v0.3.0 // indirect + github.com/muesli/termenv v0.13.0 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect + golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect + golang.org/x/text v0.3.7 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..00d8a5e --- /dev/null +++ b/go.sum @@ -0,0 +1,44 @@ +github.com/aymanbagabas/go-osc52 v1.0.3 h1:DTwqENW7X9arYimJrPeGZcV0ln14sGMt3pHZspWD+Mg= +github.com/aymanbagabas/go-osc52 v1.0.3/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4= +github.com/charmbracelet/bubbletea v0.23.1 h1:CYdteX1wCiCzKNUlwm25ZHBIc1GXlYFyUIte8WPvhck= +github.com/charmbracelet/bubbletea v0.23.1/go.mod h1:JAfGK/3/pPKHTnAS8JIE2u9f61BjWTQY57RbT25aMXU= +github.com/charmbracelet/lipgloss v0.6.0 h1:1StyZB9vBSOyuZxQUcUwGr17JmojPNm87inij9N3wJY= +github.com/charmbracelet/lipgloss v0.6.0/go.mod h1:tHh2wr34xcHjC2HCXIlGSG1jaDF0S0atAUvBMP6Ppuk= +github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw= +github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= +github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= +github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= +github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= +github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= +github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34= +github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= +github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ= +github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= +github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= +github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs= +github.com/muesli/termenv v0.13.0 h1:wK20DRpJdDX8b7Ek2QfhvqhRQFZ237RGRO0RQ/Iqdy0= +github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..c419955 --- /dev/null +++ b/main.go @@ -0,0 +1,121 @@ +package main + +import ( + "flag" + "fmt" + "os" + "time" + + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" + "github.com/yofan2408/countdown/font" +) + +type Window struct { + Width int + Height int +} + +type Countdown struct { + Window Window + Hour int + Min int + Sec int + Done bool +} + +func initialCountdown(hour, min, sec int) Countdown { + return Countdown{Hour: hour, Min: min, Sec: sec, Done: false} +} + +type tickMsg time.Time + +func Tick() tea.Cmd { + return tea.Tick(1*time.Second, func(t time.Time) tea.Msg { + return tickMsg(t) + }) +} + +func (c Countdown) Init() tea.Cmd { + return Tick() +} + +func (c Countdown) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.WindowSizeMsg: + c.Window.Width, c.Window.Height = msg.Width, msg.Height + return c, nil + case tea.KeyMsg: + switch msg.String() { + case "q", "ctrl+x": + return c, tea.Quit + } + case tickMsg: + if c.Hour == 0 && c.Min == 0 && c.Sec == 0 { + c.Done = true + } + + if c.Done { + break + } + + hour, min, sec := c.Hour, c.Min, c.Sec-1 + if sec < 0 { + min, sec = min-1, 59 + } + + if min < 0 { + hour, min = hour-1, 59 + } + + c.Hour, c.Min, c.Sec = hour, min, sec + + return c, Tick() + } + + return c, nil +} + +func (c Countdown) View() string { + timerStr := "" + if c.Hour > 0 { + timerStr = fmt.Sprintf("%02d:", c.Hour) + } + timerStr = timerStr + fmt.Sprintf("%02d:%02d", c.Min, c.Sec) + timer := "" + for _, c := range timerStr { + timer = lipgloss.JoinHorizontal(lipgloss.Center, timer, font.DrawChar(c)) + } + + ui := lipgloss.Place( + c.Window.Width, c.Window.Height, + lipgloss.Center, lipgloss.Center, + timer, + ) + + return ui +} + +func main() { + var d = flag.Duration("d", 5*time.Second, "Duration of timer.") + + flag.Parse() + + seconds := int(d.Seconds()) + if seconds < 0 { + fmt.Print("No time left\n") + os.Exit(1) + } + + minutes := seconds / 60 + seconds = seconds % 60 + hours := minutes / 60 + minutes = minutes % 60 + + p := tea.NewProgram(initialCountdown(hours, minutes, seconds), tea.WithAltScreen()) + if _, err := p.Run(); err != nil { + fmt.Printf("Error: %v", err) + os.Exit(1) + } + os.Exit(0) +} diff --git a/mama.yaml b/mama.yaml new file mode 100644 index 0000000..16da06e --- /dev/null +++ b/mama.yaml @@ -0,0 +1,21 @@ +# This is an example .goreleaser.yml file with some sensible defaults. +# Make sure to check the documentation at https://goreleaser.com + +# The lines bellow are called `modelines`. See `:help modeline` +# Feel free to remove those if you don't want/need to use them. +# yaml-language-server: $schema=https://goreleaser.com/static/schema.json +# vim: set ts=2 sw=2 tw=0 fo=cnqoj + + +project_name: countdown +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + goarch: + - amd64 + - arm64 +