Skip to content

Commit c4a0e91

Browse files
Merge pull request #126 from Dee-6777/add-slide-bug
OSD-15763 & OSD-15766 : Fix the program crashing error when many slides are removed at once
2 parents 266dbf0 + 77c01f3 commit c4a0e91

File tree

3 files changed

+34
-35
lines changed

3 files changed

+34
-35
lines changed

pkg/ui/input.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (tui *TUI) initKeyboard() {
6969
}
7070
AddNewSlide(tui, constants.OcmContainer, OcmContainerPath, []string{}, false)
7171
return nil
72-
// Delete the current active Slide
72+
7373
} else if event.Key() == tcell.KeyCtrlB {
7474
// Reset the input buffer
7575
tui.TerminalInputBuffer = []rune{}
@@ -91,9 +91,11 @@ func (tui *TUI) initKeyboard() {
9191
tui.TerminalInputBuffer = []rune{}
9292
}
9393
return nil
94+
// Delete the current active Slide
9495
} else if event.Key() == tcell.KeyCtrlE {
9596
slideNum, _ := strconv.Atoi(tui.TerminalPageBar.GetHighlights()[0])
9697
RemoveSlide(slideNum, tui)
98+
tui.TerminalInputBuffer = []rune{}
9799
return nil
98100
} else if event.Key() == tcell.KeyBackspace || event.Key() == tcell.KeyBackspace2 {
99101
if len(tui.TerminalInputBuffer) > 0 {

pkg/ui/tab.go

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,21 @@ import (
1212
// Declares the tab struct
1313
type TerminalTab struct {
1414
index int
15+
regionID int
1516
title string
1617
primitive tview.Primitive
1718
}
1819

1920
var CurrentActivePage int = 0
20-
var TotalPageCount int = -1
21+
var TotalPageCount int = 0
2122
var CursorPos int
2223

2324
// Creates and return a new tab
2425
func InitKiteTab(tui *TUI, layout *tview.Flex) *TerminalTab {
25-
TotalPageCount += 1
2626
tui.TerminalUIRegionIDs = append(tui.TerminalUIRegionIDs, TotalPageCount)
27-
index := len(tui.TerminalTabs)
28-
if len(tui.TerminalTabs) == 0 {
29-
index = 0
30-
}
3127
return &TerminalTab{
32-
index: index,
28+
index: 0,
29+
regionID: 0,
3330
title: "kite",
3431
primitive: layout,
3532
}
@@ -40,11 +37,9 @@ func NewTab(name string, command string, args []string, tui *TUI) *TerminalTab {
4037
TotalPageCount += 1
4138
tui.TerminalUIRegionIDs = append(tui.TerminalUIRegionIDs, TotalPageCount)
4239
index := len(tui.TerminalTabs)
43-
if len(tui.TerminalTabs) == 0 {
44-
index = 0
45-
}
4640
return &TerminalTab{
4741
index: index,
42+
regionID: TotalPageCount,
4843
title: name,
4944
primitive: newTabPrimitive(command, args),
5045
}
@@ -62,22 +57,22 @@ func newTabPrimitive(command string, args []string) (content tview.Primitive) {
6257
// Move to the previous slide
6358
func PreviousSlide(tui *TUI) {
6459
CurrentActivePage = (CurrentActivePage - 1 + len(tui.TerminalTabs)) % len(tui.TerminalTabs)
65-
tui.TerminalPageBar.Highlight(strconv.Itoa(tui.TerminalUIRegionIDs[CurrentActivePage])).
60+
tui.TerminalPageBar.Highlight(strconv.Itoa(tui.TerminalTabs[CurrentActivePage].regionID)).
6661
ScrollToHighlight()
6762
tui.TerminalInputBuffer = []rune{}
6863
}
6964

7065
// Move to the next slide
7166
func NextSlide(tui *TUI) {
72-
CurrentActivePage = (CurrentActivePage + 1) % len(tui.TerminalTabs)
73-
tui.TerminalPageBar.Highlight(strconv.Itoa(tui.TerminalUIRegionIDs[CurrentActivePage])).
67+
CurrentActivePage = (CurrentActivePage + 1 + len(tui.TerminalTabs)) % len(tui.TerminalTabs)
68+
tui.TerminalPageBar.Highlight(strconv.Itoa(tui.TerminalTabs[CurrentActivePage].regionID)).
7469
ScrollToHighlight()
7570
tui.TerminalInputBuffer = []rune{}
7671
}
7772

78-
func indexOf(arr []int, ele int) int {
79-
for index, item := range arr {
80-
if item == ele {
73+
func indexOf(ele int, tabs []TerminalTab) int {
74+
for index, item := range tabs {
75+
if item.regionID == ele {
8176
return index
8277
}
8378
}
@@ -86,37 +81,39 @@ func indexOf(arr []int, ele int) int {
8681

8782
// Remove the slide with the given index
8883
func RemoveSlide(s int, tui *TUI) {
89-
index := indexOf(tui.TerminalUIRegionIDs, s)
84+
index := indexOf(s, tui.TerminalTabs)
9085
tui.TerminalTabs = append(tui.TerminalTabs[:index], tui.TerminalTabs[index+1:]...)
9186
tui.TerminalUIRegionIDs = append(tui.TerminalUIRegionIDs[:index], tui.TerminalUIRegionIDs[index+1:]...)
9287
tui.TerminalPageBar.Clear()
9388
for index, tabSlide := range tui.TerminalTabs {
94-
oldIndex := tabSlide.index
9589
tabSlide.index = index
96-
fmt.Fprintf(tui.TerminalPageBar, `["%d"]%s[white][""] `, oldIndex, fmt.Sprintf("%d %s", tabSlide.index+1, tabSlide.title))
90+
fmt.Fprintf(tui.TerminalPageBar, `["%d"]%s[white][""] `, tabSlide.regionID, fmt.Sprintf("%d %s", tabSlide.index+1, tabSlide.title))
9791
}
9892
tui.TerminalPages.RemovePage(strconv.Itoa(s))
9993
PreviousSlide(tui)
94+
tui.TerminalInputBuffer = []rune{}
10095
}
10196

10297
// Adds a slide to the end of currently present slides
10398
func AddNewSlide(tui *TUI, name string, command string, args []string, isCluster bool) {
104-
if isCluster {
105-
for i, tab := range tui.TerminalTabs {
106-
if tab.title == args[0] {
107-
tui.TerminalPageBar.Highlight(strconv.Itoa(i)).
108-
ScrollToHighlight()
109-
return
99+
if len(tui.TerminalTabs) < 9 {
100+
if isCluster {
101+
for i, tab := range tui.TerminalTabs {
102+
if tab.primitive != nil && tab.title == args[0] {
103+
tui.TerminalPageBar.Highlight(strconv.Itoa(i)).
104+
ScrollToHighlight()
105+
return
106+
}
110107
}
111108
}
109+
tabSlide := NewTab(name, command, args, tui)
110+
tui.TerminalTabs = append(tui.TerminalTabs, *tabSlide)
111+
tui.TerminalPages.AddPage(strconv.Itoa(tabSlide.regionID), tabSlide.primitive, true, true)
112+
fmt.Fprintf(tui.TerminalPageBar, `["%d"]%s[white][""] `, tabSlide.regionID, fmt.Sprintf("%d %s", tabSlide.index+1, tabSlide.title))
113+
CurrentActivePage = tabSlide.index
114+
tui.TerminalPageBar.Highlight(strconv.Itoa(tabSlide.regionID)).
115+
ScrollToHighlight()
112116
}
113-
tabSlide := NewTab(name, command, args, tui)
114-
tui.TerminalTabs = append(tui.TerminalTabs, *tabSlide)
115-
tui.TerminalPages.AddPage(strconv.Itoa(tabSlide.index), tabSlide.primitive, true, tabSlide.index == 0)
116-
fmt.Fprintf(tui.TerminalPageBar, `["%d"]%s[white][""] `, tabSlide.index, fmt.Sprintf("%d %s", tabSlide.index+1, tabSlide.title))
117-
CurrentActivePage = tabSlide.index
118-
tui.TerminalPageBar.Highlight(strconv.Itoa(CurrentActivePage)).
119-
ScrollToHighlight()
120117
tui.TerminalInputBuffer = []rune{}
121118
}
122119

@@ -134,7 +131,7 @@ func InitTerminalMux(tui *TUI, kiteTab *TerminalTab) *tview.Flex {
134131
})
135132

136133
for _, slide := range tui.TerminalTabs {
137-
tui.TerminalPages.AddPage(strconv.Itoa(slide.index), slide.primitive, true, slide.index == 0)
134+
tui.TerminalPages.AddPage(strconv.Itoa(slide.index), slide.primitive, true, true)
138135
fmt.Fprintf(tui.TerminalPageBar, `["%d"]%s[white][""] `, slide.index, fmt.Sprintf("%d %s", slide.index+1, slide.title))
139136
}
140137
tui.TerminalPageBar.Highlight("0")

pkg/ui/tui.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,5 @@ func (t *TUI) StartApp() error {
196196
t.initFooter()
197197
t.initKeyboard()
198198

199-
return t.App.SetRoot(t.TerminalLayout, true).EnableMouse(true).Run()
199+
return t.App.SetRoot(t.TerminalLayout, true).EnableMouse(false).Run()
200200
}

0 commit comments

Comments
 (0)