-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommand.go
More file actions
117 lines (102 loc) · 2.15 KB
/
command.go
File metadata and controls
117 lines (102 loc) · 2.15 KB
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
package main
import (
"errors"
"regexp"
)
type CommandType uint8
const (
COMMAND_TIMELINE = iota + 1
COMMAND_MENTIONS
COMMAND_LIST
COMMAND_TWEET
COMMAND_REPLY
COMMAND_FAVORITE
COMMAND_RETWEET
COMMAND_NOT_FOUND
COMMAND_QUIT
)
var re = regexp.MustCompile(`:([a-z]*)\s?(.*)`)
type Command struct {
viewMode ViewMode
slug string
reload bool
}
func NewCommand(options *Options) *Command {
return &Command{
viewMode: options.GetViewMode(),
slug: options.GetSlug(),
reload: options.Reload,
}
}
func (command *Command) GetViewMode() ViewMode {
return command.viewMode
}
func (command *Command) SetViewMode(viewMode ViewMode) {
command.viewMode = viewMode
}
func (command *Command) GetViewModeAsString() string {
switch command.viewMode {
case MODE_TIMELINE:
return "Timeline"
case MODE_MENTION:
return "Mentions"
case MODE_LIST:
return "List: " + command.slug
}
return ""
}
func (command *Command) GetSlug() string {
return command.slug
}
func (command *Command) SetSlug(slug string) {
command.slug = slug
}
func (command *Command) IsReloadable() bool {
return command.reload
}
func (command *Command) Parse(value []byte) (CommandType, string, error) {
var (
commandType CommandType
err error
)
result := re.FindAllStringSubmatch(string(value), -1)
action := result[0][1]
arg := result[0][2]
switch action {
case "tl":
commandType = COMMAND_TIMELINE
if arg != "" {
err = errors.New("Argument is not required")
}
case "mentions":
commandType = COMMAND_MENTIONS
if arg != "" {
err = errors.New("Argument is not required")
}
case "list":
commandType = COMMAND_LIST
if arg == "" {
err = errors.New("List name is required")
}
case "tweet", "t":
commandType = COMMAND_TWEET
if arg == "" {
err = errors.New("text is required")
}
case "reply", "r":
commandType = COMMAND_REPLY
if arg == "" {
err = errors.New("text is required")
}
case "fav", "f":
commandType = COMMAND_FAVORITE
case "rt":
commandType = COMMAND_RETWEET
case "q":
commandType = COMMAND_QUIT
default:
commandType = COMMAND_NOT_FOUND
err = errors.New("Command is not found")
}
return commandType, arg, err
}