Skip to content

Commit

Permalink
Feature/async (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandros Solanos authored Nov 12, 2022
1 parent df8dd1c commit 3527e11
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 27 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ gomatrix-lite
# 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
Usage:
gomatrix-lite [OPTIONS]
Application Options:
-v, --version Show version
-c, --color= Matrix colors, can be up to 2 comma-separated colors for gradient (default: 000000,00FF00)
--no-async Disable asynchronous mode, make every line has the same speed
--no-bold Disable bold characters
Help Options:
-h, --help Show this help message
```

Use the numbers 0-9 to control the speed. Use `q` or Ctrl+C to quit the app.
Expand Down
8 changes: 8 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ 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"`

NoAsync bool `long:"no-async" description:"Disable asynchronous mode, make every line has the same speed"`

NoBold bool `long:"no-bold" description:"Disable bold characters"`
}

type Config struct {
showVersion bool
colors Colors
async bool
bold bool
}

func ParseArgs() Config {
Expand All @@ -37,5 +43,7 @@ func ParseArgs() Config {
return Config{
showVersion: opts.Version,
colors: parseColors(opts.Color),
async: !opts.NoAsync,
bold: !opts.NoBold,
}
}
14 changes: 9 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import (
"github.com/gdamore/tcell/v2"
)

const VERSION = "0.0.1"
const VERSION = "0.0.2"

func eventLoop(xmax *int, ymax *int, waitTimeMs *int64, _s *tcell.Screen) {
func getWaitTimeForSpeed(speed rune) uint64 {
return 20 - uint64((speed-'0')*2)
}

func eventLoop(xmax *int, ymax *int, waitTimeMs *uint64, _s *tcell.Screen) {
s := *_s

for {
Expand All @@ -32,7 +36,7 @@ func eventLoop(xmax *int, ymax *int, waitTimeMs *int64, _s *tcell.Screen) {
s.Fini()
os.Exit(0)
} else if ev.Rune() >= '0' && ev.Rune() <= '9' {
(*waitTimeMs) = 95 - int64((ev.Rune()-'0')*10)
(*waitTimeMs) = getWaitTimeForSpeed(ev.Rune())
}
}
}
Expand Down Expand Up @@ -66,8 +70,8 @@ func main() {
defer quit()

xmax, ymax := s.Size()
var waitTime int64 = 35
var waitTime uint64 = getWaitTimeForSpeed('7')

go Matrix(&xmax, &ymax, &waitTime, &config.colors, &s)
go Matrix(&xmax, &ymax, &waitTime, &config, &s)
eventLoop(&xmax, &ymax, &waitTime, &s)
}
50 changes: 36 additions & 14 deletions matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ import (
// 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) {
func initMatrix(xmax int, ymax int, config *Config) ([][]rune, []tcell.Style, tcell.Style, []uint64) {
// the characters
matrix := make([][]rune, xmax)
// the color of each row
colorGradient := make([]tcell.Style, ymax)
// controls the speed
columnDrag := make([]uint64, xmax)
// the start and end colors
colors := config.colors

characterStyleAttr := tcell.AttrNone
if config.bold {
characterStyleAttr = tcell.AttrBold
}

for i := 0; i < ymax; i++ {
if colors.start == colors.end && i > 0 {
Expand All @@ -24,42 +35,49 @@ func initMatrix(xmax int, ymax int, colors *Colors) ([][]rune, []tcell.Style) {
colors.end,
float32(i)/float32(ymax),
),
)
).Attributes(characterStyleAttr)
}
}

for i := range matrix {
matrix[i] = make([]rune, ymax)
if config.async {
columnDrag[i] = uint64(rand.Intn(6) + 5)
} else {
columnDrag[i] = 7
}
}

return matrix, colorGradient
whiteStyle := tcell.StyleDefault.Foreground(
tcell.ColorWhite,
).Attributes(characterStyleAttr)

return matrix, colorGradient, whiteStyle, columnDrag
}

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) {
func Matrix(xmax *int, ymax *int, waitTimeMs *uint64, _config *Config, _s *tcell.Screen) {
xmaxOld := *xmax
ymaxOld := *ymax
config := *_config
minStringLength := min(MIN_STRING_LENGTH, *ymax)

s := *_s

matrix, colorGradient := initMatrix(*xmax, *ymax, colors)

whiteStyle := tcell.StyleDefault.Foreground(
tcell.ColorWhite,
)
matrix, colorGradient, whiteStyle, columnDrag := initMatrix(*xmax, *ymax, &config)

createHead := func(column int, row int) {
matrix[column][row] = rune(rand.Intn(94) + 33)
s.SetContent(column, row, matrix[column][row], nil, whiteStyle)
}

for {
for loopCounter := uint64(0); ; loopCounter++ {
s.Show()
afterLastDraw := time.Now()

Expand All @@ -68,14 +86,18 @@ func Matrix(xmax *int, ymax *int, waitTimeMs *int64, colors *Colors, _s *tcell.S
xmaxOld = *xmax
ymaxOld = *ymax
minStringLength = min(MIN_STRING_LENGTH, *ymax)
matrix, colorGradient = initMatrix(*xmax, *ymax, colors)
matrix, colorGradient, whiteStyle, columnDrag = initMatrix(*xmax, *ymax, &config)
}

if minStringLength < 4 {
continue
}

for column := range matrix {
columnShouldMove := loopCounter%columnDrag[column] == 0
if !columnShouldMove {
continue
}
last := len(matrix[column]) - 1
for row := last; row >= 0; row-- {
if row != 0 {
Expand Down Expand Up @@ -103,13 +125,13 @@ func Matrix(xmax *int, ymax *int, waitTimeMs *int64, colors *Colors, _s *tcell.S
// row == 0
if matrix[column][row] == 0 {
// empty cell
if rand.Intn(*xmax/2) == 1 {
if rand.Intn(350/int(columnDrag[column])) == 1 {
// begin new head
createHead(column, row)
}
} else {
// cell with content
if rand.Intn(*ymax/2) == 1 {
if rand.Intn(100/int(columnDrag[column])) == 1 {
// this vertical-string has been chosen to be ended if it has the minimum length

hasMinLength := true
Expand All @@ -132,6 +154,6 @@ func Matrix(xmax *int, ymax *int, waitTimeMs *int64, colors *Colors, _s *tcell.S
}

duration := time.Since(afterLastDraw)
time.Sleep(time.Duration((*waitTimeMs)-duration.Milliseconds()) * time.Millisecond)
time.Sleep(time.Duration((*waitTimeMs)-uint64(duration.Milliseconds())) * time.Millisecond)
}
}
Binary file modified usage.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3527e11

Please sign in to comment.