Skip to content

Commit

Permalink
Checkpoint logic completed + one bug
Browse files Browse the repository at this point in the history
  • Loading branch information
insyri committed Jan 1, 2024
1 parent b690e72 commit c65f2cb
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 42 deletions.
2 changes: 1 addition & 1 deletion cmd/lesson.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var lessonCmd = &cobra.Command{
if n > len(lesson.Master.Lessons) || n == 0 {
util.LogErrorAndExit(util.NonexistentLesson)
}
err = lesson.Master.Run(n, 0)
err = lesson.Master.Run(n, 0, true)
if err != nil {
util.LogErrorAndExit(err)
}
Expand Down
54 changes: 21 additions & 33 deletions lesson/lesson.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"csclub-activities/util"
"fmt"
"github.com/fatih/color"
"github.com/mitchellh/go-wordwrap"
)

const WordWrapLimit = 100
Expand All @@ -23,7 +22,15 @@ type master struct {
Lessons []Lesson
}

func (m *master) Run(lesson int, checkpoint int) error {
// on program startup, it checks whether there was a previously saved checkpoint.
// if there was, it checks if the condition to pass that checkpoint is valid. (ShouldPromote)
// if true, then the next checkpoint is displayed. otherwise, the same checkpoint is displayed.

// Run
// setManually describes whether the checkpoint was loaded from a file or not.
// if it was not loaded (set manually), we shouldn't skip the current checkpoint
// (in other words, don't check the current checkpoint's ShouldPromote)
func (m *master) Run(lesson int, checkpoint int, setManually bool) error {

if m.Lessons == nil ||
lesson >= len(m.Lessons) {
Expand All @@ -36,28 +43,20 @@ func (m *master) Run(lesson int, checkpoint int) error {
}

cps := m.Lessons[lesson].Checkpoints
// TODO: remove this for loop for a single run conditional statement
for i := checkpoint; i < len(cps); i++ {
if cps[i].ShouldPromote() {
if i+1 < len(cps) {
break
}
i++
continue
}
if !setManually && cps[checkpoint].ShouldPromote() && util.HasNext(checkpoint, cps) {
checkpoint++
}

fmt.Printf("%-5s %s\n\n",
color.New(color.Bold).Add(color.FgGreen).Sprintf("%2d/%-2d", i+1, len(cps)),
color.New(color.Bold).Sprintf("%s", cps[i].Title),
)
cps[i].Action()
fmt.Println("\n Note: to get back to the help screen, run: `csa help`")
fmt.Printf("%-5s %s\n\n",
color.New(color.Bold).Add(color.FgGreen).Sprintf("%2d/%-2d", checkpoint+1, len(cps)),
color.New(color.Bold).Sprintf("%s", cps[checkpoint].Title),
)
cps[checkpoint].Action()
fmt.Println("\n Note: to get back to the help screen, run: `csa help`")

err := util.Save(uint8(lesson), uint8(checkpoint))
if err != nil {
return err
}
break
err := util.Save(uint8(lesson), uint8(checkpoint))
if err != nil {
return err
}
return nil
}
Expand All @@ -81,14 +80,3 @@ type ExpectantError struct {
MatchesError func() bool
Feedback string
}

func printPrompts(prompts []string) {
for x := range prompts {
if x == 0 {
fmt.Println(wordwrap.WrapString(prompts[x], WordWrapLimit))
} else {
fmt.Println("\n" + wordwrap.WrapString(prompts[x], WordWrapLimit))
}
}

}
23 changes: 16 additions & 7 deletions lesson/one.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package lesson

import (
"csclub-activities/util"
"fmt"
"github.com/mitchellh/go-wordwrap"
"os"
"regexp"
"runtime"
)

var lessonOne = &Lesson{
Expand Down Expand Up @@ -34,18 +37,24 @@ var lessonOne = &Lesson{
"If you come back to the same checkpoint, it means that you haven't met the conditions to move" +
" on to the next checkpoint. Ensure that you:",

" 1. Have a version of Bash installed on your system\n" +
" 2. Are running and using Bash at this moment" +
" 3. Are executing this program (csa) within Bash",
orderedList([]string{
"Have a version of Bash installed on your system",
"Are running and using Bash at this moment",
"[and] Are executing this program (csa) within Bash",
}),

conditional(runtime.GOOS != "windows",
"If you are having trouble with this step because you are running a custom shell"+
" (ex. fish), consider populating the `VALIDSHELL` environment variable before running csa again."),
})
},
ShouldPromote: func() bool {
switch os.Getenv("SHELL") {
case "/usr/bin/bash", "/usr/bin/zsh", "/usr/bin/sh":
return true
matched, err := regexp.MatchString(`usr([/\\])bin([/\\])(ba|z|)sh`, os.Getenv("SHELL"))
if err != nil {
util.LogErrorAndExit(err)
}
// for those who run other shells like fish, you could just use the env VALIDSHELL
return os.Getenv("VALIDSHELL") != ""
return matched || os.Getenv("VALIDSHELL") != ""
},
},
{
Expand Down
41 changes: 41 additions & 0 deletions lesson/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package lesson

import (
"fmt"
"github.com/mitchellh/go-wordwrap"
"strings"
)

func printPrompts(prompts []string) {
for i := range prompts {
if prompts[i] == "" {
continue
}

n := "\n"
if i == 0 {
n = ""
}
fmt.Println(n + wordwrap.WrapString(prompts[i], WordWrapLimit))
}
}

func orderedList(prompts []string) string {
var s strings.Builder
for i := range prompts {
// after wrapping, add indents to any places where it wrapped
x := wordwrap.WrapString(
fmt.Sprintf(" %d. %s", i+1, prompts[i]),
WordWrapLimit)
x = strings.ReplaceAll(x, "\n", "\n ") + "\n"
s.WriteString(x)
}
return strings.TrimSuffix(s.String(), "\n")
}

func conditional(f bool, prompt string) string {
if f {
return prompt
}
return ""
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func main() {
return
}

err = lesson.Master.Run(int(information.LessonId), int(information.CheckpointID))
err = lesson.Master.Run(int(information.LessonId), int(information.CheckpointID), false)
if err != nil {
util.LogErrorAndExit(err)
}
Expand Down
4 changes: 4 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ func LogErrorAndExit(err error) {
_, _ = c.Printf("Error: %v\n", err)
os.Exit(1)
}

func HasNext[T any](position int, set []T) bool {
return position+1 < len(set)
}

0 comments on commit c65f2cb

Please sign in to comment.