-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathkeyboard.go
112 lines (92 loc) · 3 KB
/
keyboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package botgolang
import (
"fmt"
)
// Keyboard represents an inline keyboard markup
// Call the NewKeyboard() func to get a keyboard instance
type Keyboard struct {
Rows [][]Button
}
// NewKeyboard returns a new keyboard instance
func NewKeyboard() Keyboard {
return Keyboard{
Rows: make([][]Button, 0),
}
}
// AddRows adds a row to the keyboard
func (k *Keyboard) AddRow(row ...Button) {
k.Rows = append(k.Rows, row)
}
// AddButton adds a button to the end of the row
func (k *Keyboard) AddButton(rowIndex int, button Button) error {
if ok := k.checkRow(rowIndex); !ok {
return fmt.Errorf("no such row: %d", rowIndex)
}
k.Rows[rowIndex] = append(k.Rows[rowIndex], button)
return nil
}
// DeleteRow removes the row from the keyboard
func (k *Keyboard) DeleteRow(index int) error {
if ok := k.checkRow(index); !ok {
return fmt.Errorf("no such row: %d", index)
}
k.Rows = append(k.Rows[:index], k.Rows[index+1:]...)
return nil
}
// DeleteButton removes the button from the row.
// Note - at least one button should remain in a row,
// if you want to delete all buttons, use the DeleteRow function
func (k *Keyboard) DeleteButton(rowIndex, buttonIndex int) error {
if ok := k.checkButton(rowIndex, buttonIndex); !ok {
return fmt.Errorf("no button at index %d or row %d", buttonIndex, rowIndex)
}
if k.RowSize(rowIndex) < 2 {
return fmt.Errorf("can't delete button: at least one should remain in a row")
}
row := &k.Rows[rowIndex]
*row = append((*row)[:buttonIndex], (*row)[buttonIndex+1:]...)
return nil
}
// ChangeButton changes the button to a new one at the specified position
func (k *Keyboard) ChangeButton(rowIndex, buttonIndex int, newButton Button) error {
if ok := k.checkButton(rowIndex, buttonIndex); !ok {
return fmt.Errorf("no button at index %d or row %d", buttonIndex, rowIndex)
}
k.Rows[rowIndex][buttonIndex] = newButton
return nil
}
// SwapRows swaps two rows in keyboard
func (k *Keyboard) SwapRows(first, second int) error {
if ok := k.checkRow(first); !ok {
return fmt.Errorf("no such index (first): %d", first)
}
if ok := k.checkRow(second); !ok {
return fmt.Errorf("no such index (second): %d", second)
}
k.Rows[first], k.Rows[second] = k.Rows[second], k.Rows[first]
return nil
}
// RowsCount returns the number of rows
func (k *Keyboard) RowsCount() int {
return len(k.Rows)
}
// RowSize returns the number of buttons in a row.
// If there is no such row, then returns -1
func (k *Keyboard) RowSize(row int) int {
if ok := k.checkRow(row); !ok {
return -1
}
return len(k.Rows[row])
}
// GetKeyboard returns an array of button rows
func (k *Keyboard) GetKeyboard() [][]Button {
return k.Rows
}
// checkRow checks that the index of row doesnt go beyond the bounds of the array
func (k *Keyboard) checkRow(i int) bool {
return i >= 0 && i < len(k.Rows)
}
// checkButton checks that the button and row indexes doesnt go beyond the bounds of the array
func (k *Keyboard) checkButton(row, button int) bool {
return k.checkRow(row) && button >= 0 && button < len(k.Rows[row])
}