Add bounds checking and defensive programming improvements
Co-authored-by: foglar <82380203+foglar@users.noreply.github.com>
This commit is contained in:
parent
cfedfd3550
commit
0e79ba9083
@ -56,7 +56,7 @@ func updateChatView(v *gocui.View) {
|
|||||||
h = 0
|
h = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(users) == 0 {
|
if len(users) == 0 || selectedUserIdx >= len(users) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,7 +72,7 @@ func updateChatView(v *gocui.View) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sendMessage(g *gocui.Gui, v *gocui.View) error {
|
func sendMessage(g *gocui.Gui, v *gocui.View) error {
|
||||||
if len(users) == 0 {
|
if len(users) == 0 || selectedUserIdx >= len(users) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -33,14 +33,22 @@ func updateContactsView(g *gocui.Gui) error {
|
|||||||
return errors.New("no contacts in the list, find some friends")
|
return errors.New("no contacts in the list, find some friends")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure selectedUserIdx is within bounds
|
||||||
|
if selectedUserIdx < 0 || selectedUserIdx >= len(users) {
|
||||||
|
selectedUserIdx = 0
|
||||||
|
}
|
||||||
|
|
||||||
LoadMessages(users[selectedUserIdx])
|
LoadMessages(users[selectedUserIdx])
|
||||||
|
|
||||||
_, maxY := g.Size()
|
_, maxY := g.Size()
|
||||||
h := min(len(users), (maxY/2)-1)
|
h := min(len(users), (maxY/2)-1)
|
||||||
|
if h <= 0 {
|
||||||
|
h = 1
|
||||||
|
}
|
||||||
startI := max(0, min(selectedUserIdx-(h/2), len(users)-h))
|
startI := max(0, min(selectedUserIdx-(h/2), len(users)-h))
|
||||||
|
|
||||||
fmt.Fprint(v, "\n\n")
|
fmt.Fprint(v, "\n\n")
|
||||||
for i := startI; i < startI+h; i++ {
|
for i := startI; i < startI+h && i < len(users); i++ {
|
||||||
u := users[i]
|
u := users[i]
|
||||||
|
|
||||||
fmt.Fprint(v, "\t\t\t\t")
|
fmt.Fprint(v, "\t\t\t\t")
|
||||||
|
|||||||
@ -14,6 +14,11 @@ var selectedUserIdx int = 0
|
|||||||
func Run() {
|
func Run() {
|
||||||
LoadContacts(defaultServerPath)
|
LoadContacts(defaultServerPath)
|
||||||
|
|
||||||
|
// Load initial messages if there are any contacts
|
||||||
|
if len(users) > 0 && selectedUserIdx < len(users) {
|
||||||
|
LoadMessages(users[selectedUserIdx])
|
||||||
|
}
|
||||||
|
|
||||||
g, err := gocui.NewGui(gocui.OutputNormal)
|
g, err := gocui.NewGui(gocui.OutputNormal)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user