-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd-contacts.go
142 lines (129 loc) · 3.36 KB
/
cmd-contacts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import "strconv"
type contactsCmd struct {
Mode string
Name string
ContactID uint
}
func (contactsCmd) Command() string {
return "contacts"
}
func (contactsCmd) Description() string {
return "manages contacts of the current crew"
}
func (contactsCmd) Help(args []string) {
output(func(printer printer) {
printer("contacts select NAME - selects the current contact")
printer("contacts new NAME - creates a new contact")
printer("contacts link NAME CREWID - creates a new contact as a proxy to another crew")
printer("contacts rm NAME - removes a contact")
printer("contacts update NAME - updates the description of a contact")
printer("contacts info NAME - lists description of contact")
})
}
func (cmd contactsCmd) Execute(args []string) error {
if activeCrewID == 0 {
return nil
}
if args[0] == "select" {
return cmd.cmdSelect(args[1:])
} else if args[0] == "new" {
return cmd.cmdNew(args[1:])
} else if args[0] == "link" {
return cmd.cmdLink(args[1:])
} else if args[0] == "rm" {
return cmd.cmdRm(args[1:])
} else if args[0] == "update" {
return cmd.cmdUpdate(args[1:])
} else if args[0] == "info" {
return cmd.cmdInfo(args[1:])
}
return nil
}
func (contactsCmd) cmdSelect(args []string) error {
if args[0] == "_" {
activeContactID = 0
updateSidebar()
return nil
}
var hit contact
filter := contact{OwnerID: activeCrewID, Name: args[0]}
database.Where(&filter).First(&hit)
activeChatID = 0
activeContactID = hit.ID
updateSidebar()
outputView.SetOrigin(0, 0)
outputView.SetCursor(0, 0)
outputView.Clear()
mails := hit.fetchSpacemail()
for _, mail := range mails {
mail.print(outputView)
}
return nil
}
func (cmd contactsCmd) cmdNew(args []string) error {
cmd.Mode = "new"
cmd.Name = args[0]
var casted cmdlinesink = cmd
inputfocus = &casted
output(func(printer printer) {
printer("Description?")
})
return nil
}
func (contactsCmd) cmdLink(args []string) error {
destinationid, _ := strconv.Atoi(args[1])
newContact := contact{OwnerID: activeCrewID, Name: args[0], CrewID: uint(destinationid)}
database.Create(&newContact)
updateSidebar()
return nil
}
func (contactsCmd) cmdRm(args []string) error {
var hit contact
filter := contact{OwnerID: activeCrewID, Name: args[0]}
database.Where(&filter).First(&hit)
database.Delete(&hit)
updateSidebar()
return nil
}
func (cmd contactsCmd) cmdUpdate(args []string) error {
var hit contact
filter := contact{OwnerID: activeCrewID, Name: args[0]}
database.Where(&filter).First(&hit)
cmd.Mode = "update"
cmd.ContactID = hit.ID
var casted cmdlinesink = cmd
inputfocus = &casted
output(func(printer printer) {
printer("Description?")
})
return nil
}
func (contactsCmd) cmdInfo(args []string) error {
var hit contact
filter := contact{OwnerID: activeCrewID, Name: args[0]}
database.Where(&filter).First(&hit)
output(func(printer printer) {
printer(hit.Description)
})
return nil
}
func (cmd contactsCmd) TextEntered(data string) error {
if cmd.Mode == "new" {
output(func(printer printer) {
printer(data)
})
contact := contact{OwnerID: activeCrewID, Name: cmd.Name, Description: data}
database.Create(&contact)
updateSidebar()
} else if cmd.Mode == "update" {
output(func(printer printer) {
printer(data)
})
var contact contact
database.First(&contact, cmd.ContactID)
contact.Description = data
database.Save(&contact)
}
return nil
}