TUI for The Lounge
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

92 lines
2.0 KiB

package main
import (
tea "github.com/charmbracelet/bubbletea"
)
type channelsModel struct {
index int
}
func doOpenChannel(conn *loungeConnection, channel *loungeChannel) tea.Cmd {
return func() tea.Msg {
loungeChannelOpen(conn, channel)
return 1
}
}
func channelsModelInit() channelsModel {
return channelsModel{0}
}
func (m mainModel) channelsInit() tea.Cmd {
return nil
}
func (m mainModel) channelsView() string {
height := m.height - 1
pages := []string{}
pageIndex := []int{}
buf := ""
curIndex := 0
curPage := 0
curLine := 0
for i := 0; i < len(m.conn.Networks); i++ {
net := m.conn.Networks[i]
for i := 0; i < len(net.Channels); i++ {
if net.Channels[i].Type != "lobby" {
buf += "\t"
}
if m.channelsModel.index == curIndex {
buf += highlightText(net.Channels[i].Name) + "\n"
} else {
buf += net.Channels[i].Name + "\n"
}
pageIndex = append(pageIndex, curPage)
curIndex++
curLine++
if curLine >= height - 1 {
curPage++
curLine = 0
pages = append(pages, buf[:len(buf)-1])
buf = ""
}
}
buf += "\n"
curLine += 1
}
if buf != "" {
pages = append(pages, buf[:len(buf)-1])
}
s := "Select a channel\n"
if len(pageIndex) > 0 {
if m.channelsModel.index < len(pageIndex) {
s += pages[pageIndex[m.channelsModel.index]]
} else {
s += pages[len(pages) - 1]
}
} else {
s += "No networks found"
}
return s
}
func (m mainModel) channelsUpdate(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
channels := []*loungeChannel{}
for i := 0; i < len(m.conn.Networks); i++ {
net := m.conn.Networks[i]
for i := 0; i < len(net.Channels); i++ {
channels = append(channels, &net.Channels[i])
}
}
index := m.channelsModel.index
_, index, button := updateIndex(msg, m.channelsModel.index, len(channels), true, []int{})
m.channelsModel.index = index
if button && index < len(channels) {
cmd = doOpenChannel(m.conn, channels[index])
m.view = viewChat
m.chatModel.chatInput.SetValue("")
}
return m, cmd
}