Skip to content

Commit 90d3369

Browse files
authored
Merge pull request #41 from Rudi9719/dev
Dev
2 parents 005d737 + 69b5442 commit 90d3369

30 files changed

+895
-353
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ emojiList.go
66
.idea/*
77
.idea
88
*.log
9+
.travis.yml

cmdClean.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func init() {
1515

1616
func cmdClean(cmd []string) {
1717
clearView("Chat")
18+
clearView("List")
1819
go populateChat()
19-
20+
go populateList()
2021
}

cmdConfig.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// +build !rm_basic_commands allcommands setcmd
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"io/ioutil"
8+
"os"
9+
10+
"github.com/pelletier/go-toml"
11+
)
12+
13+
func init() {
14+
command := Command{
15+
Cmd: []string{"config"},
16+
Description: "Change various settings",
17+
Help: "",
18+
Exec: cmdConfig,
19+
}
20+
21+
RegisterCommand(command)
22+
}
23+
24+
func cmdConfig(cmd []string) {
25+
var err error
26+
switch {
27+
case len(cmd) == 2:
28+
if cmd[1] == "load" {
29+
config, err = readConfig()
30+
if err != nil {
31+
printError(err.Error())
32+
return
33+
}
34+
printInfoF("Config file loaded: $TEXT", config.Colors.Message.Attachment.stylize(config.filepath))
35+
return
36+
}
37+
case len(cmd) > 2:
38+
if cmd[1] == "load" {
39+
config, err = readConfig(cmd[3])
40+
if err != nil {
41+
printError(err.Error())
42+
return
43+
}
44+
printInfoF("Config file loaded: $TEXT", config.Colors.Message.Attachment.stylize(config.filepath))
45+
return
46+
}
47+
}
48+
printError("Must pass a valid command")
49+
}
50+
51+
func readConfig(filepath ...string) (*Config, error) {
52+
var result = new(Config)
53+
var configFile string
54+
var env bool
55+
56+
// Load default config first, this way any values missing from the provided config file will remain the default value
57+
d := []byte(defaultConfig)
58+
toml.Unmarshal(d, result)
59+
60+
switch len(filepath) {
61+
case 0:
62+
configFile, env = os.LookupEnv("KBTUI_CFG")
63+
if !env {
64+
configFile = "~/.config/kbtui.toml"
65+
if _, err := os.Stat(configFile); os.IsNotExist(err) {
66+
configFile = "kbtui.toml"
67+
}
68+
}
69+
default:
70+
configFile = filepath[0]
71+
if _, err := os.Stat(configFile); os.IsNotExist(err) {
72+
return result, fmt.Errorf("Unable to load config: %s not found", configFile)
73+
}
74+
}
75+
76+
f, err := ioutil.ReadFile(configFile)
77+
if err != nil {
78+
f = []byte(defaultConfig)
79+
}
80+
81+
err = toml.Unmarshal(f, result)
82+
if err != nil {
83+
return result, err
84+
}
85+
86+
result.filepath = configFile
87+
return result, nil
88+
}

cmdDelete.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package main
44

55
import (
6-
"fmt"
76
"strconv"
87
)
98

@@ -28,7 +27,7 @@ func cmdDelete(cmd []string) {
2827
chat := k.NewChat(channel)
2928
_, err := chat.Delete(messageID)
3029
if err != nil {
31-
printToView("Feed", fmt.Sprintf("There was an error deleting your message."))
30+
printError("There was an error deleting your message.")
3231
}
3332

3433
}

cmdDownload.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ func init() {
2121
func cmdDownloadFile(cmd []string) {
2222

2323
if len(cmd) < 2 {
24-
printToView("Feed", fmt.Sprintf("%s%s $messageId $fileName - Download a file to user's downloadpath", cmdPrefix, cmd[0]))
24+
printInfo(fmt.Sprintf("%s%s $messageId $fileName - Download a file to user's downloadpath", config.Basics.CmdPrefix, cmd[0]))
2525
return
2626
}
2727
messageID, err := strconv.Atoi(cmd[1])
2828
if err != nil {
29-
printToView("Feed", "There was an error converting your messageID to an int")
29+
printError("There was an error converting your messageID to an int")
3030
return
3131
}
3232
chat := k.NewChat(channel)
3333
api, err := chat.ReadMessage(messageID)
3434
if err != nil {
35-
printToView("Feed", fmt.Sprintf("There was an error pulling message %d", messageID))
35+
printError(fmt.Sprintf("There was an error pulling message %d", messageID))
3636
return
3737
}
3838
if api.Result.Messages[0].Msg.Content.Type != "attachment" {
39-
printToView("Feed", "No attachment detected")
39+
printError("No attachment detected")
4040
return
4141
}
4242
var fileName string
@@ -46,10 +46,11 @@ func cmdDownloadFile(cmd []string) {
4646
fileName = api.Result.Messages[0].Msg.Content.Attachment.Object.Filename
4747
}
4848

49-
_, err = chat.Download(messageID, fmt.Sprintf("%s/%s", downloadPath, fileName))
49+
_, err = chat.Download(messageID, fmt.Sprintf("%s/%s", config.Basics.DownloadPath, fileName))
50+
channelName := config.Colors.Message.LinkKeybase.stylize(channel.Name)
5051
if err != nil {
51-
printToView("Feed", fmt.Sprintf("There was an error downloading %s from %s", fileName, channel.Name))
52+
printErrorF(fmt.Sprintf("There was an error downloading %s from $TEXT", fileName), channelName)
5253
} else {
53-
printToView("Feed", fmt.Sprintf("Downloaded %s from %s", fileName, channel.Name))
54+
printInfoF(fmt.Sprintf("Downloaded %s from $TEXT", fileName), channelName)
5455
}
5556
}

cmdEdit.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,24 @@ func cmdEdit(cmd []string) {
2626
if len(cmd) == 2 {
2727
messageID, _ = strconv.Atoi(cmd[1])
2828
} else if lastMessage.ID != 0 {
29+
message, _ := chat.ReadMessage(lastMessage.ID)
30+
lastMessage.Type = message.Result.Messages[0].Msg.Content.Type
2931
if lastMessage.Type != "text" {
30-
printToView("Feed", "Last message isn't editable (is it an edit?)")
32+
printError("Last message isn't editable (is it an edit?)")
3133
return
3234
}
3335
messageID = lastMessage.ID
3436
} else {
35-
printToView("Feed", "No message to edit")
37+
printError("No message to edit")
3638
return
3739
}
3840
origMessage, _ := chat.ReadMessage(messageID)
3941
if origMessage.Result.Messages[0].Msg.Content.Type != "text" {
40-
printToView("Feed", fmt.Sprintf("%+v", origMessage))
42+
printInfo(fmt.Sprintf("%+v", origMessage))
4143
return
4244
}
4345
if origMessage.Result.Messages[0].Msg.Sender.Username != k.Username {
44-
printToView("Feed", "You cannot edit another user's messages.")
46+
printError("You cannot edit another user's messages.")
4547
return
4648
}
4749
editString := origMessage.Result.Messages[0].Msg.Content.Text.Body
@@ -53,14 +55,14 @@ func cmdEdit(cmd []string) {
5355
return
5456
}
5557
if len(cmd) < 3 {
56-
printToView("Feed", "Not enough options for Edit")
58+
printError("Not enough options for Edit")
5759
return
5860
}
5961
messageID, _ = strconv.Atoi(cmd[1])
6062
newMessage := strings.Join(cmd[2:], " ")
6163
_, err := chat.Edit(messageID, newMessage)
6264
if err != nil {
63-
printToView("Feed", fmt.Sprintf("Error editing message %d, %+v", messageID, err))
65+
printError(fmt.Sprintf("Error editing message %d, %+v", messageID, err))
6466
}
6567

6668
}

cmdExec.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// +build !rm_basic_commands allcommands execcmd
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"strings"
8+
)
9+
10+
func init() {
11+
command := Command{
12+
Cmd: []string{"exec", "ex"},
13+
Description: "$keybase args - executes keybase $args and returns the output",
14+
Help: "",
15+
Exec: cmdExec,
16+
}
17+
RegisterCommand(command)
18+
}
19+
20+
func cmdExec(cmd []string) {
21+
l := len(cmd)
22+
switch {
23+
case l >= 2:
24+
if cmd[1] == "keybase" {
25+
// if the user types /exec keybase wallet list
26+
// only send ["wallet", "list"]
27+
runKeybaseExec(cmd[2:])
28+
} else {
29+
// send everything except the command
30+
runKeybaseExec(cmd[1:])
31+
}
32+
case l == 1:
33+
fallthrough
34+
default:
35+
printExecHelp()
36+
}
37+
}
38+
39+
func runKeybaseExec(args []string) {
40+
outputBytes, err := k.Exec(args...)
41+
if err != nil {
42+
printToView("Feed", fmt.Sprintf("Exec error: %+v", err))
43+
} else {
44+
channel.Name = ""
45+
// unjoin the chat
46+
clearView("Chat")
47+
setViewTitle("Input", fmt.Sprintf(" /exec %s ", strings.Join(args, " ")))
48+
output := string(outputBytes)
49+
printToView("Chat", fmt.Sprintf("%s", output))
50+
}
51+
}
52+
53+
func printExecHelp() {
54+
printInfo(fmt.Sprintf("To execute a keybase command use %sexec <keybase args>", config.Basics.CmdPrefix))
55+
}

cmdHelp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func cmdHelp(cmd []string) {
2525
if len(cmd) == 1 {
2626
sort.Strings(baseCommands)
2727
for _, c := range baseCommands {
28-
helpText = fmt.Sprintf("%s%s%s\t\t%s\n", helpText, cmdPrefix, c, commands[c].Description)
28+
helpText = fmt.Sprintf("%s%s%s\t\t%s\n", helpText, config.Basics.CmdPrefix, c, commands[c].Description)
2929
}
3030
if len(typeCommands) > 0 {
3131
for c := range typeCommands {

cmdJoin.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ func cmdJoin(cmd []string) {
4141
channel.TopicName = ""
4242
channel.MembersType = keybase.USER
4343
}
44-
printToView("Feed", fmt.Sprintf("You are joining: %s", joinedName))
44+
printInfoF("You are joining: $TEXT", config.Colors.Message.LinkKeybase.stylize(joinedName))
4545
clearView("Chat")
4646
setViewTitle("Input", fmt.Sprintf(" %s ", joinedName))
47+
lastChat = joinedName
48+
autoScrollView("Chat")
4749
go populateChat()
4850
default:
49-
printToView("Feed", fmt.Sprintf("To join a team use %sjoin <team> <channel>", cmdPrefix))
50-
printToView("Feed", fmt.Sprintf("To join a PM use %sjoin <user>", cmdPrefix))
51+
printInfo(fmt.Sprintf("To join a team use %sjoin <team> <channel>", config.Basics.CmdPrefix))
52+
printInfo(fmt.Sprintf("To join a PM use %sjoin <user>", config.Basics.CmdPrefix))
5153
}
5254
}

cmdPost.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func cmdPost(cmd []string) {
2929
chat := k.NewChat(pubChan)
3030
_, err := chat.Send(post)
3131
if err != nil {
32-
printToView("Feed", fmt.Sprintf("There was an error with your post: %+v", err))
32+
printError(fmt.Sprintf("There was an error with your post: %+v", err))
3333
} else {
34-
printToView("Feed", "You have publically posted to your wall, signed by your current device.")
34+
printInfo("You have publically posted to your wall, signed by your current device.")
3535
}
3636
}

cmdReact.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ func doReact(messageID int, reaction string) {
3838
chat := k.NewChat(channel)
3939
_, err := chat.React(messageID, reaction)
4040
if err != nil {
41-
printToView("Feed", "There was an error reacting to the message.")
41+
printError("There was an error reacting to the message.")
4242
}
4343
}

cmdReply.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,17 @@ func init() {
2222
func cmdReply(cmd []string) {
2323
chat := k.NewChat(channel)
2424
if len(cmd) < 2 {
25-
printToView("Feed", fmt.Sprintf("%s%s $ID - Reply to message $ID", cmdPrefix, cmd[0]))
25+
printInfo(fmt.Sprintf("%s%s $ID - Reply to message $ID", config.Basics.CmdPrefix, cmd[0]))
2626
return
2727
}
2828
messageID, err := strconv.Atoi(cmd[1])
2929
if err != nil {
30-
printToView("Feed", fmt.Sprintf("There was an error determining message ID %s", cmd[1]))
30+
printError(fmt.Sprintf("There was an error determining message ID %s", cmd[1]))
3131
return
3232
}
3333
_, err = chat.Reply(messageID, strings.Join(cmd[2:], " "))
3434
if err != nil {
35-
printToView("Feed", "There was an error with your reply.")
35+
printError("There was an error with your reply.")
3636
return
3737
}
38-
return
3938
}

0 commit comments

Comments
 (0)