-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremovetopic.go
More file actions
93 lines (77 loc) · 2.31 KB
/
Copy pathremovetopic.go
File metadata and controls
93 lines (77 loc) · 2.31 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
package main
import (
"os"
persistence "primotibalt/checkTests/topicpersistence"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/x/term"
)
type RemoveTopicModel struct {
RemoveTopicForm *huh.Form
UserAborted bool
}
func (m RemoveTopicModel) Init() tea.Cmd {
return tea.ClearScreen
}
func (m RemoveTopicModel) Update(msg tea.Msg) (model tea.Model, cmd tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC, tea.KeyEsc:
m.UserAborted = true
return m, tea.Batch(tea.ClearScreen, tea.Quit)
case tea.KeyEnter:
return m, tea.Batch(tea.ClearScreen, tea.Quit)
}
}
_, cmd = m.RemoveTopicForm.Update(msg)
model = m
return
}
func (m RemoveTopicModel) View() string {
return m.RemoveTopicForm.View()
}
func RemoveTopic(topics []persistence.TopicName, topicToQa map[persistence.TopicName][]string) {
width, height, err := term.GetSize(os.Stdout.Fd())
if err != nil {
panic(err)
}
topicsOptions := make([]huh.Option[persistence.TopicName], len(topics))
for ptr, topic := range topics {
topicsOptions[ptr] = huh.NewOption(string(topic), topic)
}
var topicToRemove persistence.TopicName
selectTopicPanel := huh.NewSelect[persistence.TopicName]().Options(
topicsOptions...).
Value(&topicToRemove)
questionsPanel := huh.NewNote().DescriptionFunc(func() string {
return GetQuestionsFromTopics(topicToRemove, topicToQa)
}, &topicToRemove)
selectTopicGroup := huh.NewGroup(selectTopicPanel, questionsPanel).WithWidth(width).WithHeight(height)
form := huh.NewForm(selectTopicGroup).WithWidth(width).WithHeight(height - Padding)
model := RemoveTopicModel{
form,
false,
}
m, err := tea.NewProgram(model).Run()
if err != nil {
panic(err)
}
MoveCursorToTopLeft()
if m.(RemoveTopicModel).UserAborted {
return
}
if len(topicToRemove) > 0 {
var userConfirmed bool
huh.NewConfirm().Value(&userConfirmed).
Affirmative("Удалить").Negative("Отмена").
Description(GetQuestionsFromTopics(topicToRemove, topicToQa)).
Title("Вы уверены что хотите удалить топик \"" + string(topicToRemove) + "\"?").
WithTheme(MakeThemeCenterInnerElement(huh.ThemeCharm())).
Run()
MoveCursorToTopLeft()
if userConfirmed {
persistence.RemoveTopic(topicToRemove)
}
}
}