Skip to content

Commit

Permalink
refactor!: remove deprecated
Browse files Browse the repository at this point in the history
Remove deprecated commands in preparation for release
  • Loading branch information
aymanbagabas committed Mar 25, 2024
1 parent 7199a7f commit f93b16e
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 447 deletions.
28 changes: 0 additions & 28 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,34 +147,6 @@ func Tick(d time.Duration, fn func(time.Time) Msg) Cmd {
}
}

// Sequentially produces a command that sequentially executes the given
// commands.
// The Msg returned is the first non-nil message returned by a Cmd.
//
// func saveStateCmd() Msg {
// if err := save(); err != nil {
// return errMsg{err}
// }
// return nil
// }
//
// cmd := Sequentially(saveStateCmd, Quit)
//
// Deprecated: use Sequence instead.
func Sequentially(cmds ...Cmd) Cmd {
return func() Msg {
for _, cmd := range cmds {
if cmd == nil {
continue
}
if msg := cmd(); msg != nil {
return msg
}
}
return nil
}
}

// setWindowTitleMsg is an internal message used to set the window title.
type setWindowTitleMsg string

Expand Down
24 changes: 17 additions & 7 deletions commands_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tea

import (
"context"
"fmt"
"testing"
"time"
Expand All @@ -26,7 +27,7 @@ func TestTick(t *testing.T) {
}
}

