mirror of
https://github.com/Rudi9719/kbtui.git
synced 2026-03-22 08:47:24 +00:00
Globalization
This commit is contained in:
@ -28,5 +28,5 @@ func cmdHelp(g *gocui.Gui, cmd []string) {
|
|||||||
helpText = fmt.Sprintf("%s%s%s\t\t%s\n", helpText, cmdPrefix, c, commands[c].Description)
|
helpText = fmt.Sprintf("%s%s%s\t\t%s\n", helpText, cmdPrefix, c, commands[c].Description)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printToView(g, "Chat", helpText)
|
printToView("Chat", helpText)
|
||||||
}
|
}
|
||||||
|
|||||||
16
cmdJoin.go
16
cmdJoin.go
@ -26,18 +26,18 @@ func cmdJoin(g *gocui.Gui, cmd []string) {
|
|||||||
channel.MembersType = keybase.TEAM
|
channel.MembersType = keybase.TEAM
|
||||||
channel.Name = cmd[1]
|
channel.Name = cmd[1]
|
||||||
channel.TopicName = cmd[2]
|
channel.TopicName = cmd[2]
|
||||||
printToView(g, "Feed", fmt.Sprintf("You are joining: @%s#%s", channel.Name, channel.TopicName))
|
printToView("Feed", fmt.Sprintf("You are joining: @%s#%s", channel.Name, channel.TopicName))
|
||||||
clearView(g, "Chat")
|
clearView("Chat")
|
||||||
go populateChat(g)
|
go populateChat()
|
||||||
} else if len(cmd) == 2 {
|
} else if len(cmd) == 2 {
|
||||||
channel.MembersType = keybase.USER
|
channel.MembersType = keybase.USER
|
||||||
channel.Name = cmd[1]
|
channel.Name = cmd[1]
|
||||||
channel.TopicName = ""
|
channel.TopicName = ""
|
||||||
printToView(g, "Feed", fmt.Sprintf("You are joining: @%s", channel.Name))
|
printToView("Feed", fmt.Sprintf("You are joining: @%s", channel.Name))
|
||||||
clearView(g, "Chat")
|
clearView("Chat")
|
||||||
go populateChat(g)
|
go populateChat()
|
||||||
} else {
|
} else {
|
||||||
printToView(g, "Feed", fmt.Sprintf("To join a team use %sjoin <team> <channel>", cmdPrefix))
|
printToView("Feed", fmt.Sprintf("To join a team use %sjoin <team> <channel>", cmdPrefix))
|
||||||
printToView(g, "Feed", fmt.Sprintf("To join a PM use %sjoin <user>", cmdPrefix))
|
printToView("Feed", fmt.Sprintf("To join a PM use %sjoin <user>", cmdPrefix))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,8 +30,8 @@ func cmdUploadFile(g *gocui.Gui, cmd []string) {
|
|||||||
chat := k.NewChat(channel)
|
chat := k.NewChat(channel)
|
||||||
_, err := chat.Upload(fileName, filePath)
|
_, err := chat.Upload(fileName, filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printToView(g, "Feed", fmt.Sprintf("There was an error uploading %s to %s", filePath, channel.Name))
|
printToView("Feed", fmt.Sprintf("There was an error uploading %s to %s", filePath, channel.Name))
|
||||||
} else {
|
} else {
|
||||||
printToView(g, "Feed", fmt.Sprintf("Uploaded %s to %s", filePath, channel.Name))
|
printToView("Feed", fmt.Sprintf("Uploaded %s to %s", filePath, channel.Name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
91
main.go
91
main.go
@ -33,6 +33,7 @@ var channel keybase.Channel
|
|||||||
var channels []keybase.Channel
|
var channels []keybase.Channel
|
||||||
var stream = false
|
var stream = false
|
||||||
var lastMessage keybase.ChatAPI
|
var lastMessage keybase.ChatAPI
|
||||||
|
var g *gocui.Gui
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if !k.LoggedIn {
|
if !k.LoggedIn {
|
||||||
@ -46,17 +47,17 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer kbtui.Close()
|
defer kbtui.Close()
|
||||||
kbtui.SetManagerFunc(layout)
|
kbtui.SetManagerFunc(layout)
|
||||||
|
g = kbtui
|
||||||
go populateList(kbtui)
|
go populateList()
|
||||||
go updateChatWindow(kbtui)
|
go updateChatWindow()
|
||||||
if err := initKeybindings(kbtui); err != nil {
|
if err := initKeybindings(); err != nil {
|
||||||
log.Printf("%+v", err)
|
log.Printf("%+v", err)
|
||||||
}
|
}
|
||||||
if err := kbtui.MainLoop(); err != nil && err != gocui.ErrQuit {
|
if err := kbtui.MainLoop(); err != nil && err != gocui.ErrQuit {
|
||||||
log.Printf("%+v", err)
|
log.Printf("%+v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func populateChat(g *gocui.Gui) {
|
func populateChat() {
|
||||||
lastMessage.ID = 0
|
lastMessage.ID = 0
|
||||||
chat := k.NewChat(channel)
|
chat := k.NewChat(channel)
|
||||||
maxX, _ := g.Size()
|
maxX, _ := g.Size()
|
||||||
@ -71,10 +72,10 @@ func populateChat(g *gocui.Gui) {
|
|||||||
chat = k.NewChat(channel)
|
chat = k.NewChat(channel)
|
||||||
_, err2 := chat.Read(2)
|
_, err2 := chat.Read(2)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
printToView(g, "Feed", fmt.Sprintf("%+v", err))
|
printToView("Feed", fmt.Sprintf("%+v", err))
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
go populateChat(g)
|
go populateChat()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -98,15 +99,15 @@ func populateChat(g *gocui.Gui) {
|
|||||||
actuallyPrintMe += "\n"
|
actuallyPrintMe += "\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printToView(g, "Chat", actuallyPrintMe)
|
printToView("Chat", actuallyPrintMe)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendChat(message string, g *gocui.Gui) {
|
func sendChat(message string) {
|
||||||
chat := k.NewChat(channel)
|
chat := k.NewChat(channel)
|
||||||
_, err := chat.Send(message)
|
_, err := chat.Send(message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printToView(g, "Feed", fmt.Sprintf("There was an error %+v", err))
|
printToView("Feed", fmt.Sprintf("There was an error %+v", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func formatOutput(api keybase.ChatAPI) string {
|
func formatOutput(api keybase.ChatAPI) string {
|
||||||
@ -124,23 +125,23 @@ func formatOutput(api keybase.ChatAPI) string {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadFile(g *gocui.Gui, messageID int, fileName string) {
|
func downloadFile(messageID int, fileName string) {
|
||||||
chat := k.NewChat(channel)
|
chat := k.NewChat(channel)
|
||||||
_, err := chat.Download(messageID, fmt.Sprintf("%s/%s", downloadPath, fileName))
|
_, err := chat.Download(messageID, fmt.Sprintf("%s/%s", downloadPath, fileName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printToView(g, "Feed", fmt.Sprintf("There was an error downloading %s from %s", fileName, channel.Name))
|
printToView("Feed", fmt.Sprintf("There was an error downloading %s from %s", fileName, channel.Name))
|
||||||
} else {
|
} else {
|
||||||
printToView(g, "Feed", fmt.Sprintf("Downloaded %s from %s", fileName, channel.Name))
|
printToView("Feed", fmt.Sprintf("Downloaded %s from %s", fileName, channel.Name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func populateList(g *gocui.Gui) {
|
func populateList() {
|
||||||
_, maxY := g.Size()
|
_, maxY := g.Size()
|
||||||
if testVar, err := k.ChatList(); err != nil {
|
if testVar, err := k.ChatList(); err != nil {
|
||||||
log.Printf("%+v", err)
|
log.Printf("%+v", err)
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
clearView(g, "List")
|
clearView("List")
|
||||||
var recentPMs = "---[PMs]---\n"
|
var recentPMs = "---[PMs]---\n"
|
||||||
var recentPMsCount = 0
|
var recentPMsCount = 0
|
||||||
var recentChannels = "---[Teams]---\n"
|
var recentChannels = "---[Teams]---\n"
|
||||||
@ -166,13 +167,13 @@ func populateList(g *gocui.Gui) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
time.Sleep(1 * time.Millisecond)
|
time.Sleep(1 * time.Millisecond)
|
||||||
printToView(g, "List", fmt.Sprintf("%s%s", recentPMs, recentChannels))
|
printToView("List", fmt.Sprintf("%s%s", recentPMs, recentChannels))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func clearView(kbtui *gocui.Gui, viewName string) {
|
func clearView(viewName string) {
|
||||||
kbtui.Update(func(g *gocui.Gui) error {
|
g.Update(func(g *gocui.Gui) error {
|
||||||
inputView, err := kbtui.View(viewName)
|
inputView, err := g.View(viewName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
@ -185,9 +186,9 @@ func clearView(kbtui *gocui.Gui, viewName string) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func printToView(kbtui *gocui.Gui, viewName string, message string) {
|
func printToView(viewName string, message string) {
|
||||||
kbtui.Update(func(g *gocui.Gui) error {
|
g.Update(func(g *gocui.Gui) error {
|
||||||
updatingView, err := kbtui.View(viewName)
|
updatingView, err := g.View(viewName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
@ -214,7 +215,7 @@ func layout(g *gocui.Gui) error {
|
|||||||
chatView.Autoscroll = true
|
chatView.Autoscroll = true
|
||||||
chatView.Wrap = true
|
chatView.Wrap = true
|
||||||
fmt.Fprintf(chatView, "Welcome %s!\n\nYour chats will appear here.\nSupported commands are as follows:\n\n", k.Username)
|
fmt.Fprintf(chatView, "Welcome %s!\n\nYour chats will appear here.\nSupported commands are as follows:\n\n", k.Username)
|
||||||
RunCommand(g, "help")
|
RunCommand("help")
|
||||||
}
|
}
|
||||||
if inputView, err3 := g.SetView("Input", maxX/2-maxX/3, maxY-4, maxX-1, maxY-1); err3 != nil {
|
if inputView, err3 := g.SetView("Input", maxX/2-maxX/3, maxY-4, maxX-1, maxY-1); err3 != nil {
|
||||||
if err3 != gocui.ErrUnknownView {
|
if err3 != gocui.ErrUnknownView {
|
||||||
@ -236,20 +237,20 @@ func layout(g *gocui.Gui) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getInputString(g *gocui.Gui) (string, error) {
|
func getInputString() (string, error) {
|
||||||
inputView, _ := g.View("Input")
|
inputView, _ := g.View("Input")
|
||||||
return inputView.Line(0)
|
return inputView.Line(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initKeybindings(g *gocui.Gui) error {
|
func initKeybindings() error {
|
||||||
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone,
|
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone,
|
||||||
func(g *gocui.Gui, v *gocui.View) error {
|
func(g *gocui.Gui, v *gocui.View) error {
|
||||||
input, err := getInputString(g)
|
input, err := getInputString()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if input != "" {
|
if input != "" {
|
||||||
clearView(g, "Input")
|
clearView("Input")
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
return gocui.ErrQuit
|
return gocui.ErrQuit
|
||||||
@ -259,16 +260,16 @@ func initKeybindings(g *gocui.Gui) error {
|
|||||||
}
|
}
|
||||||
if err := g.SetKeybinding("Input", gocui.KeyEnter, gocui.ModNone,
|
if err := g.SetKeybinding("Input", gocui.KeyEnter, gocui.ModNone,
|
||||||
func(g *gocui.Gui, v *gocui.View) error {
|
func(g *gocui.Gui, v *gocui.View) error {
|
||||||
return handleInput(g)
|
return handleInput()
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateChatWindow(g *gocui.Gui) {
|
func updateChatWindow() {
|
||||||
k.Run(func(api keybase.ChatAPI) {
|
k.Run(func(api keybase.ChatAPI) {
|
||||||
handleMessage(api, g)
|
handleMessage(api)
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -278,9 +279,9 @@ func cleanChannelName(c string) string {
|
|||||||
return strings.Replace(newChannelName, fmt.Sprintf(",%s", k.Username), "", 1)
|
return strings.Replace(newChannelName, fmt.Sprintf(",%s", k.Username), "", 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleMessage(api keybase.ChatAPI, g *gocui.Gui) {
|
func handleMessage(api keybase.ChatAPI) {
|
||||||
if api.Msg.Content.Type == "text" {
|
if api.Msg.Content.Type == "text" {
|
||||||
go populateList(g)
|
go populateList()
|
||||||
msgBody := api.Msg.Content.Text.Body
|
msgBody := api.Msg.Content.Text.Body
|
||||||
msgSender := api.Msg.Sender.Username
|
msgSender := api.Msg.Sender.Username
|
||||||
channelName := api.Msg.Channel.Name
|
channelName := api.Msg.Channel.Name
|
||||||
@ -292,7 +293,7 @@ func handleMessage(api keybase.ChatAPI, g *gocui.Gui) {
|
|||||||
if m.Text == k.Username {
|
if m.Text == k.Username {
|
||||||
// We are in a team
|
// We are in a team
|
||||||
if topicName != channel.TopicName {
|
if topicName != channel.TopicName {
|
||||||
printToView(g, "Feed", fmt.Sprintf("[ %s#%s ] %s: %s", channelName, topicName, msgSender, msgBody))
|
printToView("Feed", fmt.Sprintf("[ %s#%s ] %s: %s", channelName, topicName, msgSender, msgBody))
|
||||||
}
|
}
|
||||||
|
|
||||||
break
|
break
|
||||||
@ -300,7 +301,7 @@ func handleMessage(api keybase.ChatAPI, g *gocui.Gui) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if msgSender != channel.Name {
|
if msgSender != channel.Name {
|
||||||
printToView(g, "Feed", fmt.Sprintf("PM from @%s: %s", cleanChannelName(channelName), msgBody))
|
printToView("Feed", fmt.Sprintf("PM from @%s: %s", cleanChannelName(channelName), msgBody))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -310,7 +311,7 @@ func handleMessage(api keybase.ChatAPI, g *gocui.Gui) {
|
|||||||
// Do nothing, wrong channel
|
// Do nothing, wrong channel
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
printToView(g, "Chat", formatOutput(api))
|
printToView("Chat", formatOutput(api))
|
||||||
chat := k.NewChat(channel)
|
chat := k.NewChat(channel)
|
||||||
lastMessage.ID = api.Msg.ID
|
lastMessage.ID = api.Msg.ID
|
||||||
chat.Read(api.Msg.ID)
|
chat.Read(api.Msg.ID)
|
||||||
@ -320,15 +321,15 @@ func handleMessage(api keybase.ChatAPI, g *gocui.Gui) {
|
|||||||
} else {
|
} else {
|
||||||
if api.Msg.Channel.MembersType == keybase.TEAM {
|
if api.Msg.Channel.MembersType == keybase.TEAM {
|
||||||
topicName := api.Msg.Channel.TopicName
|
topicName := api.Msg.Channel.TopicName
|
||||||
printToView(g, "Chat", fmt.Sprintf("@%s#%s [%s]: %s", channelName, topicName, msgSender, msgBody))
|
printToView("Chat", fmt.Sprintf("@%s#%s [%s]: %s", channelName, topicName, msgSender, msgBody))
|
||||||
} else {
|
} else {
|
||||||
printToView(g, "Chat", fmt.Sprintf("PM @%s [%s]: %s", cleanChannelName(channelName), msgSender, msgBody))
|
printToView("Chat", fmt.Sprintf("PM @%s [%s]: %s", cleanChannelName(channelName), msgSender, msgBody))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//TODO: For edit/delete run this
|
//TODO: For edit/delete run this
|
||||||
if api.Msg.Channel.MembersType == channel.MembersType && cleanChannelName(api.Msg.Channel.Name) == channel.Name {
|
if api.Msg.Channel.MembersType == channel.MembersType && cleanChannelName(api.Msg.Channel.Name) == channel.Name {
|
||||||
go populateChat(g)
|
go populateChat()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -342,9 +343,9 @@ func reactToMessageId(messageId string, reaction string) {
|
|||||||
ID, _ := strconv.Atoi(messageId)
|
ID, _ := strconv.Atoi(messageId)
|
||||||
chat.React(ID, reaction)
|
chat.React(ID, reaction)
|
||||||
}
|
}
|
||||||
func handleInput(g *gocui.Gui) error {
|
func handleInput() error {
|
||||||
clearView(g, "Input")
|
clearView("Input")
|
||||||
inputString, _ := getInputString(g)
|
inputString, _ := getInputString()
|
||||||
if inputString == "" {
|
if inputString == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -356,16 +357,16 @@ func handleInput(g *gocui.Gui) error {
|
|||||||
} else if cmd[0] == "q" || cmd[0] == "quit" {
|
} else if cmd[0] == "q" || cmd[0] == "quit" {
|
||||||
return gocui.ErrQuit
|
return gocui.ErrQuit
|
||||||
} else {
|
} else {
|
||||||
printToView(g, "Feed", fmt.Sprintf("Command '%s' not recognized", cmd[0]))
|
printToView("Feed", fmt.Sprintf("Command '%s' not recognized", cmd[0]))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if inputString[:1] == "+" {
|
if inputString[:1] == "+" {
|
||||||
reactToMessage(strings.Replace(inputString, "+", "", 1))
|
reactToMessage(strings.Replace(inputString, "+", "", 1))
|
||||||
} else {
|
} else {
|
||||||
go sendChat(inputString, g)
|
go sendChat(inputString)
|
||||||
}
|
}
|
||||||
go populateList(g)
|
go populateList()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -393,6 +394,6 @@ func RegisterCommand(c Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RunCommand calls a command as if it was run by the user
|
// RunCommand calls a command as if it was run by the user
|
||||||
func RunCommand(g *gocui.Gui, c ...string) {
|
func RunCommand(c ...string) {
|
||||||
commands[c[0]].Exec(g, c)
|
commands[c[0]].Exec(g, c)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user