diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..ccc125b --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,29 @@ +name: Release + +on: + push: + tags: + - "*" + +permissions: + contents: write + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - run: git fetch --force --tags + - uses: actions/setup-go@v3 + with: + go-version: ">=1.19.3" + cache: true + - uses: goreleaser/goreleaser-action@v2 + with: + distribution: goreleaser + version: latest + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b288307 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +gomatrix-lite +.vscode diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..9b124b4 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,23 @@ +project_name: gomatrix-lite + +builds: + - env: [CGO_ENABLED=0] + goos: + - linux + - windows + - darwin + goarch: + - amd64 + - "386" + - arm + - arm64 + +nfpms: + - maintainer: Alexandros Solanos + description: A matrix screen written in go + homepage: https://github.com/hytromo/gomatrix-lite + license: MIT + formats: + - deb + - rpm + - apk diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..de86264 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2022 Alexandros Solanos + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..6a8f65e --- /dev/null +++ b/README.md @@ -0,0 +1,44 @@ +

+gomatrix-lite +
+
+Showcase of using the tool +
+
+

+ +- [Usage](#usage) +- [Installation](#installation) +- [Purpose](#purpose) + +# Usage + +``` +Usage of ./gomatrix-lite: + -c string + Matrix colors, can be up to 2 comma-separated colors for gradient (shorthand) (default "000000,00FF00") + -color string + Matrix colors, can be up to 2 comma-separated colors for gradient (default "000000,00FF00") + -v Show version (shorthand) + -version + Show version +``` + +Use the numbers 0-9 to control the speed. Use `q` or Ctrl+C to quit the app. + +# Installation + +Go over to the Releases and pick up a tarballed binary for your OS/arch, or a packaged file (deb, rpm etc) + +# Purpose + +This is obviously just a toy to see how easy it'd be to make something like `cmatrix` using Golang. + +The name came from the fact that this seems to be lighter on the CPU usage from quite a few other implementations: + +

+CPU usage comparison between similar tools +

+ +The comparison was made with as similar settings as possible (speed, character set) and in terminals of the same dimensions. +Not much thought has gone into this project, it probably can be optimized more. diff --git a/args.go b/args.go new file mode 100644 index 0000000..da8eed5 --- /dev/null +++ b/args.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "os" + + "github.com/jessevdk/go-flags" +) + +var opts struct { + Version bool `short:"v" long:"version" description:"Show version"` + + Color string `short:"c" long:"color" description:"Matrix colors, can be up to 2 comma-separated colors for gradient" default:"000000,00FF00"` +} + +type Config struct { + showVersion bool + colors Colors +} + +func ParseArgs() Config { + _, err := flags.Parse(&opts) + + if err != nil { + flagError := err.(*flags.Error) + if flagError.Type == flags.ErrHelp { + os.Exit(0) + } else if flagError.Type == flags.ErrUnknownFlag { + fmt.Println("Use --help to view all available options.") + os.Exit(0) + } else { + fmt.Printf("Error parsing flags: %s\n", err) + os.Exit(1) + } + } + + return Config{ + showVersion: opts.Version, + colors: parseColors(opts.Color), + } +} diff --git a/colors.go b/colors.go new file mode 100644 index 0000000..611c58c --- /dev/null +++ b/colors.go @@ -0,0 +1,46 @@ +package main + +import ( + "strings" + + "github.com/gdamore/tcell/v2" +) + +type Colors struct { + start tcell.Color + end tcell.Color +} + +func parseColors(color string) Colors { + var startColor tcell.Color + var endColor tcell.Color + + if strings.Contains(color, ",") { + colorsArr := strings.Split(color, ",") + + startColor = tcell.GetColor("#" + colorsArr[0]) + endColor = tcell.GetColor("#" + colorsArr[1]) + } else { + startColor = tcell.GetColor("#" + color) + endColor = startColor + } + + return Colors{ + start: startColor, + end: endColor, + } +} + +func pickBetweenGradient(color1 tcell.Color, color2 tcell.Color, weight float32) tcell.Color { + w2 := weight + w1 := 1.0 - w2 + c1r, c1g, c1b := color1.RGB() + c2r, c2g, c2b := color2.RGB() + rgb := tcell.NewRGBColor( + int32(float32(c1r)*w1+float32(c2r)*w2), + int32(float32(c1g)*w1+float32(c2g)*w2), + int32(float32(c1b)*w1+float32(c2b)*w2), + ) + + return rgb +} diff --git a/comparison.jpg b/comparison.jpg new file mode 100755 index 0000000..e765133 Binary files /dev/null and b/comparison.jpg differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..05a84d8 --- /dev/null +++ b/go.mod @@ -0,0 +1,18 @@ +module github.com/hytromo/gomatrix-lite + +go 1.19 + +require ( + github.com/gdamore/tcell/v2 v2.5.3 + github.com/jessevdk/go-flags v1.5.0 +) + +require ( + github.com/gdamore/encoding v1.0.0 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-runewidth v0.0.13 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + golang.org/x/sys v0.0.0-20220318055525-2edf467146b5 // indirect + golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect + golang.org/x/text v0.3.7 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..dc86fc1 --- /dev/null +++ b/go.sum @@ -0,0 +1,22 @@ +github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko= +github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= +github.com/gdamore/tcell/v2 v2.5.3 h1:b9XQrT6QGbgI7JvZOJXFNczOQeIYbo8BfeSMzt2sAV0= +github.com/gdamore/tcell/v2 v2.5.3/go.mod h1:wSkrPaXoiIWZqW/g7Px4xc79di6FTcpB8tvaKJ6uGBo= +github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc= +github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= +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-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= +github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +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-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220318055525-2edf467146b5 h1:saXMvIOKvRFwbOMicHXr0B1uwoxq9dGmLe5ExMES6c4= +golang.org/x/sys v0.0.0-20220318055525-2edf467146b5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf h1:MZ2shdL+ZM/XzY3ZGOnh4Nlpnxz5GSOhOmtHo3iPU6M= +golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +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..1fc7020 --- /dev/null +++ b/main.go @@ -0,0 +1,73 @@ +package main + +import ( + "fmt" + "log" + + "os" + + "github.com/gdamore/tcell/v2" +) + +const VERSION = "0.0.1" + +func eventLoop(xmax *int, ymax *int, waitTimeMs *int64, _s *tcell.Screen) { + s := *_s + + for { + s.Show() + + ev := s.PollEvent() + + switch ev := ev.(type) { + case *tcell.EventResize: + s.Sync() + *xmax, *ymax = s.Size() + case *tcell.EventKey: + if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC { + return + } else if ev.Key() == tcell.KeyCtrlL { + s.Sync() + } else if ev.Rune() == 'Q' || ev.Rune() == 'q' { + s.Fini() + os.Exit(0) + } else if ev.Rune() >= '0' && ev.Rune() <= '9' { + (*waitTimeMs) = 95 - int64((ev.Rune()-'0')*10) + } + } + } +} + +func main() { + config := ParseArgs() + + if config.showVersion { + fmt.Println(VERSION) + return + } + + s, err := tcell.NewScreen() + if err != nil { + log.Fatalf("%+v", err) + } + if err := s.Init(); err != nil { + log.Fatalf("%+v", err) + } + s.EnablePaste() + s.Clear() + + quit := func() { + maybePanic := recover() + s.Fini() + if maybePanic != nil { + panic(maybePanic) + } + } + defer quit() + + xmax, ymax := s.Size() + var waitTime int64 = 35 + + go Matrix(&xmax, &ymax, &waitTime, &config.colors, &s) + eventLoop(&xmax, &ymax, &waitTime, &s) +} diff --git a/matrix.go b/matrix.go new file mode 100644 index 0000000..0b0d699 --- /dev/null +++ b/matrix.go @@ -0,0 +1,137 @@ +package main + +import ( + "math/rand" + "time" + + "github.com/gdamore/tcell/v2" +) + +// the minimum length of a vertical string of characters +const MIN_STRING_LENGTH = 8 + +func initMatrix(xmax int, ymax int, colors *Colors) ([][]rune, []tcell.Style) { + matrix := make([][]rune, xmax) + colorGradient := make([]tcell.Style, ymax) + + for i := 0; i < ymax; i++ { + if colors.start == colors.end && i > 0 { + colorGradient[i] = colorGradient[0] + } else { + colorGradient[i] = tcell.StyleDefault.Foreground( + pickBetweenGradient( + colors.start, + colors.end, + float32(i)/float32(ymax), + ), + ) + } + } + + for i := range matrix { + matrix[i] = make([]rune, ymax) + } + + return matrix, colorGradient +} +func min(a, b int) int { + if a < b { + return a + } + return b +} + +func Matrix(xmax *int, ymax *int, waitTimeMs *int64, colors *Colors, _s *tcell.Screen) { + xmaxOld := *xmax + ymaxOld := *ymax + minStringLength := min(MIN_STRING_LENGTH, *ymax) + + s := *_s + + matrix, colorGradient := initMatrix(*xmax, *ymax, colors) + + whiteStyle := tcell.StyleDefault.Foreground( + tcell.ColorWhite, + ) + + createHead := func(column int, row int) { + matrix[column][row] = rune(rand.Intn(94) + 33) + s.SetContent(column, row, matrix[column][row], nil, whiteStyle) + } + + for { + s.Show() + afterLastDraw := time.Now() + + if (xmaxOld != *xmax) || (ymaxOld != *ymax) { + s.Clear() + xmaxOld = *xmax + ymaxOld = *ymax + minStringLength = min(MIN_STRING_LENGTH, *ymax) + matrix, colorGradient = initMatrix(*xmax, *ymax, colors) + } + + if minStringLength < 4 { + continue + } + + for column := range matrix { + last := len(matrix[column]) - 1 + for row := last; row >= 0; row-- { + if row != 0 { + if matrix[column][row-1] == 0 { + // if the character above is empty, move it down (chop the tail) + if matrix[column][row] != 0 { + matrix[column][row] = 0 + s.SetContent(column, row, matrix[column][row], nil, whiteStyle) + } + } else if matrix[column][row] == 0 { + // if the character above is not empty and the current character is empty, create more head + createHead(column, row) + // change previous head to the appropriate color + s.SetContent(column, row-1, matrix[column][row-1], nil, colorGradient[row-1]) + + // fuck the police 😎 + // here we are at the head so we know that we have at least minStringLength chars that we can skip because they are drawn + row -= (minStringLength - 1) + } + + if (row == last) && (matrix[column][row] != 0) { + s.SetContent(column, row, matrix[column][row], nil, colorGradient[row]) + } + } else { + // row == 0 + if matrix[column][row] == 0 { + // empty cell + if rand.Intn(*xmax/2) == 1 { + // begin new head + createHead(column, row) + } + } else { + // cell with content + if rand.Intn(*ymax/2) == 1 { + // this vertical-string has been chosen to be ended if it has the minimum length + + hasMinLength := true + for i := 0; i < minStringLength; i++ { + if matrix[column][row+i] == 0 { + hasMinLength = false + break + } + } + + if hasMinLength { + // finish this column-string + matrix[column][row] = 0 + s.SetContent(column, row, matrix[column][row], nil, whiteStyle) + } + } + } + } + } + } + + duration := time.Since(afterLastDraw) + time.Sleep(time.Duration((*waitTimeMs)-duration.Milliseconds()) * time.Millisecond) + } +} diff --git a/usage.gif b/usage.gif new file mode 100755 index 0000000..271c113 Binary files /dev/null and b/usage.gif differ