Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions internal/csv/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,27 +179,29 @@ func createMultipleChoiceQuestionFromAnswers(question, correctAnswer string, all
func getRandomDistractors(correctAnswer string, allAnswers []string, seed int) []string {
r := rand.New(rand.NewSource(time.Now().UnixNano() + int64(seed*13)))

var distractors []string
var otherAnswers []string
unique := make(map[string]struct{})

for _, answer := range allAnswers {
if answer != correctAnswer {
otherAnswers = append(otherAnswers, answer)
if _, exists := unique[answer]; !exists {
unique[answer] = struct{}{}
otherAnswers = append(otherAnswers, answer)
}
}
}

if len(otherAnswers) > 0 {
r.Shuffle(len(otherAnswers), func(i, j int) {
otherAnswers[i], otherAnswers[j] = otherAnswers[j], otherAnswers[i]
})
if len(otherAnswers) == 0 {
return []string{}
}

maxDistractors := len(otherAnswers)
if maxDistractors > 3 {
maxDistractors = 3
}
r.Shuffle(len(otherAnswers), func(i, j int) {
otherAnswers[i], otherAnswers[j] = otherAnswers[j], otherAnswers[i]
})

distractors = otherAnswers[:maxDistractors]
if len(otherAnswers) > 3 {
otherAnswers = otherAnswers[:3]
}

return distractors
return otherAnswers
}
16 changes: 16 additions & 0 deletions internal/models/quiz.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ func (m Model) HandleMenuUpdate(msg tea.KeyMsg) (Model, tea.Cmd) {
}

func (m Model) HandleQuestionUpdate(msg tea.KeyMsg) (Model, tea.Cmd) {
if len(m.Questions) == 0 {
switch msg.String() {
case "ctrl+c":
return m, tea.Quit
case "q":
m.State = StateMenu
m.Cursor = 0
m.CurrentQ = 0
m.InputText = ""
m.CorrectCount = 0
return m, nil
default:
return m, nil
}
}

switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func RenderEmptyQuestionsError() string {
s += "Please check that your CSV file is properly formatted:\n"
s += "- Each line should have: question,answer\n"
s += "- Questions and answers should not be empty\n\n"
s += lipgloss.NewStyle().Faint(true).Render("q: Exit to main menu")
s += lipgloss.NewStyle().Faint(true).Render("q: Back to menu")

return s
}
Expand Down