mirror of
https://github.com/Rudi9719/kbtui.git
synced 2026-03-22 11:07:22 +00:00
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
|||||||
kbtui
|
kbtui
|
||||||
|
emojiList.go
|
||||||
*~
|
*~
|
||||||
.\#*
|
.\#*
|
||||||
\#*\#
|
\#*\#
|
||||||
|
|||||||
13
.travis.yml
Normal file
13
.travis.yml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
language: go
|
||||||
|
|
||||||
|
go:
|
||||||
|
- tip
|
||||||
|
- 1.13.x
|
||||||
|
|
||||||
|
install: true
|
||||||
|
|
||||||
|
script:
|
||||||
|
- go get -u github.com/magefile/mage/mage
|
||||||
|
- go run build.go buildBeta
|
||||||
|
- go vet ./...
|
||||||
|
- go fmt ./...
|
||||||
@ -31,16 +31,16 @@ go get -u github.com/rudi9719/kbtui
|
|||||||
```
|
```
|
||||||
Or you can do the following:
|
Or you can do the following:
|
||||||
```
|
```
|
||||||
go get ./
|
go get ./...
|
||||||
go run build.go
|
go run build.go
|
||||||
go run build.go {build, buildBeta... etc}
|
go run build.go {build, buildBeta... etc}
|
||||||
./kbtui
|
./kbtui
|
||||||
```
|
```
|
||||||
|
|
||||||
You may see an error with `go get ./` about PATHs, that may be safely ignored.
|
You may see an error with `go get ./...` about PATHs, that may be safely ignored.
|
||||||
|
|
||||||
If you see an error about a missing dependancy during a build, you'll want to resolve that.
|
If you see an error about a missing dependancy during a build, you'll want to resolve that.
|
||||||
|
|
||||||
|
|
||||||
Occasionally when @dxb updates his API it will be necessary to run
|
Occasionally when [@dxb](https://keybase.io/dxb) updates his API it will be necessary to run
|
||||||
`go get -u ./`
|
`go get -u ./...` or `go get -u samhofi.us/x/keybase`
|
||||||
|
|||||||
@ -19,16 +19,34 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func cmdDownloadFile(cmd []string) {
|
func cmdDownloadFile(cmd []string) {
|
||||||
messageID, _ := strconv.Atoi(cmd[1])
|
|
||||||
|
if len(cmd) < 2 {
|
||||||
|
printToView("Feed", fmt.Sprintf("%s%s $messageId $fileName - Download a file to user's downloadpath", cmdPrefix, cmd[0]))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
messageID, err := strconv.Atoi(cmd[1])
|
||||||
|
if err != nil {
|
||||||
|
printToView("Feed", "There was an error converting your messageID to an int")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
chat := k.NewChat(channel)
|
||||||
|
api, err := chat.ReadMessage(messageID)
|
||||||
|
if err != nil {
|
||||||
|
printToView("Feed", fmt.Sprintf("There was an error pulling message %d", messageID))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if api.Result.Messages[0].Msg.Content.Type != "attachment" {
|
||||||
|
printToView("Feed", "No attachment detected")
|
||||||
|
return
|
||||||
|
}
|
||||||
var fileName string
|
var fileName string
|
||||||
if len(cmd) == 3 {
|
if len(cmd) == 3 {
|
||||||
fileName = cmd[2]
|
fileName = cmd[2]
|
||||||
} else {
|
} else {
|
||||||
fileName = ""
|
fileName = api.Result.Messages[0].Msg.Content.Attachment.Object.Filename
|
||||||
}
|
}
|
||||||
|
|
||||||
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("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 {
|
||||||
|
|||||||
31
cmdEdit.go
31
cmdEdit.go
@ -11,7 +11,7 @@ import (
|
|||||||
func init() {
|
func init() {
|
||||||
command := Command{
|
command := Command{
|
||||||
Cmd: []string{"edit", "e"},
|
Cmd: []string{"edit", "e"},
|
||||||
Description: "$messageId - Edit a message (messageID is optional)",
|
Description: "$messageID - Edit a message (messageID is optional)",
|
||||||
Help: "",
|
Help: "",
|
||||||
Exec: cmdEdit,
|
Exec: cmdEdit,
|
||||||
}
|
}
|
||||||
@ -20,16 +20,22 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func cmdEdit(cmd []string) {
|
func cmdEdit(cmd []string) {
|
||||||
var messageId int
|
var messageID int
|
||||||
chat := k.NewChat(channel)
|
chat := k.NewChat(channel)
|
||||||
if len(cmd) == 2 || len(cmd) == 1 {
|
if len(cmd) == 2 || len(cmd) == 1 {
|
||||||
if len(cmd) == 2 {
|
if len(cmd) == 2 {
|
||||||
messageId, _ = strconv.Atoi(cmd[1])
|
messageID, _ = strconv.Atoi(cmd[1])
|
||||||
} else {
|
} else if lastMessage.ID != 0 {
|
||||||
messageId = lastMessage.ID
|
if lastMessage.Type != "text" {
|
||||||
|
printToView("Feed", "Last message isn't editable (is it an edit?)")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
messageID = lastMessage.ID
|
||||||
origMessage, _ := chat.ReadMessage(messageId)
|
} else {
|
||||||
|
printToView("Feed", "No message to edit")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
origMessage, _ := chat.ReadMessage(messageID)
|
||||||
if origMessage.Result.Messages[0].Msg.Content.Type != "text" {
|
if origMessage.Result.Messages[0].Msg.Content.Type != "text" {
|
||||||
printToView("Feed", fmt.Sprintf("%+v", origMessage))
|
printToView("Feed", fmt.Sprintf("%+v", origMessage))
|
||||||
return
|
return
|
||||||
@ -41,19 +47,20 @@ func cmdEdit(cmd []string) {
|
|||||||
editString := origMessage.Result.Messages[0].Msg.Content.Text.Body
|
editString := origMessage.Result.Messages[0].Msg.Content.Text.Body
|
||||||
clearView("Edit")
|
clearView("Edit")
|
||||||
popupView("Edit")
|
popupView("Edit")
|
||||||
printToView("Edit", fmt.Sprintf("/e %d %s", messageId, editString))
|
printToView("Edit", fmt.Sprintf("/e %d %s", messageID, editString))
|
||||||
viewTitle("Edit", fmt.Sprintf(" Editing message %d ", messageId))
|
setViewTitle("Edit", fmt.Sprintf(" Editing message %d ", messageID))
|
||||||
|
moveCursorToEnd("Edit")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(cmd) < 3 {
|
if len(cmd) < 3 {
|
||||||
printToView("Feed", "Not enough options for Edit")
|
printToView("Feed", "Not enough options for Edit")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
messageId, _ = strconv.Atoi(cmd[1])
|
messageID, _ = strconv.Atoi(cmd[1])
|
||||||
newMessage := strings.Join(cmd[2:], " ")
|
newMessage := strings.Join(cmd[2:], " ")
|
||||||
_, err := chat.Edit(messageId, newMessage)
|
_, err := chat.Edit(messageID, newMessage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printToView("Feed", fmt.Sprintf("Error editing message %d, %+v", messageId, err))
|
printToView("Feed", fmt.Sprintf("Error editing message %d, %+v", messageID, err))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,7 +28,7 @@ func cmdHelp(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)
|
||||||
}
|
}
|
||||||
if len(typeCommands) > 0 {
|
if len(typeCommands) > 0 {
|
||||||
for c, _ := range typeCommands {
|
for c := range typeCommands {
|
||||||
tCommands = append(tCommands, typeCommands[c].Name)
|
tCommands = append(tCommands, typeCommands[c].Name)
|
||||||
}
|
}
|
||||||
sort.Strings(tCommands)
|
sort.Strings(tCommands)
|
||||||
|
|||||||
@ -43,7 +43,7 @@ func cmdJoin(cmd []string) {
|
|||||||
}
|
}
|
||||||
printToView("Feed", fmt.Sprintf("You are joining: %s", joinedName))
|
printToView("Feed", fmt.Sprintf("You are joining: %s", joinedName))
|
||||||
clearView("Chat")
|
clearView("Chat")
|
||||||
viewTitle("Input", fmt.Sprintf(" %s ", joinedName))
|
setViewTitle("Input", fmt.Sprintf(" %s ", joinedName))
|
||||||
go populateChat()
|
go populateChat()
|
||||||
default:
|
default:
|
||||||
printToView("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))
|
||||||
|
|||||||
12
cmdReact.go
12
cmdReact.go
@ -10,7 +10,7 @@ import (
|
|||||||
func init() {
|
func init() {
|
||||||
command := Command{
|
command := Command{
|
||||||
Cmd: []string{"react", "r", "+"},
|
Cmd: []string{"react", "r", "+"},
|
||||||
Description: "$messageId $reaction - React to a message (messageID is optional)",
|
Description: "$messageID $reaction - React to a message (messageID is optional)",
|
||||||
Help: "",
|
Help: "",
|
||||||
Exec: cmdReact,
|
Exec: cmdReact,
|
||||||
}
|
}
|
||||||
@ -20,7 +20,7 @@ func init() {
|
|||||||
|
|
||||||
func cmdReact(cmd []string) {
|
func cmdReact(cmd []string) {
|
||||||
if len(cmd) > 2 {
|
if len(cmd) > 2 {
|
||||||
reactToMessageId(cmd[1], strings.Join(cmd[2:], " "))
|
reactToMessageID(cmd[1], strings.Join(cmd[2:], " "))
|
||||||
} else if len(cmd) == 2 {
|
} else if len(cmd) == 2 {
|
||||||
reactToMessage(cmd[1])
|
reactToMessage(cmd[1])
|
||||||
}
|
}
|
||||||
@ -30,13 +30,13 @@ func cmdReact(cmd []string) {
|
|||||||
func reactToMessage(reaction string) {
|
func reactToMessage(reaction string) {
|
||||||
doReact(lastMessage.ID, reaction)
|
doReact(lastMessage.ID, reaction)
|
||||||
}
|
}
|
||||||
func reactToMessageId(messageId string, reaction string) {
|
func reactToMessageID(messageID string, reaction string) {
|
||||||
ID, _ := strconv.Atoi(messageId)
|
ID, _ := strconv.Atoi(messageID)
|
||||||
doReact(ID, reaction)
|
doReact(ID, reaction)
|
||||||
}
|
}
|
||||||
func doReact(messageId int, reaction string) {
|
func doReact(messageID int, reaction string) {
|
||||||
chat := k.NewChat(channel)
|
chat := k.NewChat(channel)
|
||||||
_, err := chat.React(messageId, reaction)
|
_, err := chat.React(messageID, reaction)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printToView("Feed", "There was an error reacting to the message.")
|
printToView("Feed", "There was an error reacting to the message.")
|
||||||
}
|
}
|
||||||
|
|||||||
14
cmdReply.go
14
cmdReply.go
@ -3,6 +3,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -20,10 +21,19 @@ func init() {
|
|||||||
|
|
||||||
func cmdReply(cmd []string) {
|
func cmdReply(cmd []string) {
|
||||||
chat := k.NewChat(channel)
|
chat := k.NewChat(channel)
|
||||||
messageId, err := strconv.Atoi(cmd[1])
|
if len(cmd) < 2 {
|
||||||
_, err = chat.Reply(messageId, strings.Join(cmd[2:], " "))
|
printToView("Feed", fmt.Sprintf("%s%s $ID - Reply to message $ID", cmdPrefix, cmd[0]))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
messageID, err := strconv.Atoi(cmd[1])
|
||||||
|
if err != nil {
|
||||||
|
printToView("Feed", fmt.Sprintf("There was an error determining message ID %s", cmd[1]))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = chat.Reply(messageID, strings.Join(cmd[2:], " "))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printToView("Feed", "There was an error with your reply.")
|
printToView("Feed", "There was an error with your reply.")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
46
cmdSet.go
46
cmdSet.go
@ -5,6 +5,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pelletier/go-toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -17,16 +19,11 @@ func init() {
|
|||||||
|
|
||||||
RegisterCommand(command)
|
RegisterCommand(command)
|
||||||
}
|
}
|
||||||
|
func printSetting(cmd []string) {
|
||||||
func cmdSet(cmd []string) {
|
|
||||||
if len(cmd) < 2 {
|
|
||||||
printToView("Feed", "No config value specified")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(cmd) < 3 {
|
|
||||||
switch cmd[1] {
|
switch cmd[1] {
|
||||||
case "load":
|
case "load":
|
||||||
printToView("Feed", "Load values from file?")
|
loadFromToml()
|
||||||
|
printToView("Feed", fmt.Sprintf("Loading config from toml"))
|
||||||
case "downloadPath":
|
case "downloadPath":
|
||||||
printToView("Feed", fmt.Sprintf("Setting for %s -> %s", cmd[1], downloadPath))
|
printToView("Feed", fmt.Sprintf("Setting for %s -> %s", cmd[1], downloadPath))
|
||||||
case "outputFormat":
|
case "outputFormat":
|
||||||
@ -43,6 +40,14 @@ func cmdSet(cmd []string) {
|
|||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
func cmdSet(cmd []string) {
|
||||||
|
if len(cmd) < 2 {
|
||||||
|
printToView("Feed", "No config value specified")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(cmd) < 3 {
|
||||||
|
printSetting(cmd)
|
||||||
|
}
|
||||||
switch cmd[1] {
|
switch cmd[1] {
|
||||||
case "downloadPath":
|
case "downloadPath":
|
||||||
if len(cmd) != 3 {
|
if len(cmd) != 3 {
|
||||||
@ -62,3 +67,28 @@ func cmdSet(cmd []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
func loadFromToml() {
|
||||||
|
config, err := toml.LoadFile("kbtui.tml")
|
||||||
|
if err != nil {
|
||||||
|
printToView("Feed", fmt.Sprintf("Could not read config file: %+v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if config.Has("Basics.colorless") {
|
||||||
|
colorless = config.Get("Basics.colorless").(bool)
|
||||||
|
}
|
||||||
|
if config.Has("Basics.downloadPath") {
|
||||||
|
downloadPath = config.Get("Basics.downloadPath").(string)
|
||||||
|
}
|
||||||
|
if config.Has("Basics.cmdPrefix") {
|
||||||
|
cmdPrefix = config.Get("Basics.cmdPrefix").(string)
|
||||||
|
}
|
||||||
|
if config.Has("Formatting.outputFormat") {
|
||||||
|
outputFormat = config.Get("Formatting.outputFormat").(string)
|
||||||
|
}
|
||||||
|
if config.Has("Formatting.dateFormat") {
|
||||||
|
dateFormat = config.Get("Formatting.dateFormat").(string)
|
||||||
|
}
|
||||||
|
if config.Has("Formatting.timeFormat") {
|
||||||
|
timeFormat = config.Get("Formatting.timeFormat").(string)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -18,6 +18,6 @@ func cmdStream(cmd []string) {
|
|||||||
channel.Name = ""
|
channel.Name = ""
|
||||||
|
|
||||||
printToView("Feed", "You are now viewing the formatted stream")
|
printToView("Feed", "You are now viewing the formatted stream")
|
||||||
viewTitle("Input", " Stream - Not in a chat /j to join ")
|
setViewTitle("Input", " Stream - Not in a chat /j to join ")
|
||||||
clearView("Chat")
|
clearView("Chat")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,12 +4,14 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
command := Command{
|
command := Command{
|
||||||
Cmd: []string{"upload", "u"},
|
Cmd: []string{"upload", "u"},
|
||||||
Description: "$filePath $fileName - Upload file with optional name",
|
Description: "$filePath $fileName - Upload file from absolute path with optional name",
|
||||||
Help: "",
|
Help: "",
|
||||||
Exec: cmdUploadFile,
|
Exec: cmdUploadFile,
|
||||||
}
|
}
|
||||||
@ -18,7 +20,18 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func cmdUploadFile(cmd []string) {
|
func cmdUploadFile(cmd []string) {
|
||||||
|
if len(cmd) < 2 {
|
||||||
|
printToView("Feed", fmt.Sprintf("%s%s $filePath $fileName - Upload file from absolute path with optional name", cmdPrefix, cmd[0]))
|
||||||
|
return
|
||||||
|
}
|
||||||
filePath := cmd[1]
|
filePath := cmd[1]
|
||||||
|
if !strings.HasPrefix(filePath, "/") {
|
||||||
|
dir, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
printToView("Feed", fmt.Sprintf("There was an error determining path %+v", err))
|
||||||
|
}
|
||||||
|
filePath = fmt.Sprintf("%s/%s", dir, filePath)
|
||||||
|
}
|
||||||
var fileName string
|
var fileName string
|
||||||
if len(cmd) == 3 {
|
if len(cmd) == 3 {
|
||||||
fileName = cmd[2]
|
fileName = cmd[2]
|
||||||
@ -28,7 +41,7 @@ func cmdUploadFile(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("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\n%+v", filePath, channel.Name, err))
|
||||||
} else {
|
} else {
|
||||||
printToView("Feed", fmt.Sprintf("Uploaded %s to %s", filePath, channel.Name))
|
printToView("Feed", fmt.Sprintf("Uploaded %s to %s", filePath, channel.Name))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,7 +15,7 @@ import (
|
|||||||
func init() {
|
func init() {
|
||||||
command := Command{
|
command := Command{
|
||||||
Cmd: []string{"wall", "w"},
|
Cmd: []string{"wall", "w"},
|
||||||
Description: "- Show public messages for a user",
|
Description: "$user / !all - Show public messages for a user or all users you follow",
|
||||||
Help: "",
|
Help: "",
|
||||||
Exec: cmdWall,
|
Exec: cmdWall,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -34,7 +34,7 @@ func cmdWallet(cmd []string) {
|
|||||||
chars := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
chars := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
||||||
"abcdefghijklmnopqrstuvwxyz" +
|
"abcdefghijklmnopqrstuvwxyz" +
|
||||||
"0123456789")
|
"0123456789")
|
||||||
length := 5
|
length := 8
|
||||||
var b strings.Builder
|
var b strings.Builder
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
b.WriteRune(chars[rand.Intn(len(chars))])
|
b.WriteRune(chars[rand.Intn(len(chars))])
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
30
kbtui.tml
Normal file
30
kbtui.tml
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
[Basics]
|
||||||
|
downloadPath = "/tmp/"
|
||||||
|
colorless = false
|
||||||
|
# The prefix before evaluating a command
|
||||||
|
cmdPrefix = "/"
|
||||||
|
|
||||||
|
[Formatting]
|
||||||
|
# BASH-like PS1 variable equivalent
|
||||||
|
outputFormat = "┌──[$USER@$DEVICE] [$ID] [$DATE - $TIME]\n└╼ $MSG"
|
||||||
|
|
||||||
|
# 02 = Day, Jan = Month, 06 = Year
|
||||||
|
dateFormat = "02Jan06"
|
||||||
|
|
||||||
|
# 15 = hours, 04 = minutes, 05 = seconds
|
||||||
|
timeFormat = "15:04"
|
||||||
|
|
||||||
|
|
||||||
|
[Colors]
|
||||||
|
channelsColor = 8
|
||||||
|
channelsHeaderColor = 6
|
||||||
|
noColor = -1
|
||||||
|
mentionColor = 3
|
||||||
|
messageHeaderColor = 8
|
||||||
|
messageIdColor = 7
|
||||||
|
messageTimeColor = 6
|
||||||
|
messageSenderDefaultColor = 8
|
||||||
|
messageSenderDeviceColor = 8
|
||||||
|
messageBodyColor = -1
|
||||||
|
messageAttachmentColor = 2
|
||||||
|
messageLinkColor = 4
|
||||||
73
mage.go
73
mage.go
@ -5,12 +5,13 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/magefile/mage/mg"
|
|
||||||
"github.com/magefile/mage/sh"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/magefile/mage/mg"
|
||||||
|
"github.com/magefile/mage/sh"
|
||||||
)
|
)
|
||||||
|
|
||||||
// emoji related constants
|
// emoji related constants
|
||||||
@ -61,8 +62,34 @@ func createEmojiSlice() ([]string, error) {
|
|||||||
return emojiSlice, nil
|
return emojiSlice, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getRemotePackages() error {
|
||||||
|
var packages = []string{
|
||||||
|
"samhofi.us/x/keybase",
|
||||||
|
"github.com/awesome-gocui/gocui",
|
||||||
|
"github.com/magefile/mage/mage",
|
||||||
|
"github.com/magefile/mage/mg",
|
||||||
|
"github.com/magefile/mage/sh",
|
||||||
|
"github.com/pelletier/go-toml",
|
||||||
|
}
|
||||||
|
for _, p := range packages {
|
||||||
|
if err := sh.Run("go", "get", "-u", p); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// proper error reporting and exit code
|
||||||
|
func exit(err error) {
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "%+v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Build kbtui with emoji lookup support
|
// Build kbtui with emoji lookup support
|
||||||
func BuildEmoji() error {
|
func BuildEmoji() error {
|
||||||
|
mg.Deps(getRemotePackages)
|
||||||
emojis, err := createEmojiSlice()
|
emojis, err := createEmojiSlice()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -84,14 +111,24 @@ func BuildEmoji() error {
|
|||||||
|
|
||||||
// Build kbtui with just the basic commands.
|
// Build kbtui with just the basic commands.
|
||||||
func Build() {
|
func Build() {
|
||||||
sh.Run("go", "build")
|
mg.Deps(getRemotePackages)
|
||||||
|
if err := sh.Run("go", "build"); err != nil {
|
||||||
|
defer func() {
|
||||||
|
exit(err)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build kbtui with the basic commands, and the ShowReactions "TypeCommand".
|
// Build kbtui with the basic commands, and the ShowReactions "TypeCommand".
|
||||||
// The ShowReactions TypeCommand will print a message in the feed window when
|
// The ShowReactions TypeCommand will print a message in the feed window when
|
||||||
// a reaction is received in the current conversation.
|
// a reaction is received in the current conversation.
|
||||||
func BuildShowReactions() {
|
func BuildShowReactions() {
|
||||||
sh.Run("go", "build", "-tags", "showreactionscmd")
|
mg.Deps(getRemotePackages)
|
||||||
|
if err := sh.Run("go", "build", "-tags", "showreactionscmd"); err != nil {
|
||||||
|
defer func() {
|
||||||
|
exit(err)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build kbtui with the basec commands, and the AutoReact "TypeCommand".
|
// Build kbtui with the basec commands, and the AutoReact "TypeCommand".
|
||||||
@ -99,21 +136,41 @@ func BuildShowReactions() {
|
|||||||
// received in the current conversation. This gets pretty annoying, and
|
// received in the current conversation. This gets pretty annoying, and
|
||||||
// is not recommended.
|
// is not recommended.
|
||||||
func BuildAutoReact() {
|
func BuildAutoReact() {
|
||||||
sh.Run("go", "build", "-tags", "autoreactcmd")
|
mg.Deps(getRemotePackages)
|
||||||
|
if err := sh.Run("go", "build", "-tags", "autoreactcmd"); err != nil {
|
||||||
|
defer func() {
|
||||||
|
exit(err)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build kbtui with all commands and TypeCommands disabled.
|
// Build kbtui with all commands and TypeCommands disabled.
|
||||||
func BuildAllCommands() {
|
func BuildAllCommands() {
|
||||||
sh.Run("go", "build", "-tags", "allcommands")
|
mg.Deps(getRemotePackages)
|
||||||
|
if err := sh.Run("go", "build", "-tags", "allcommands"); err != nil {
|
||||||
|
defer func() {
|
||||||
|
exit(err)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build kbtui with all Commands and TypeCommands enabled.
|
// Build kbtui with all Commands and TypeCommands enabled.
|
||||||
func BuildAllCommandsT() {
|
func BuildAllCommandsT() {
|
||||||
sh.Run("go", "build", "-tags", "type_commands,allcommands")
|
mg.Deps(getRemotePackages)
|
||||||
|
if err := sh.Run("go", "build", "-tags", "type_commands,allcommands"); err != nil {
|
||||||
|
defer func() {
|
||||||
|
exit(err)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build kbtui with beta functionality
|
// Build kbtui with beta functionality
|
||||||
func BuildBeta() {
|
func BuildBeta() {
|
||||||
|
mg.Deps(getRemotePackages)
|
||||||
mg.Deps(BuildEmoji)
|
mg.Deps(BuildEmoji)
|
||||||
sh.Run("go", "build", "-tags", "allcommands,showreactionscmd,emojiList")
|
if err := sh.Run("go", "build", "-tags", "allcommands,showreactionscmd,emojiList,tabcompletion"); err != nil {
|
||||||
|
defer func() {
|
||||||
|
exit(err)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
696
main.go
696
main.go
@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -12,17 +11,19 @@ import (
|
|||||||
"samhofi.us/x/keybase"
|
"samhofi.us/x/keybase"
|
||||||
)
|
)
|
||||||
|
|
||||||
var typeCommands = make(map[string]TypeCommand)
|
var (
|
||||||
var commands = make(map[string]Command)
|
typeCommands = make(map[string]TypeCommand)
|
||||||
var baseCommands = make([]string, 0)
|
commands = make(map[string]Command)
|
||||||
|
baseCommands = make([]string, 0)
|
||||||
|
|
||||||
var dev = false
|
dev = false
|
||||||
var k = keybase.NewKeybase()
|
k = keybase.NewKeybase()
|
||||||
var channel keybase.Channel
|
channel keybase.Channel
|
||||||
var channels []keybase.Channel
|
channels []keybase.Channel
|
||||||
var stream = false
|
stream = false
|
||||||
var lastMessage keybase.ChatAPI
|
lastMessage keybase.ChatAPI
|
||||||
var g *gocui.Gui
|
g *gocui.Gui
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if !k.LoggedIn {
|
if !k.LoggedIn {
|
||||||
@ -50,387 +51,10 @@ func main() {
|
|||||||
if err := g.MainLoop(); err != nil && !gocui.IsQuit(err) {
|
if err := g.MainLoop(); err != nil && !gocui.IsQuit(err) {
|
||||||
fmt.Printf("%+v", err)
|
fmt.Printf("%+v", err)
|
||||||
}
|
}
|
||||||
|
go generateChannelTabCompletionSlice()
|
||||||
}
|
}
|
||||||
|
|
||||||
func viewTitle(viewName string, title string) {
|
// Gocui basic setup
|
||||||
g.Update(func(g *gocui.Gui) error {
|
|
||||||
updatingView, err := g.View(viewName)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
updatingView.Title = title
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func getViewTitle(viewName string) string {
|
|
||||||
view, err := g.View(viewName)
|
|
||||||
if err != nil {
|
|
||||||
// in case there is active tab completion, filter that to just the view title and not the completion options.
|
|
||||||
writeToView("Feed", fmt.Sprintf("Error getting view title: %s", err))
|
|
||||||
return ""
|
|
||||||
} else {
|
|
||||||
return strings.Split(view.Title, "||")[0]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func popupView(viewName string) {
|
|
||||||
_, err := g.SetCurrentView(viewName)
|
|
||||||
if err != nil {
|
|
||||||
printToView("Feed", fmt.Sprintf("%+v", err))
|
|
||||||
}
|
|
||||||
_, err = g.SetViewOnTop(viewName)
|
|
||||||
if err != nil {
|
|
||||||
printToView("Feed", fmt.Sprintf("%+v", err))
|
|
||||||
}
|
|
||||||
g.Update(func(g *gocui.Gui) error {
|
|
||||||
updatingView, err := g.View(viewName)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
viewX, viewY := updatingView.Size()
|
|
||||||
updatingView.MoveCursor(viewX, viewY, true)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func populateChat() {
|
|
||||||
lastMessage.ID = 0
|
|
||||||
chat := k.NewChat(channel)
|
|
||||||
maxX, _ := g.Size()
|
|
||||||
api, err := chat.Read(maxX / 2)
|
|
||||||
if err != nil {
|
|
||||||
for _, testChan := range channels {
|
|
||||||
if channel.Name == testChan.Name {
|
|
||||||
channel = testChan
|
|
||||||
channel.TopicName = "general"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
chat = k.NewChat(channel)
|
|
||||||
_, err2 := chat.Read(2)
|
|
||||||
if err2 != nil {
|
|
||||||
printToView("Feed", fmt.Sprintf("%+v", err))
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
go populateChat()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var printMe []string
|
|
||||||
var actuallyPrintMe string
|
|
||||||
if len(api.Result.Messages) > 0 {
|
|
||||||
lastMessage.ID = api.Result.Messages[0].Msg.ID
|
|
||||||
}
|
|
||||||
for _, message := range api.Result.Messages {
|
|
||||||
if message.Msg.Content.Type == "text" || message.Msg.Content.Type == "attachment" {
|
|
||||||
if lastMessage.ID < 1 {
|
|
||||||
lastMessage.ID = message.Msg.ID
|
|
||||||
}
|
|
||||||
var apiCast keybase.ChatAPI
|
|
||||||
apiCast.Msg = &message.Msg
|
|
||||||
newMessage := formatOutput(apiCast)
|
|
||||||
printMe = append(printMe, newMessage)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for i := len(printMe) - 1; i >= 0; i-- {
|
|
||||||
actuallyPrintMe += printMe[i]
|
|
||||||
if i > 0 {
|
|
||||||
actuallyPrintMe += "\n"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printToView("Chat", actuallyPrintMe)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func sendChat(message string) {
|
|
||||||
chat := k.NewChat(channel)
|
|
||||||
_, err := chat.Send(message)
|
|
||||||
if err != nil {
|
|
||||||
printToView("Feed", fmt.Sprintf("There was an error %+v", err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func formatOutput(api keybase.ChatAPI) string {
|
|
||||||
ret := ""
|
|
||||||
msgType := api.Msg.Content.Type
|
|
||||||
switch msgType {
|
|
||||||
case "text", "attachment":
|
|
||||||
var c = messageHeaderColor
|
|
||||||
ret = colorText(outputFormat, c, noColor)
|
|
||||||
tm := time.Unix(int64(api.Msg.SentAt), 0)
|
|
||||||
var msg = api.Msg.Content.Text.Body
|
|
||||||
// mention teams or users
|
|
||||||
msg = colorRegex(msg, `(@\w*(\.\w+)*)`, messageLinkColor, messageBodyColor)
|
|
||||||
// mention URL
|
|
||||||
msg = colorRegex(msg, `(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*))`, messageLinkColor, messageBodyColor)
|
|
||||||
msg = colorText(colorReplaceMentionMe(msg, messageBodyColor), messageBodyColor, c)
|
|
||||||
if msgType == "attachment" {
|
|
||||||
msg = fmt.Sprintf("%s\n%s", msg, colorText("[Attachment]", messageAttachmentColor, c))
|
|
||||||
}
|
|
||||||
|
|
||||||
user := colorUsername(api.Msg.Sender.Username, c)
|
|
||||||
device := colorText(api.Msg.Sender.DeviceName, messageSenderDeviceColor, c)
|
|
||||||
msgId := colorText(fmt.Sprintf("%d", api.Msg.ID), messageIdColor, c)
|
|
||||||
ts := colorText(fmt.Sprintf("%s", tm.Format(timeFormat)), messageTimeColor, c)
|
|
||||||
ret = strings.Replace(ret, "$MSG", msg, 1)
|
|
||||||
ret = strings.Replace(ret, "$USER", user, 1)
|
|
||||||
ret = strings.Replace(ret, "$DEVICE", device, 1)
|
|
||||||
ret = strings.Replace(ret, "$ID", msgId, 1)
|
|
||||||
ret = strings.Replace(ret, "$TIME", ts, 1)
|
|
||||||
ret = strings.Replace(ret, "$DATE", fmt.Sprintf("%s", tm.Format(dateFormat)), 1)
|
|
||||||
ret = strings.Replace(ret, "```", fmt.Sprintf("\n<code>\n"), -1)
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
func populateList() {
|
|
||||||
_, maxY := g.Size()
|
|
||||||
if testVar, err := k.ChatList(); err != nil {
|
|
||||||
log.Printf("%+v", err)
|
|
||||||
} else {
|
|
||||||
|
|
||||||
clearView("List")
|
|
||||||
var recentPMs = fmt.Sprintf("%s---[PMs]---%s\n", channelsHeaderColor, channelsColor)
|
|
||||||
var recentPMsCount = 0
|
|
||||||
var recentChannels = fmt.Sprintf("%s---[Teams]---%s\n", channelsHeaderColor, channelsColor)
|
|
||||||
var recentChannelsCount = 0
|
|
||||||
for _, s := range testVar.Result.Conversations {
|
|
||||||
channels = append(channels, s.Channel)
|
|
||||||
if s.Channel.MembersType == keybase.TEAM {
|
|
||||||
recentChannelsCount++
|
|
||||||
if recentChannelsCount <= ((maxY - 2) / 3) {
|
|
||||||
if s.Unread {
|
|
||||||
recentChannels += fmt.Sprintf("%s*", color(0))
|
|
||||||
}
|
|
||||||
recentChannels += fmt.Sprintf("%s\n\t#%s\n%s", s.Channel.Name, s.Channel.TopicName, channelsColor)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
recentPMsCount++
|
|
||||||
if recentPMsCount <= ((maxY - 2) / 3) {
|
|
||||||
if s.Unread {
|
|
||||||
recentChannels += fmt.Sprintf("%s*", color(0))
|
|
||||||
}
|
|
||||||
recentPMs += fmt.Sprintf("%s\n%s", cleanChannelName(s.Channel.Name), channelsColor)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
time.Sleep(1 * time.Millisecond)
|
|
||||||
printToView("List", fmt.Sprintf("%s%s%s%s", channelsColor, recentPMs, recentChannels, noColor))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getCurrentChannelMembership() []string {
|
|
||||||
var rs []string
|
|
||||||
if channel.Name != "" {
|
|
||||||
t := k.NewTeam(channel.Name)
|
|
||||||
if testVar, err := t.MemberList(); err != nil {
|
|
||||||
return rs // then this isn't a team, its a PM or there was an error in the API call
|
|
||||||
} else {
|
|
||||||
for _, m := range testVar.Result.Members.Owners {
|
|
||||||
rs = append(rs, fmt.Sprintf("%+v", m.Username))
|
|
||||||
}
|
|
||||||
for _, m := range testVar.Result.Members.Admins {
|
|
||||||
rs = append(rs, fmt.Sprintf("%+v", m.Username))
|
|
||||||
}
|
|
||||||
for _, m := range testVar.Result.Members.Writers {
|
|
||||||
rs = append(rs, fmt.Sprintf("%+v", m.Username))
|
|
||||||
}
|
|
||||||
for _, m := range testVar.Result.Members.Readers {
|
|
||||||
rs = append(rs, fmt.Sprintf("%+v", m.Username))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return rs
|
|
||||||
}
|
|
||||||
|
|
||||||
func filterStringSlice(ss []string, fv string) []string {
|
|
||||||
var rs []string
|
|
||||||
for _, s := range ss {
|
|
||||||
if strings.HasPrefix(s, fv) {
|
|
||||||
rs = append(rs, s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return rs
|
|
||||||
}
|
|
||||||
|
|
||||||
func longestCommonPrefix(ss []string) string {
|
|
||||||
// cover the case where the slice has no or one members
|
|
||||||
switch len(ss) {
|
|
||||||
case 0:
|
|
||||||
return ""
|
|
||||||
case 1:
|
|
||||||
return ss[0]
|
|
||||||
}
|
|
||||||
// all strings are compared by bytes here forward (TBD unicode normalization?)
|
|
||||||
// establish min, max lenth members of the slice by iterating over the members
|
|
||||||
min, max := ss[0], ss[0]
|
|
||||||
for _, s := range ss[1:] {
|
|
||||||
switch {
|
|
||||||
case s < min:
|
|
||||||
min = s
|
|
||||||
case s > max:
|
|
||||||
max = s
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// then iterate over the characters from min to max, as soon as chars don't match return
|
|
||||||
for i := 0; i < len(min) && i < len(max); i++ {
|
|
||||||
if min[i] != max[i] {
|
|
||||||
return min[:i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// to cover the case where all members are equal, just return one
|
|
||||||
return min
|
|
||||||
}
|
|
||||||
|
|
||||||
func stringRemainder(aStr, bStr string) string {
|
|
||||||
var long, short string
|
|
||||||
//figure out which string is longer
|
|
||||||
switch {
|
|
||||||
case len(aStr) < len(bStr):
|
|
||||||
short = aStr
|
|
||||||
long = bStr
|
|
||||||
default:
|
|
||||||
short = bStr
|
|
||||||
long = aStr
|
|
||||||
}
|
|
||||||
// iterate over the strings using an external iterator so we don't lose the value
|
|
||||||
i := 0
|
|
||||||
for i < len(short) && i < len(long) {
|
|
||||||
if short[i] != long[i] {
|
|
||||||
// the strings aren't equal so don't return anything
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
// return whatever's left of the longer string
|
|
||||||
return long[i:]
|
|
||||||
}
|
|
||||||
|
|
||||||
func appendIfNotInSlice(ss []string, s string) []string {
|
|
||||||
for _, element := range ss {
|
|
||||||
if element == s {
|
|
||||||
return ss
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return append(ss, s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func generateChannelTabCompletionSlice(inputWord string) []string {
|
|
||||||
// create a slice to hold the values
|
|
||||||
var firstSlice []string
|
|
||||||
// iterate over all the conversation results
|
|
||||||
for _, s := range channels {
|
|
||||||
if s.MembersType == keybase.TEAM {
|
|
||||||
// its a team so add the topic name as a possible tab completion
|
|
||||||
firstSlice = appendIfNotInSlice(firstSlice, s.TopicName)
|
|
||||||
firstSlice = appendIfNotInSlice(firstSlice, s.Name)
|
|
||||||
} else {
|
|
||||||
// its a user, so clean the name and append the users name as a possible tab completion
|
|
||||||
firstSlice = appendIfNotInSlice(firstSlice, cleanChannelName(s.Name))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// next fetch all members of the current channel and add them to the slice
|
|
||||||
secondSlice := getCurrentChannelMembership()
|
|
||||||
for _, m := range secondSlice {
|
|
||||||
firstSlice = appendIfNotInSlice(firstSlice, m)
|
|
||||||
}
|
|
||||||
// now return the resultSlice which contains all that are prefixed with inputWord
|
|
||||||
resultSlice := filterStringSlice(firstSlice, inputWord)
|
|
||||||
return resultSlice
|
|
||||||
}
|
|
||||||
|
|
||||||
func generateEmojiTabCompletionSlice(inputWord string) []string {
|
|
||||||
// use the emojiSlice from emojiList.go and filter it for the input word
|
|
||||||
resultSlice := filterStringSlice(emojiSlice, inputWord)
|
|
||||||
return resultSlice
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleTab() error {
|
|
||||||
inputString, err := getInputString("Input")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
// if you successfully get an input string, grab the last word from the string
|
|
||||||
ss := regexp.MustCompile(`[ #]`).Split(inputString, -1)
|
|
||||||
s := ss[len(ss)-1]
|
|
||||||
// create a variable in which to store the result
|
|
||||||
var resultSlice []string
|
|
||||||
// if the word starts with a : its an emoji lookup
|
|
||||||
if strings.HasPrefix(s, ":") {
|
|
||||||
resultSlice = generateEmojiTabCompletionSlice(s)
|
|
||||||
} else {
|
|
||||||
// now in case the word (s) is a mention @something, lets remove it to normalize
|
|
||||||
if strings.HasPrefix(s, "@") {
|
|
||||||
s = strings.Replace(s, "@", "", 1)
|
|
||||||
}
|
|
||||||
// now call get the list of all possible cantidates that have that as a prefix
|
|
||||||
resultSlice = generateChannelTabCompletionSlice(s)
|
|
||||||
}
|
|
||||||
rLen := len(resultSlice)
|
|
||||||
lcp := longestCommonPrefix(resultSlice)
|
|
||||||
if lcp != "" {
|
|
||||||
originalViewTitle := getViewTitle("Input")
|
|
||||||
newViewTitle := ""
|
|
||||||
if rLen >= 1 && originalViewTitle != "" {
|
|
||||||
if rLen == 1 {
|
|
||||||
newViewTitle = originalViewTitle
|
|
||||||
} else if rLen <= 5 {
|
|
||||||
newViewTitle = fmt.Sprintf("%s|| %s", originalViewTitle, strings.Join(resultSlice, " "))
|
|
||||||
} else if rLen > 5 {
|
|
||||||
newViewTitle = fmt.Sprintf("%s|| %s +%d more", originalViewTitle, strings.Join(resultSlice[:6], " "), rLen-5)
|
|
||||||
}
|
|
||||||
viewTitle("Input", newViewTitle)
|
|
||||||
remainder := stringRemainder(s, lcp)
|
|
||||||
writeToView("Input", remainder)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func clearView(viewName string) {
|
|
||||||
g.Update(func(g *gocui.Gui) error {
|
|
||||||
inputView, err := g.View(viewName)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
inputView.Clear()
|
|
||||||
inputView.SetCursor(0, 0)
|
|
||||||
inputView.SetOrigin(0, 0)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeToView(viewName string, message string) {
|
|
||||||
g.Update(func(g *gocui.Gui) error {
|
|
||||||
updatingView, err := g.View(viewName)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
for _, c := range message {
|
|
||||||
updatingView.EditWrite(c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func printToView(viewName string, message string) {
|
|
||||||
g.Update(func(g *gocui.Gui) error {
|
|
||||||
updatingView, err := g.View(viewName)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(updatingView, "%s\n", message)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func layout(g *gocui.Gui) error {
|
func layout(g *gocui.Gui) error {
|
||||||
maxX, maxY := g.Size()
|
maxX, maxY := g.Size()
|
||||||
if editView, err := g.SetView("Edit", maxX/2-maxX/3+1, maxY/2, maxX-2, maxY/2+10, 0); err != nil {
|
if editView, err := g.SetView("Edit", maxX/2-maxX/3+1, maxY/2, maxX-2, maxY/2+10, 0); err != nil {
|
||||||
@ -480,17 +104,6 @@ func layout(g *gocui.Gui) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getInputString(viewName string) (string, error) {
|
|
||||||
inputView, err := g.View(viewName)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
retString := inputView.Buffer()
|
|
||||||
retString = strings.Replace(retString, "\n", "", 800)
|
|
||||||
return retString, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func initKeybindings() 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 {
|
||||||
@ -501,9 +114,17 @@ func initKeybindings() error {
|
|||||||
if input != "" {
|
if input != "" {
|
||||||
clearView("Input")
|
clearView("Input")
|
||||||
return nil
|
return nil
|
||||||
} else {
|
|
||||||
return gocui.ErrQuit
|
|
||||||
}
|
}
|
||||||
|
return gocui.ErrQuit
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.SetKeybinding("Edit", gocui.KeyCtrlC, gocui.ModNone,
|
||||||
|
func(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
popupView("Chat")
|
||||||
|
popupView("Input")
|
||||||
|
clearView("Edit")
|
||||||
|
return nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -515,7 +136,7 @@ func initKeybindings() error {
|
|||||||
}
|
}
|
||||||
if err := g.SetKeybinding("Input", gocui.KeyTab, gocui.ModNone,
|
if err := g.SetKeybinding("Input", gocui.KeyTab, gocui.ModNone,
|
||||||
func(g *gocui.Gui, v *gocui.View) error {
|
func(g *gocui.Gui, v *gocui.View) error {
|
||||||
return handleTab()
|
return handleTab("Input")
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -528,9 +149,118 @@ func initKeybindings() error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err := g.SetKeybinding("Input", gocui.KeyArrowUp, gocui.ModNone,
|
||||||
|
func(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
RunCommand("edit")
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// End gocui basic setup
|
||||||
|
|
||||||
|
// Gocui helper funcs
|
||||||
|
func setViewTitle(viewName string, title string) {
|
||||||
|
g.Update(func(g *gocui.Gui) error {
|
||||||
|
updatingView, err := g.View(viewName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
updatingView.Title = title
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func getViewTitle(viewName string) string {
|
||||||
|
view, err := g.View(viewName)
|
||||||
|
if err != nil {
|
||||||
|
// in case there is active tab completion, filter that to just the view title and not the completion options.
|
||||||
|
printToView("Feed", fmt.Sprintf("Error getting view title: %s", err))
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return strings.Split(view.Title, "||")[0]
|
||||||
|
|
||||||
|
}
|
||||||
|
func popupView(viewName string) {
|
||||||
|
_, err := g.SetCurrentView(viewName)
|
||||||
|
if err != nil {
|
||||||
|
printToView("Feed", fmt.Sprintf("%+v", err))
|
||||||
|
}
|
||||||
|
_, err = g.SetViewOnTop(viewName)
|
||||||
|
if err != nil {
|
||||||
|
printToView("Feed", fmt.Sprintf("%+v", err))
|
||||||
|
}
|
||||||
|
g.Update(func(g *gocui.Gui) error {
|
||||||
|
updatingView, err := g.View(viewName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
updatingView.MoveCursor(0, 0, true)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func moveCursorToEnd(viewName string) {
|
||||||
|
g.Update(func(g *gocui.Gui) error {
|
||||||
|
inputView, err := g.View(viewName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
inputString, _ := getInputString(viewName)
|
||||||
|
stringLen := len(inputString)
|
||||||
|
maxX, _ := inputView.Size()
|
||||||
|
x := stringLen % maxX
|
||||||
|
y := stringLen / maxX
|
||||||
|
inputView.SetCursor(0, 0)
|
||||||
|
inputView.SetOrigin(0, 0)
|
||||||
|
inputView.MoveCursor(x, y, true)
|
||||||
|
return nil
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func clearView(viewName string) {
|
||||||
|
g.Update(func(g *gocui.Gui) error {
|
||||||
|
inputView, err := g.View(viewName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
inputView.Clear()
|
||||||
|
inputView.SetCursor(0, 0)
|
||||||
|
inputView.SetOrigin(0, 0)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
func writeToView(viewName string, message string) {
|
||||||
|
g.Update(func(g *gocui.Gui) error {
|
||||||
|
updatingView, err := g.View(viewName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, c := range message {
|
||||||
|
updatingView.EditWrite(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func printToView(viewName string, message string) {
|
||||||
|
g.Update(func(g *gocui.Gui) error {
|
||||||
|
updatingView, err := g.View(viewName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Fprintf(updatingView, "%s\n", message)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// End gocui helper funcs
|
||||||
|
|
||||||
|
// Update/Populate views automatically
|
||||||
func updateChatWindow() {
|
func updateChatWindow() {
|
||||||
|
|
||||||
runOpts := keybase.RunOptions{
|
runOpts := keybase.RunOptions{
|
||||||
@ -542,12 +272,133 @@ func updateChatWindow() {
|
|||||||
runOpts)
|
runOpts)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
func populateChat() {
|
||||||
|
lastMessage.ID = 0
|
||||||
|
chat := k.NewChat(channel)
|
||||||
|
maxX, _ := g.Size()
|
||||||
|
api, err := chat.Read(maxX / 2)
|
||||||
|
if err != nil {
|
||||||
|
for _, testChan := range channels {
|
||||||
|
if channel.Name == testChan.Name {
|
||||||
|
channel = testChan
|
||||||
|
channel.TopicName = "general"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
chat = k.NewChat(channel)
|
||||||
|
_, err2 := chat.Read(2)
|
||||||
|
if err2 != nil {
|
||||||
|
printToView("Feed", fmt.Sprintf("%+v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go populateChat()
|
||||||
|
go generateChannelTabCompletionSlice()
|
||||||
|
return
|
||||||
|
|
||||||
|
}
|
||||||
|
var printMe []string
|
||||||
|
var actuallyPrintMe string
|
||||||
|
if len(api.Result.Messages) > 0 {
|
||||||
|
lastMessage.ID = api.Result.Messages[0].Msg.ID
|
||||||
|
}
|
||||||
|
for _, message := range api.Result.Messages {
|
||||||
|
if message.Msg.Content.Type == "text" || message.Msg.Content.Type == "attachment" {
|
||||||
|
if lastMessage.ID < 1 {
|
||||||
|
lastMessage.ID = message.Msg.ID
|
||||||
|
}
|
||||||
|
var apiCast keybase.ChatAPI
|
||||||
|
apiCast.Msg = &message.Msg
|
||||||
|
newMessage := formatOutput(apiCast)
|
||||||
|
printMe = append(printMe, newMessage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := len(printMe) - 1; i >= 0; i-- {
|
||||||
|
actuallyPrintMe += printMe[i]
|
||||||
|
if i > 0 {
|
||||||
|
actuallyPrintMe += "\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printToView("Chat", actuallyPrintMe)
|
||||||
|
|
||||||
|
}
|
||||||
|
func populateList() {
|
||||||
|
_, maxY := g.Size()
|
||||||
|
if testVar, err := k.ChatList(); err != nil {
|
||||||
|
log.Printf("%+v", err)
|
||||||
|
} else {
|
||||||
|
|
||||||
|
clearView("List")
|
||||||
|
var recentPMs = fmt.Sprintf("%s---[PMs]---%s\n", channelsHeaderColor, channelsColor)
|
||||||
|
var recentPMsCount = 0
|
||||||
|
var recentChannels = fmt.Sprintf("%s---[Teams]---%s\n", channelsHeaderColor, channelsColor)
|
||||||
|
var recentChannelsCount = 0
|
||||||
|
for _, s := range testVar.Result.Conversations {
|
||||||
|
channels = append(channels, s.Channel)
|
||||||
|
if s.Channel.MembersType == keybase.TEAM {
|
||||||
|
recentChannelsCount++
|
||||||
|
if recentChannelsCount <= ((maxY - 2) / 3) {
|
||||||
|
if s.Unread {
|
||||||
|
recentChannels += fmt.Sprintf("%s*", color(0))
|
||||||
|
}
|
||||||
|
recentChannels += fmt.Sprintf("%s\n\t#%s\n%s", s.Channel.Name, s.Channel.TopicName, channelsColor)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
recentPMsCount++
|
||||||
|
if recentPMsCount <= ((maxY - 2) / 3) {
|
||||||
|
if s.Unread {
|
||||||
|
recentChannels += fmt.Sprintf("%s*", color(0))
|
||||||
|
}
|
||||||
|
recentPMs += fmt.Sprintf("%s\n%s", cleanChannelName(s.Channel.Name), channelsColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
time.Sleep(1 * time.Millisecond)
|
||||||
|
printToView("List", fmt.Sprintf("%s%s%s%s", channelsColor, recentPMs, recentChannels, noColor))
|
||||||
|
go generateRecentTabCompletionSlice()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// End update/populate views automatically
|
||||||
|
|
||||||
|
// Formatting
|
||||||
func cleanChannelName(c string) string {
|
func cleanChannelName(c string) string {
|
||||||
newChannelName := strings.Replace(c, fmt.Sprintf("%s,", k.Username), "", 1)
|
newChannelName := strings.Replace(c, fmt.Sprintf("%s,", k.Username), "", 1)
|
||||||
return strings.Replace(newChannelName, fmt.Sprintf(",%s", k.Username), "", 1)
|
return strings.Replace(newChannelName, fmt.Sprintf(",%s", k.Username), "", 1)
|
||||||
}
|
}
|
||||||
|
func formatOutput(api keybase.ChatAPI) string {
|
||||||
|
ret := ""
|
||||||
|
msgType := api.Msg.Content.Type
|
||||||
|
switch msgType {
|
||||||
|
case "text", "attachment":
|
||||||
|
var c = messageHeaderColor
|
||||||
|
ret = colorText(outputFormat, c, noColor)
|
||||||
|
tm := time.Unix(int64(api.Msg.SentAt), 0)
|
||||||
|
var msg = api.Msg.Content.Text.Body
|
||||||
|
// mention teams or users
|
||||||
|
msg = colorRegex(msg, `(@\w*(\.\w+)*)`, messageLinkColor, messageBodyColor)
|
||||||
|
// mention URL
|
||||||
|
msg = colorRegex(msg, `(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*))`, messageLinkColor, messageBodyColor)
|
||||||
|
msg = colorText(colorReplaceMentionMe(msg, messageBodyColor), messageBodyColor, c)
|
||||||
|
if msgType == "attachment" {
|
||||||
|
msg = fmt.Sprintf("%s\n%s", api.Msg.Content.Attachment.Object.Title, colorText(fmt.Sprintf("[Attachment: %s]", api.Msg.Content.Attachment.Object.Filename), messageAttachmentColor, c))
|
||||||
|
}
|
||||||
|
user := colorUsername(api.Msg.Sender.Username, c)
|
||||||
|
device := colorText(api.Msg.Sender.DeviceName, messageSenderDeviceColor, c)
|
||||||
|
msgID := colorText(fmt.Sprintf("%d", api.Msg.ID), messageIdColor, c)
|
||||||
|
ts := colorText(tm.Format(timeFormat), messageTimeColor, c)
|
||||||
|
ret = strings.Replace(ret, "$MSG", msg, 1)
|
||||||
|
ret = strings.Replace(ret, "$USER", user, 1)
|
||||||
|
ret = strings.Replace(ret, "$DEVICE", device, 1)
|
||||||
|
ret = strings.Replace(ret, "$ID", msgID, 1)
|
||||||
|
ret = strings.Replace(ret, "$TIME", ts, 1)
|
||||||
|
ret = strings.Replace(ret, "$DATE", colorText(tm.Format(dateFormat), messageTimeColor, c), 1)
|
||||||
|
ret = strings.Replace(ret, "```", fmt.Sprintf("\n<code>\n"), -1)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
// End formatting
|
||||||
|
|
||||||
|
// Input handling
|
||||||
func handleMessage(api keybase.ChatAPI) {
|
func handleMessage(api keybase.ChatAPI) {
|
||||||
if _, ok := typeCommands[api.Msg.Content.Type]; ok {
|
if _, ok := typeCommands[api.Msg.Content.Type]; ok {
|
||||||
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 {
|
||||||
@ -586,16 +437,12 @@ func handleMessage(api keybase.ChatAPI) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
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 {
|
||||||
if channel.MembersType == keybase.TEAM && channel.TopicName != api.Msg.Channel.TopicName {
|
if channel.MembersType == keybase.USER || channel.MembersType == keybase.TEAM && channel.TopicName == api.Msg.Channel.TopicName {
|
||||||
// Do nothing, wrong channel
|
|
||||||
} else {
|
|
||||||
|
|
||||||
printToView("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)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if api.Msg.Channel.MembersType == keybase.TEAM {
|
if api.Msg.Channel.MembersType == keybase.TEAM {
|
||||||
@ -612,9 +459,16 @@ func handleMessage(api keybase.ChatAPI) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func getInputString(viewName string) (string, error) {
|
||||||
// It seems that golang doesn't have filter and other high order functions :'(
|
inputView, err := g.View(viewName)
|
||||||
func delete_empty(s []string) []string {
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
retString := inputView.Buffer()
|
||||||
|
retString = strings.Replace(retString, "\n", "", 800)
|
||||||
|
return retString, err
|
||||||
|
}
|
||||||
|
func deleteEmpty(s []string) []string {
|
||||||
var r []string
|
var r []string
|
||||||
for _, str := range s {
|
for _, str := range s {
|
||||||
if str != "" {
|
if str != "" {
|
||||||
@ -623,7 +477,6 @@ func delete_empty(s []string) []string {
|
|||||||
}
|
}
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleInput(viewName string) error {
|
func handleInput(viewName string) error {
|
||||||
clearView(viewName)
|
clearView(viewName)
|
||||||
inputString, _ := getInputString(viewName)
|
inputString, _ := getInputString(viewName)
|
||||||
@ -631,7 +484,10 @@ func handleInput(viewName string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(inputString, cmdPrefix) {
|
if strings.HasPrefix(inputString, cmdPrefix) {
|
||||||
cmd := delete_empty(strings.Split(inputString[len(cmdPrefix):], " "))
|
cmd := deleteEmpty(strings.Split(inputString[len(cmdPrefix):], " "))
|
||||||
|
if len(cmd) < 1 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if c, ok := commands[cmd[0]]; ok {
|
if c, ok := commands[cmd[0]]; ok {
|
||||||
c.Exec(cmd)
|
c.Exec(cmd)
|
||||||
return nil
|
return nil
|
||||||
@ -644,18 +500,28 @@ func handleInput(viewName string) error {
|
|||||||
}
|
}
|
||||||
if inputString[:1] == "+" || inputString[:1] == "-" {
|
if inputString[:1] == "+" || inputString[:1] == "-" {
|
||||||
cmd := strings.Split(inputString, " ")
|
cmd := strings.Split(inputString, " ")
|
||||||
|
cmd[0] = inputString[:1]
|
||||||
RunCommand(cmd...)
|
RunCommand(cmd...)
|
||||||
} else {
|
} else {
|
||||||
go sendChat(inputString)
|
go sendChat(inputString)
|
||||||
}
|
}
|
||||||
// restore any tab completion view titles on input commit
|
// restore any tab completion view titles on input commit
|
||||||
if newViewTitle := getViewTitle(viewName); newViewTitle != "" {
|
if newViewTitle := getViewTitle(viewName); newViewTitle != "" {
|
||||||
viewTitle(viewName, newViewTitle)
|
setViewTitle(viewName, newViewTitle)
|
||||||
}
|
}
|
||||||
|
|
||||||
go populateList()
|
go populateList()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
func sendChat(message string) {
|
||||||
|
chat := k.NewChat(channel)
|
||||||
|
_, err := chat.Send(message)
|
||||||
|
if err != nil {
|
||||||
|
printToView("Feed", fmt.Sprintf("There was an error %+v", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// End input handling
|
||||||
|
|
||||||
func quit(g *gocui.Gui, v *gocui.View) error {
|
func quit(g *gocui.Gui, v *gocui.View) error {
|
||||||
return gocui.ErrQuit
|
return gocui.ErrQuit
|
||||||
|
|||||||
213
tabComplete.go
Normal file
213
tabComplete.go
Normal file
@ -0,0 +1,213 @@
|
|||||||
|
// +build !rm_basic_commands allcommands tabcompletion
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"samhofi.us/x/keybase"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
tabSlice []string
|
||||||
|
commandSlice []string
|
||||||
|
)
|
||||||
|
|
||||||
|
// This defines the handleTab function thats called by key bindind tab for the input control.
|
||||||
|
func handleTab(viewName string) error {
|
||||||
|
inputString, err := getInputString(viewName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// if you successfully get an input string, grab the last word from the string
|
||||||
|
ss := regexp.MustCompile(`[ #]`).Split(inputString, -1)
|
||||||
|
s := ss[len(ss)-1]
|
||||||
|
// create a variable in which to store the result
|
||||||
|
var resultSlice []string
|
||||||
|
// if the word starts with a : its an emoji lookup
|
||||||
|
if strings.HasPrefix(s, ":") {
|
||||||
|
resultSlice = getEmojiTabCompletionSlice(s)
|
||||||
|
} else if strings.HasPrefix(s, "/") {
|
||||||
|
generateCommandTabCompletionSlice()
|
||||||
|
s = strings.Replace(s, "/", "", 1)
|
||||||
|
resultSlice = getCommandTabCompletionSlice(s)
|
||||||
|
} else {
|
||||||
|
if strings.HasPrefix(s, "@") {
|
||||||
|
// now in case the word (s) is a mention @something, lets remove it to normalize
|
||||||
|
s = strings.Replace(s, "@", "", 1)
|
||||||
|
}
|
||||||
|
// now call get the list of all possible cantidates that have that as a prefix
|
||||||
|
resultSlice = getChannelTabCompletionSlice(s)
|
||||||
|
}
|
||||||
|
rLen := len(resultSlice)
|
||||||
|
lcp := longestCommonPrefix(resultSlice)
|
||||||
|
if lcp != "" {
|
||||||
|
originalViewTitle := getViewTitle("Input")
|
||||||
|
newViewTitle := ""
|
||||||
|
if rLen >= 1 && originalViewTitle != "" {
|
||||||
|
if rLen == 1 {
|
||||||
|
newViewTitle = originalViewTitle
|
||||||
|
} else if rLen <= 5 {
|
||||||
|
newViewTitle = fmt.Sprintf("%s|| %s", originalViewTitle, strings.Join(resultSlice, " "))
|
||||||
|
} else if rLen > 5 {
|
||||||
|
newViewTitle = fmt.Sprintf("%s|| %s +%d more", originalViewTitle, strings.Join(resultSlice[:6], " "), rLen-5)
|
||||||
|
}
|
||||||
|
setViewTitle(viewName, newViewTitle)
|
||||||
|
remainder := stringRemainder(s, lcp)
|
||||||
|
writeToView(viewName, remainder)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Main tab completion functions
|
||||||
|
func getEmojiTabCompletionSlice(inputWord string) []string {
|
||||||
|
// use the emojiSlice from emojiList.go and filter it for the input word
|
||||||
|
resultSlice := filterStringSlice(emojiSlice, inputWord)
|
||||||
|
return resultSlice
|
||||||
|
}
|
||||||
|
func getChannelTabCompletionSlice(inputWord string) []string {
|
||||||
|
// use the tabSlice from above and filter it for the input word
|
||||||
|
resultSlice := filterStringSlice(tabSlice, inputWord)
|
||||||
|
return resultSlice
|
||||||
|
}
|
||||||
|
func getCommandTabCompletionSlice(inputWord string) []string {
|
||||||
|
// use the commandSlice from above and filter it for the input word
|
||||||
|
resultSlice := filterStringSlice(commandSlice, inputWord)
|
||||||
|
return resultSlice
|
||||||
|
}
|
||||||
|
|
||||||
|
//Generator Functions (should be called externally when chat/list/join changes
|
||||||
|
func generateChannelTabCompletionSlice() {
|
||||||
|
// fetch all members of the current channel and add them to the slice
|
||||||
|
channelSlice := getCurrentChannelMembership()
|
||||||
|
for _, m := range channelSlice {
|
||||||
|
tabSlice = appendIfNotInSlice(tabSlice, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func generateCommandTabCompletionSlice() {
|
||||||
|
// get the maps of all built commands - this should only need to be done on startup
|
||||||
|
// removing typeCommands for now, since they aren't actually commands you can type - contrary to the naming
|
||||||
|
/*for commandString1 := range typeCommands {
|
||||||
|
commandSlice = appendIfNotInSlice(commandSlice, commandString1)
|
||||||
|
}*/
|
||||||
|
for commandString2 := range commands {
|
||||||
|
commandSlice = appendIfNotInSlice(commandSlice, commandString2)
|
||||||
|
}
|
||||||
|
for _, commandString3 := range baseCommands {
|
||||||
|
commandSlice = appendIfNotInSlice(commandSlice, commandString3)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func generateRecentTabCompletionSlice() {
|
||||||
|
var recentSlice []string
|
||||||
|
for _, s := range channels {
|
||||||
|
if s.MembersType == keybase.TEAM {
|
||||||
|
// its a team so add the topic name and channel name
|
||||||
|
recentSlice = appendIfNotInSlice(recentSlice, s.TopicName)
|
||||||
|
recentSlice = appendIfNotInSlice(recentSlice, s.Name)
|
||||||
|
} else {
|
||||||
|
//its a user, so clean the name and append
|
||||||
|
recentSlice = appendIfNotInSlice(recentSlice, cleanChannelName(s.Name))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, s := range recentSlice {
|
||||||
|
tabSlice = appendIfNotInSlice(tabSlice, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper functions
|
||||||
|
func getCurrentChannelMembership() []string {
|
||||||
|
var rs []string
|
||||||
|
if channel.Name != "" {
|
||||||
|
t := k.NewTeam(channel.Name)
|
||||||
|
testVar, err := t.MemberList()
|
||||||
|
if err != nil {
|
||||||
|
return rs // then this isn't a team, its a PM or there was an error in the API call
|
||||||
|
}
|
||||||
|
for _, m := range testVar.Result.Members.Owners {
|
||||||
|
rs = append(rs, fmt.Sprintf("%+v", m.Username))
|
||||||
|
}
|
||||||
|
for _, m := range testVar.Result.Members.Admins {
|
||||||
|
rs = append(rs, fmt.Sprintf("%+v", m.Username))
|
||||||
|
}
|
||||||
|
for _, m := range testVar.Result.Members.Writers {
|
||||||
|
rs = append(rs, fmt.Sprintf("%+v", m.Username))
|
||||||
|
}
|
||||||
|
for _, m := range testVar.Result.Members.Readers {
|
||||||
|
rs = append(rs, fmt.Sprintf("%+v", m.Username))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return rs
|
||||||
|
}
|
||||||
|
func filterStringSlice(ss []string, fv string) []string {
|
||||||
|
var rs []string
|
||||||
|
for _, s := range ss {
|
||||||
|
if strings.HasPrefix(s, fv) {
|
||||||
|
rs = append(rs, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return rs
|
||||||
|
}
|
||||||
|
func longestCommonPrefix(ss []string) string {
|
||||||
|
// cover the case where the slice has no or one members
|
||||||
|
switch len(ss) {
|
||||||
|
case 0:
|
||||||
|
return ""
|
||||||
|
case 1:
|
||||||
|
return ss[0]
|
||||||
|
}
|
||||||
|
// all strings are compared by bytes here forward (TBD unicode normalization?)
|
||||||
|
// establish min, max lenth members of the slice by iterating over the members
|
||||||
|
min, max := ss[0], ss[0]
|
||||||
|
for _, s := range ss[1:] {
|
||||||
|
switch {
|
||||||
|
case s < min:
|
||||||
|
min = s
|
||||||
|
case s > max:
|
||||||
|
max = s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// then iterate over the characters from min to max, as soon as chars don't match return
|
||||||
|
for i := 0; i < len(min) && i < len(max); i++ {
|
||||||
|
if min[i] != max[i] {
|
||||||
|
return min[:i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// to cover the case where all members are equal, just return one
|
||||||
|
return min
|
||||||
|
}
|
||||||
|
func stringRemainder(aStr, bStr string) string {
|
||||||
|
var long, short string
|
||||||
|
//figure out which string is longer
|
||||||
|
switch {
|
||||||
|
case len(aStr) < len(bStr):
|
||||||
|
short = aStr
|
||||||
|
long = bStr
|
||||||
|
default:
|
||||||
|
short = bStr
|
||||||
|
long = aStr
|
||||||
|
}
|
||||||
|
// iterate over the strings using an external iterator so we don't lose the value
|
||||||
|
i := 0
|
||||||
|
for i < len(short) && i < len(long) {
|
||||||
|
if short[i] != long[i] {
|
||||||
|
// the strings aren't equal so don't return anything
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
// return whatever's left of the longer string
|
||||||
|
return long[i:]
|
||||||
|
}
|
||||||
|
func appendIfNotInSlice(ss []string, s string) []string {
|
||||||
|
for _, element := range ss {
|
||||||
|
if element == s {
|
||||||
|
return ss
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return append(ss, s)
|
||||||
|
}
|
||||||
@ -17,7 +17,7 @@ var messageBodyColor = noColor
|
|||||||
var messageAttachmentColor = color(2)
|
var messageAttachmentColor = color(2)
|
||||||
var messageLinkColor = color(4)
|
var messageLinkColor = color(4)
|
||||||
|
|
||||||
// BASH-like PS1 variable equivalent (without colours)
|
// BASH-like PS1 variable equivalent
|
||||||
var outputFormat = "┌──[$USER@$DEVICE] [$ID] [$DATE - $TIME]\n└╼ $MSG"
|
var outputFormat = "┌──[$USER@$DEVICE] [$ID] [$DATE - $TIME]\n└╼ $MSG"
|
||||||
|
|
||||||
// 02 = Day, Jan = Month, 06 = Year
|
// 02 = Day, Jan = Month, 06 = Year
|
||||||
|
|||||||
Reference in New Issue
Block a user