refactor survery cmd
move package files to pkg
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/LazyBachelor/LazyPM/cmd/survey/tasks"
|
||||
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/cli/repl"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/tui"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/web"
|
||||
)
|
||||
@@ -20,13 +21,13 @@ func initializeServices(ctx context.Context) (*service.Services, func(), error)
|
||||
return service.NewServices(ctx, config)
|
||||
}
|
||||
|
||||
func initTasks() []*tasks.Task {
|
||||
return []*tasks.Task{
|
||||
func initTasks() []*task.Task {
|
||||
return []*task.Task{
|
||||
tasks.NewCreateIssueTask(),
|
||||
tasks.NewCreateIssueTask(),
|
||||
}
|
||||
}
|
||||
|
||||
func initInterfaces() []tasks.Interface {
|
||||
return []tasks.Interface{repl.NewRepl(), tui.NewTui(), web.NewWeb()}
|
||||
func initInterfaces() []task.Interface {
|
||||
return []task.Interface{repl.NewRepl(), tui.NewTui(), web.NewWeb()}
|
||||
}
|
||||
|
||||
@@ -5,14 +5,12 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/LazyBachelor/LazyPM/cmd/survey/tasks"
|
||||
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||
)
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
ctx := context.Background()
|
||||
|
||||
svc, close, err := initializeServices(ctx)
|
||||
@@ -29,7 +27,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func taskLoop(ctx context.Context, svc *service.Services, surveyTasks []*tasks.Task, interfaces []tasks.Interface) error {
|
||||
func taskLoop(ctx context.Context, svc *service.Services, surveyTasks []*task.Task, interfaces []task.Interface) error {
|
||||
interfaceIndex := rand.Int() % len(interfaces)
|
||||
|
||||
for _, task := range surveyTasks {
|
||||
|
||||
@@ -4,25 +4,26 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/LazyBachelor/LazyPM/cmd/survey/ui"
|
||||
"github.com/LazyBachelor/LazyPM/internal/models"
|
||||
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||
ui "github.com/LazyBachelor/LazyPM/pkg/task/ui"
|
||||
"github.com/charmbracelet/huh"
|
||||
)
|
||||
|
||||
func NewCreateIssueTask() *Task {
|
||||
func NewCreateIssueTask() *task.Task {
|
||||
aboutScreen := ui.NewTaskModel(createIssueDetails())
|
||||
questionnaire := ui.NewQuestionnaireModel(createIssueQuestionnaire())
|
||||
|
||||
task := NewTask(aboutScreen, questionnaire)
|
||||
task := task.NewTask(aboutScreen, questionnaire)
|
||||
task.SetConfigFunc(createIssueConfig)
|
||||
task.SetDbStateFunc(createIssueDbState)
|
||||
task.SetValidateFunc(createIssueValidate)
|
||||
return task
|
||||
}
|
||||
|
||||
func createIssueConfig() TaskConfig {
|
||||
return TaskConfig{
|
||||
func createIssueConfig() task.TaskConfig {
|
||||
return task.TaskConfig{
|
||||
IssuePrefix: "pm",
|
||||
BeadsDBPath: "./.pm/db.db",
|
||||
StatisticsStoragePath: "./.pm/task-1-stats.json",
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
func (t *Task) IntroduceTask() error {
|
||||
if t.aboutScreen == nil {
|
||||
return fmt.Errorf("aboutScreen is not set")
|
||||
}
|
||||
_, err := tea.NewProgram(t.aboutScreen, tea.WithAltScreen()).Run()
|
||||
return err
|
||||
}
|
||||
|
||||
func (t *Task) StartInterface(ctx context.Context, cfg service.Config) error {
|
||||
if t.interfaceType == nil {
|
||||
return fmt.Errorf("interfaceType is not set")
|
||||
}
|
||||
|
||||
return t.interfaceType.Run(ctx, cfg)
|
||||
}
|
||||
|
||||
func (t *Task) StartQuestionnaire() error {
|
||||
if t.questionnaire == nil {
|
||||
return fmt.Errorf("questionnaire is not set")
|
||||
}
|
||||
_, err := tea.NewProgram(t.questionnaire, tea.WithAltScreen()).Run()
|
||||
return err
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
// task.go
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
type Task struct {
|
||||
Config TaskConfig
|
||||
interfaceType Interface
|
||||
aboutScreen tea.Model
|
||||
questionnaire tea.Model
|
||||
|
||||
validateFunc ValidateFunc
|
||||
dbStateFunc DbStateFunc
|
||||
}
|
||||
|
||||
func NewTask(aboutScreen tea.Model, questionnaire tea.Model) *Task {
|
||||
return &Task{
|
||||
aboutScreen: aboutScreen,
|
||||
questionnaire: questionnaire,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Task) SetConfigFunc(fn ConfigFunc) {
|
||||
t.Config = fn()
|
||||
}
|
||||
|
||||
func (t *Task) SetInterface(interfaceType Interface) {
|
||||
t.interfaceType = interfaceType
|
||||
}
|
||||
|
||||
func (t *Task) SetDbStateFunc(fn DbStateFunc) {
|
||||
t.dbStateFunc = fn
|
||||
}
|
||||
|
||||
func (t *Task) SetValidateFunc(fn ValidateFunc) {
|
||||
t.validateFunc = fn
|
||||
}
|
||||
|
||||
func (t *Task) Initialize(ctx context.Context, svc *service.Services) error {
|
||||
if t.dbStateFunc == nil {
|
||||
return fmt.Errorf("dbStateFunc is not set")
|
||||
}
|
||||
return t.dbStateFunc(ctx, svc)
|
||||
}
|
||||
|
||||
func (t *Task) Validate(ctx context.Context, svc *service.Services) (bool, error) {
|
||||
if t.validateFunc == nil {
|
||||
return false, fmt.Errorf("validateFunc is not set")
|
||||
}
|
||||
return t.validateFunc(ctx, svc)
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||
)
|
||||
|
||||
type TaskConfig = service.Config
|
||||
|
||||
type Interface interface {
|
||||
Run(context.Context, TaskConfig) error
|
||||
}
|
||||
|
||||
type ConfigFunc func() TaskConfig
|
||||
type ValidateFunc func(context.Context, *service.Services) (ok bool, err error)
|
||||
type DbStateFunc func(context.Context, *service.Services) error
|
||||
@@ -1,27 +0,0 @@
|
||||
package ui
|
||||
|
||||
import "github.com/charmbracelet/bubbles/key"
|
||||
|
||||
type TaskHelpKeys struct {
|
||||
Quit key.Binding
|
||||
Continue key.Binding
|
||||
}
|
||||
|
||||
var DefaultTaskKeys = TaskHelpKeys{
|
||||
Quit: key.NewBinding(
|
||||
key.WithKeys("q", "ctrl+c"),
|
||||
key.WithHelp("q", "Quit"),
|
||||
),
|
||||
Continue: key.NewBinding(
|
||||
key.WithKeys(" "),
|
||||
key.WithHelp("space", "Continue"),
|
||||
),
|
||||
}
|
||||
|
||||
func (h TaskHelpKeys) ShortHelp() []key.Binding {
|
||||
return []key.Binding{h.Continue, h.Quit}
|
||||
}
|
||||
|
||||
func (h TaskHelpKeys) FullHelp() [][]key.Binding {
|
||||
return [][]key.Binding{{h.Continue, h.Quit}}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"charm.land/lipgloss/v2"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/huh"
|
||||
)
|
||||
|
||||
func NewQuestionnaireModel(questions Questions) *QuestionnaireModel {
|
||||
form := huh.NewForm(questions...).
|
||||
WithTheme(theme()).WithLayout(huh.LayoutGrid(1, 1))
|
||||
|
||||
return &QuestionnaireModel{
|
||||
Questions: questions,
|
||||
form: form,
|
||||
}
|
||||
}
|
||||
|
||||
func (q *QuestionnaireModel) Init() tea.Cmd {
|
||||
return q.form.Init()
|
||||
}
|
||||
|
||||
func (q *QuestionnaireModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.WindowSizeMsg:
|
||||
q.SetSize(msg.Width, msg.Height)
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "q", "ctrl+c":
|
||||
return q, tea.Quit
|
||||
}
|
||||
}
|
||||
|
||||
form, cmd := q.form.Update(msg)
|
||||
if f, ok := form.(*huh.Form); ok {
|
||||
q.form = f
|
||||
}
|
||||
return q, cmd
|
||||
}
|
||||
|
||||
func (q *QuestionnaireModel) View() string {
|
||||
form := lipgloss.NewStyle().
|
||||
Width(q.width).Align(lipgloss.Center).
|
||||
Render(q.form.View())
|
||||
|
||||
return lipgloss.Place(
|
||||
q.width, q.height, lipgloss.Center, lipgloss.Center, form,
|
||||
)
|
||||
}
|
||||
|
||||
func (q *QuestionnaireModel) SetSize(width, height int) {
|
||||
q.width, q.height = width, height
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"github.com/charmbracelet/huh"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
func theme() *huh.Theme {
|
||||
theme := huh.ThemeBase16()
|
||||
|
||||
theme.Focused.Base = lipgloss.NewStyle().Align(lipgloss.Center)
|
||||
|
||||
return theme
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"charm.land/lipgloss/v2"
|
||||
"github.com/charmbracelet/bubbles/help"
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
func NewTaskModel(details TaskDetails) TaskModel {
|
||||
return TaskModel{
|
||||
TaskDetails: details,
|
||||
keys: DefaultTaskKeys,
|
||||
help: help.New(),
|
||||
}
|
||||
}
|
||||
|
||||
func (m TaskModel) Init() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m TaskModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.WindowSizeMsg:
|
||||
m.SetSize(msg.Width, msg.Height)
|
||||
case tea.KeyMsg:
|
||||
switch {
|
||||
case key.Matches(msg, m.keys.Quit):
|
||||
return m, tea.Quit
|
||||
case key.Matches(msg, m.keys.Continue):
|
||||
return m, tea.Quit
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m TaskModel) View() string {
|
||||
padding := 3
|
||||
|
||||
header := lipgloss.NewStyle().
|
||||
PaddingTop(padding).Width(m.width).Align(lipgloss.Center).
|
||||
Bold(true).Render(m.Title)
|
||||
|
||||
headerHeight := lipgloss.Height(header)
|
||||
|
||||
helpView := lipgloss.NewStyle().
|
||||
PaddingBottom(padding).
|
||||
Width(m.width).Align(lipgloss.Center).
|
||||
Render(m.help.View(m.keys))
|
||||
|
||||
helpHeigh := lipgloss.Height(helpView)
|
||||
|
||||
detailsText := fmt.Sprintf("Time to complete: %s | Difficulty: %s", m.TimeToComplete, m.Difficulty)
|
||||
details := lipgloss.NewStyle().Align(lipgloss.Center).
|
||||
Width(m.width).PaddingBottom(1).Render(detailsText)
|
||||
|
||||
detailsHeight := lipgloss.Height(details)
|
||||
|
||||
content := lipgloss.NewStyle().
|
||||
Width(m.width).Height(m.height-headerHeight-helpHeigh-detailsHeight).
|
||||
Align(lipgloss.Center, lipgloss.Center).
|
||||
Render(m.Description)
|
||||
|
||||
return lipgloss.JoinVertical(lipgloss.Top, header, content, details, helpView)
|
||||
|
||||
}
|
||||
|
||||
func (m *TaskModel) SetSize(width, height int) {
|
||||
m.width, m.height = width, height
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"github.com/charmbracelet/bubbles/help"
|
||||
"github.com/charmbracelet/huh"
|
||||
)
|
||||
|
||||
type TaskDetails struct {
|
||||
Title string
|
||||
Description string
|
||||
TimeToComplete string
|
||||
Difficulty string
|
||||
}
|
||||
|
||||
type TaskModel struct {
|
||||
TaskDetails
|
||||
keys TaskHelpKeys
|
||||
help help.Model
|
||||
width, height int
|
||||
}
|
||||
|
||||
type Questions []*huh.Group
|
||||
|
||||
type QuestionnaireModel struct {
|
||||
Questions
|
||||
form *huh.Form
|
||||
width, height int
|
||||
}
|
||||
Reference in New Issue
Block a user