func TestSequentially(t *testing.T) {
func TestSequence(t *testing.T) {
expectedErrMsg := fmt.Errorf("some err")
expectedStrMsg := "some msg"

Expand All @@ -37,12 +38,12 @@ func TestSequentially(t *testing.T) {
tests := []struct {
name string
cmds []Cmd
expected Msg
expected []Msg
}{
{
name: "all nil",
cmds: []Cmd{nilReturnCmd, nilReturnCmd},
expected: nil,
expected: []Msg{nil, nil},
},
{
name: "null cmds",
Expand All @@ -58,7 +59,7 @@ func TestSequentially(t *testing.T) {
},
nilReturnCmd,
},
expected: expectedErrMsg,
expected: []Msg{nil, expectedErrMsg, nil},
},
{
name: "some msg",
Expand All @@ -69,13 +70,22 @@ func TestSequentially(t *testing.T) {
},
nilReturnCmd,
},
expected: expectedStrMsg,
expected: []Msg{nil, expectedStrMsg, nil},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if msg := Sequentially(test.cmds...)(); msg != test.expected {
t.Fatalf("expected a msg %v but got %v", test.expected, msg)
var msgs []Msg
sequentially(context.TODO(), Sequence(test.cmds...)().(sequenceMsg), func(m Msg) {
msgs = append(msgs, m)
})
if len(msgs) != len(test.expected) {
t.Fatalf("expected %d msgs but got %d", len(test.expected), len(msgs))
}
for i, msg := range msgs {
if msg != test.expected[i] {
t.Fatalf("expected a msg %v but got %v", test.expected[i], msg)
}
}
})
}
Expand Down
103 changes: 66 additions & 37 deletions key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ func TestDetectOneMsg(t *testing.T) {
// Mouse event.
seqTest{
[]byte{'\x1b', '[', 'M', byte(32) + 0b0100_0000, byte(65), byte(49)},
MouseMsg{X: 32, Y: 16, Type: MouseWheelUp, Button: MouseButtonWheelUp, Action: MouseActionPress},
MouseMsg{X: 32, Y: 16, Button: MouseButtonWheelUp, Action: MouseActionPress},
},
// SGR Mouse event.
seqTest{
[]byte("\x1b[<0;33;17M"),
MouseMsg{X: 32, Y: 16, Type: MouseLeft, Button: MouseButtonLeft, Action: MouseActionPress},
MouseMsg{X: 32, Y: 16, Button: MouseButtonLeft, Action: MouseActionPress},
},
// Runes.
seqTest{
Expand Down Expand Up @@ -242,7 +242,8 @@ func TestReadInput(t *testing.T) {
out []Msg
}
testData := []test{
{"a",
{
"a",
[]byte{'a'},
[]Msg{
KeyMsg{
Expand All @@ -251,7 +252,8 @@ func TestReadInput(t *testing.T) {
},
},
},
{" ",
{
" ",
[]byte{' '},
[]Msg{
KeyMsg{
Expand All @@ -260,37 +262,42 @@ func TestReadInput(t *testing.T) {
},
},
},
{"a alt+a",
{
"a alt+a",
[]byte{'a', '\x1b', 'a'},
[]Msg{
KeyMsg{Type: KeyRunes, Runes: []rune{'a'}},
KeyMsg{Type: KeyRunes, Runes: []rune{'a'}, Alt: true},
},
},
{"a alt+a a",
{
"a alt+a a",
[]byte{'a', '\x1b', 'a', 'a'},
[]Msg{
KeyMsg{Type: KeyRunes, Runes: []rune{'a'}},
KeyMsg{Type: KeyRunes, Runes: []rune{'a'}, Alt: true},
KeyMsg{Type: KeyRunes, Runes: []rune{'a'}},
},
},
{"ctrl+a",
{
"ctrl+a",
[]byte{byte(keySOH)},
[]Msg{
KeyMsg{
Type: KeyCtrlA,
},
},
},
{"ctrl+a ctrl+b",
{
"ctrl+a ctrl+b",
[]byte{byte(keySOH), byte(keySTX)},
[]Msg{
KeyMsg{Type: KeyCtrlA},
KeyMsg{Type: KeyCtrlB},
},
},
{"alt+a",
{
"alt+a",
[]byte{byte(0x1b), 'a'},
[]Msg{
KeyMsg{
Expand All @@ -300,7 +307,8 @@ func TestReadInput(t *testing.T) {
},
},
},
{"abcd",
{
"abcd",
[]byte{'a', 'b', 'c', 'd'},
[]Msg{
KeyMsg{
Expand All @@ -309,27 +317,29 @@ func TestReadInput(t *testing.T) {
},
},
},
{"up",
{
"up",
[]byte("\x1b[A"),
[]Msg{
KeyMsg{
Type: KeyUp,
},
},
},
{"wheel up",
{
"wheel up",
[]byte{'\x1b', '[', 'M', byte(32) + 0b0100_0000, byte(65), byte(49)},
[]Msg{
MouseMsg{
X: 32,
Y: 16,
Type: MouseWheelUp,
Button: MouseButtonWheelUp,
Action: MouseActionPress,
},
},
},
{"left motion release",
{
"left motion release",
[]byte{
'\x1b', '[', 'M', byte(32) + 0b0010_0000, byte(32 + 33), byte(16 + 33),
'\x1b', '[', 'M', byte(32) + 0b0000_0011, byte(64 + 33), byte(32 + 33),
Expand All @@ -338,32 +348,33 @@ func TestReadInput(t *testing.T) {
MouseMsg(MouseEvent{
X: 32,
Y: 16,
Type: MouseLeft,
Button: MouseButtonLeft,
Action: MouseActionMotion,
}),
MouseMsg(MouseEvent{
X: 64,
Y: 32,
Type: MouseRelease,
Button: MouseButtonNone,
Action: MouseActionRelease,
}),
},
},
{"shift+tab",
{
"shift+tab",
[]byte{'\x1b', '[', 'Z'},
[]Msg{
KeyMsg{
Type: KeyShiftTab,
},
},
},
{"enter",
{
"enter",
[]byte{'\r'},
[]Msg{KeyMsg{Type: KeyEnter}},
},
{"alt+enter",
{
"alt+enter",
[]byte{'\x1b', '\r'},
[]Msg{
KeyMsg{
Expand All @@ -372,15 +383,17 @@ func TestReadInput(t *testing.T) {
},
},
},
{"insert",
{
"insert",
[]byte{'\x1b', '[', '2', '~'},
[]Msg{
KeyMsg{
Type: KeyInsert,
},
},
},
{"alt+ctrl+a",
{
"alt+ctrl+a",
[]byte{'\x1b', byte(keySOH)},
[]Msg{
KeyMsg{
Expand All @@ -389,52 +402,64 @@ func TestReadInput(t *testing.T) {
},
},
},
{"?CSI[45 45 45 45 88]?",
{
"?CSI[45 45 45 45 88]?",
[]byte{'\x1b', '[', '-', '-', '-', '-', 'X'},
[]Msg{unknownCSISequenceMsg([]byte{'\x1b', '[', '-', '-', '-', '-', 'X'})},
},
// Powershell sequences.
{"up",
{
"up",
[]byte{'\x1b', 'O', 'A'},
[]Msg{KeyMsg{Type: KeyUp}},
},
{"down",
{
"down",
[]byte{'\x1b', 'O', 'B'},
[]Msg{KeyMsg{Type: KeyDown}},
},
{"right",
{
"right",
[]byte{'\x1b', 'O', 'C'},
[]Msg{KeyMsg{Type: KeyRight}},
},
{"left",
{
"left",
[]byte{'\x1b', 'O', 'D'},
[]Msg{KeyMsg{Type: KeyLeft}},
},
{"alt+enter",
{
"alt+enter",
[]byte{'\x1b', '\x0d'},
[]Msg{KeyMsg{Type: KeyEnter, Alt: true}},
},
{"alt+backspace",
{
"alt+backspace",
[]byte{'\x1b', '\x7f'},
[]Msg{KeyMsg{Type: KeyBackspace, Alt: true}},
},
{"ctrl+@",
{
"ctrl+@",
[]byte{'\x00'},
[]Msg{KeyMsg{Type: KeyCtrlAt}},
},
{"alt+ctrl+@",
{
"alt+ctrl+@",
[]byte{'\x1b', '\x00'},
[]Msg{KeyMsg{Type: KeyCtrlAt, Alt: true}},
},
{"esc",
{
"esc",
[]byte{'\x1b'},
[]Msg{KeyMsg{Type: KeyEsc}},
},
{"alt+esc",
{
"alt+esc",
[]byte{'\x1b', '\x1b'},
[]Msg{KeyMsg{Type: KeyEsc, Alt: true}},
},
{"[a b] o",
{
"[a b] o",
[]byte{
'\x1b', '[', '2', '0', '0', '~',
'a', ' ', 'b',
Expand All @@ -446,11 +471,13 @@ func TestReadInput(t *testing.T) {
KeyMsg{Type: KeyRunes, Runes: []rune("o")},
},
},
{"[a\x03\nb]",
{
"[a\x03\nb]",
[]byte{
'\x1b', '[', '2', '0', '0', '~',
'a', '\x03', '\n', 'b',
'\x1b', '[', '2', '0', '1', '~'},
'\x1b', '[', '2', '0', '1', '~',
},
[]Msg{
KeyMsg{Type: KeyRunes, Runes: []rune("a\x03\nb"), Paste: true},
},
Expand All @@ -460,11 +487,13 @@ func TestReadInput(t *testing.T) {
// Sadly, utf8.DecodeRune([]byte(0xfe)) returns a valid rune on windows.
// This is incorrect, but it makes our test fail if we try it out.
testData = append(testData,
test{"?0xfe?",
test{
"?0xfe?",
[]byte{'\xfe'},
[]Msg{unknownInputByteMsg(0xfe)},
},
test{"a ?0xfe? b",
test{
"a ?0xfe? b",
[]byte{'a', '\xfe', ' ', 'b'},
[]Msg{
KeyMsg{Type: KeyRunes, Runes: []rune{'a'}},
Expand Down

0 comments on commit f93b16e

Please sign in to comment.