/
prompts.go
152 lines (118 loc) · 4.04 KB
/
prompts.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
143
144
145
146
147
148
149
150
151
152
package editor
import (
"fmt"
"github.com/Songmu/prompter"
"github.com/shelepuginivan/hakutest/internal/pkg/test"
)
func promptAddAttachment() test.Attachment {
attachment := test.Attachment{}
attachment.Name = prompter.Prompt(nestedMessage("Name", 2), "")
attachment.Type = prompter.Choose(
nestedMessage("Type", 2),
[]string{"image", "video", "audio", "file"},
"file",
)
src := prompter.Prompt(nestedMessage("Source (URL or local file)", 2), "")
attachmentSrc, err := getAttachmentSrc(src)
for err != nil && prompter.YN(secondaryMessage("Failed to add attachment! Try again?"), false) {
src = prompter.Prompt(nestedMessage("Source (URL or local file)", 2), "")
attachmentSrc, err = getAttachmentSrc(src)
}
attachment.Src = attachmentSrc
return attachment
}
func promptEditAttachment(attachment test.Attachment) test.Attachment {
attachment.Name = prompter.Prompt(nestedMessage("Name", 2), attachment.Name)
attachment.Type = prompter.Choose(
nestedMessage("Type", 2),
[]string{"image", "video", "audio", "file"},
attachment.Type,
)
if prompter.YN(secondaryMessage("Edit attachment source"), false) {
src := prompter.Prompt(nestedMessage("Source (URL or local file)", 2), "")
attachmentSrc, err := getAttachmentSrc(src)
for err != nil && prompter.YN(secondaryMessage("Failed to add attachment! Try again?"), false) {
src = prompter.Prompt(nestedMessage("Source (URL or local file)", 2), "")
attachmentSrc, err = getAttachmentSrc(src)
}
attachment.Src = attachmentSrc
}
return attachment
}
func promptNewTask() test.Task {
task := test.Task{}
taskType := map[string]string{
"Single answer": "single",
"Multiple answers": "multiple",
"Open question": "open",
}
task.Type = taskType[prompter.Choose(
secondaryMessage("Type of the task"),
[]string{"Single answer", "Multiple answers", "Open question"},
"Open question",
)]
task.Text = prompter.Prompt(secondaryMessage("Task text"), "")
fmt.Println(secondaryMessage("Answer options:"))
option := prompter.Prompt(nestedMessage("Add option (leave blank to stop)", 2), "")
for option != "" {
task.Options = append(task.Options, option)
option = prompter.Prompt(nestedMessage("Add option (leave blank to stop)", 2), "")
}
task.Answer = prompter.Prompt(secondaryMessage("Correct answer"), "")
if prompter.YN(secondaryMessage("Add attachment"), false) {
task.Attachment = promptAddAttachment()
}
return task
}
func promptEditTask(task test.Task) test.Task {
taskTypeName := map[string]string{
"single": "Single answer",
"multiple": "Multiple answers",
"open": "Open question",
}
taskType := map[string]string{
"Single answer": "single",
"Multiple answers": "multiple",
"Open question": "open",
}
task.Type = taskType[prompter.Choose(
secondaryMessage("Type of the task"),
[]string{"Single answer", "Multiple answers", "Open question"},
taskTypeName[task.Type],
)]
task.Text = prompter.Prompt(secondaryMessage("Task text"), task.Text)
fmt.Println(secondaryMessage("Answer options:"))
for i := 0; i < len(task.Options); {
option := task.Options[i]
switch prompter.Choose(
nestedMessage(fmt.Sprintf("%d) %s", i+1, option), 2),
[]string{"leave unchanged", "remove", "edit"},
"leave unchanged",
) {
case "remove":
task.Options = append(task.Options[:i], task.Options[i+1:]...)
case "edit":
task.Options[i] = prompter.Prompt(nestedMessage("Answer option", 3), option)
i++
case "leave unchanged":
i++
}
}
option := prompter.Prompt(nestedMessage("Add option (leave blank to stop)", 2), "")
for option != "" {
task.Options = append(task.Options, option)
option = prompter.Prompt(nestedMessage("Add option (leave blank to stop)", 2), "")
}
task.Answer = prompter.Prompt(secondaryMessage("Correct answer"), task.Answer)
switch prompter.Choose(
secondaryMessage("Task attachment"),
[]string{"leave unchanged", "edit", "remove"},
"leave unchanged",
) {
case "edit":
task.Attachment = promptEditAttachment(task.Attachment)
case "remove":
task.Attachment = test.Attachment{}
}
return task
}