Skip to content

Commit

Permalink
matix refresh interval -> param (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
sha1n committed May 30, 2021
1 parent 84d9285 commit 8de1355
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
5 changes: 3 additions & 2 deletions internal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ func demo(t termite.Terminal) {
func demoMatrix(t termite.Terminal) {
printTitle("Matrix Layout", t)

m := termite.NewMatrix(t)
refreshInterval := time.Millisecond * 10
m := termite.NewMatrix(t, refreshInterval)
cancel := m.Start()

lines := []io.StringWriter{
m.NewLineStringWriter(), m.NewLineStringWriter(), m.NewLineStringWriter(), m.NewLineStringWriter(), m.NewLineStringWriter(),
}

for i := 0; i < 100; i++ {
time.Sleep(time.Millisecond * 10)
time.Sleep(refreshInterval)
lines[i%len(lines)].WriteString(fmt.Sprintf("- Matrix Line -> version %d", i+1))
}

Expand Down
11 changes: 3 additions & 8 deletions matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

// Matrix is a multiline structure that reflects its state on screen
type Matrix interface {
StringWriter() io.StringWriter
RefreshInterval() time.Duration
NewLineStringWriter() io.StringWriter
NewLineWriter() io.Writer
Expand All @@ -34,20 +33,16 @@ type matrixLineWriter struct {
matrix *terminalMatrix
}

// NewMatrix creates a new Matrix for the specified Terminal
func NewMatrix(writer io.StringWriter) Matrix {
// NewMatrix creates a new matrix that writes to the specified writer and refreshes every refreshInterval.
func NewMatrix(writer io.StringWriter, refreshInterval time.Duration) Matrix {
return &terminalMatrix{
lines: []string{},
refreshInterval: time.Millisecond * 100,
refreshInterval: refreshInterval,
writer: writer,
mx: &sync.RWMutex{},
}
}

func (m *terminalMatrix) StringWriter() io.StringWriter {
return m.writer
}

func (m *terminalMatrix) RefreshInterval() time.Duration {
return m.refreshInterval
}
Expand Down
16 changes: 8 additions & 8 deletions matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestMatrixWritesToTerminalOutput(t *testing.T) {
examples := generateMultiLineExamples(3)

matrix, cancel := startMatrix()
matrix, cancel := startNewMatrix()
defer cancel()

matrix.NewLineStringWriter().WriteString(examples[0])
Expand All @@ -28,7 +28,7 @@ func TestMatrixWritesToTerminalOutput(t *testing.T) {
func TestMatrixUpdatesTerminalOutput(t *testing.T) {
examples := generateMultiLineExamples(3)

matrix, cancel := startMatrix()
matrix, cancel := startNewMatrix()
defer cancel()

matrix.NewLineStringWriter().WriteString(examples[0])
Expand All @@ -44,7 +44,7 @@ func TestMatrixUpdatesTerminalOutput(t *testing.T) {
func TestMatrixStructure(t *testing.T) {
examples := generateMultiLineExamples(3)

matrix, cancel := startMatrix()
matrix, cancel := startNewMatrix()
defer cancel()

matrix.NewLineStringWriter().WriteString(examples[0])
Expand All @@ -57,10 +57,10 @@ func TestMatrixStructure(t *testing.T) {
func TestWriterLineInterface(t *testing.T) {
example := generateRandomString()

matrix1, cancel1 := startMatrix()
matrix1, cancel1 := startNewMatrix()
defer cancel1()

matrix2, cancel2 := startMatrix()
matrix2, cancel2 := startNewMatrix()
defer cancel2()

matrix1.NewLineStringWriter().WriteString(example)
Expand All @@ -72,7 +72,7 @@ func TestWriterLineInterface(t *testing.T) {
func assertEventualSequence(t *testing.T, matrix Matrix, examples []string) {
contantsAllExamplesInOrderFn := func() bool {
return strings.Contains(
matrix.StringWriter().(*fakeTerm).Out.String(),
matrix.(*terminalMatrix).writer.(*fakeTerm).Out.String(),
expectedOutputSequenceFor(examples),
)
}
Expand All @@ -93,9 +93,9 @@ func expectedOutputSequenceFor(examples []string) string {
return buf.String()
}

func startMatrix() (Matrix, context.CancelFunc) {
func startNewMatrix() (Matrix, context.CancelFunc) {
term := NewFakeTerminal(80, 80)
matrix := NewMatrix(term)
matrix := NewMatrix(term, time.Millisecond)
cancel := matrix.Start()

return matrix, cancel
Expand Down

0 comments on commit 8de1355

Please sign in to comment.