Skip to content

Commit

Permalink
Add threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo-Hafsaoui committed Nov 23, 2024
1 parent 39fb2d3 commit 229cfd1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ RUN go mod download && go mod verify
COPY . .
RUN make build

CMD ["./anemon", "generate"]
CMD ["./anemon", "-g"]
24 changes: 0 additions & 24 deletions cmd/generate.go

This file was deleted.

38 changes: 31 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,41 @@ package cmd
import (
"os"
"github.com/spf13/cobra"
"anemon/internal/adapters/input"
)

var rootCmd = &cobra.Command{
Use: "anemon",
Short: "a CV genrator",
Long: `This CLI tool, written in Go, automates the generation of customized CVs from Markdown files based on a specified configuration. It parses CV sections in
multiple languages, prioritizes key skills or features as defined in an output.yml file, and outputs LaTeX files for each CV version, ready for compilation.`,
Use: "anemon",
Short: "A CV generator",
Long: `This CLI tool, automates the generation of customized CVs from Markdown files based on a specified configuration.`,
RunE: func(cmd *cobra.Command, args []string) error {
threshold, err := cmd.Flags().GetInt("threshold")
if err != nil {
return err
}
input.ChangeOverflowThreshold(threshold)

generate, err := cmd.Flags().GetBool("generate")
if err != nil {
return err
}
if generate {
root, err := os.Getwd()
if err != nil {
return err
}
return input.GenerateCVFromMarkDownToLatex(root)
}

return nil
},
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
rootCmd.Flags().IntP("threshold", "t", 1, "Set the page overflow threshold (default 1)")
rootCmd.Flags().BoolP("generate", "g", false, "Generate a CV")
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}

5 changes: 5 additions & 0 deletions internal/adapters/input/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ func GenerateCVFromMarkDownToLatex(root string)error{
service := builder.GetService()
return service.GenerateTemplates()
}

//Change the threshold for the regeration of the PDF
func ChangeOverflowThreshold(newThreshold int){
core.SetOverflowThreshold(newThreshold)
}
30 changes: 27 additions & 3 deletions internal/core/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ package core
import (
"log/slog"
"fmt"
"sync"
"sort"
"strings"
)
const THESHOLD_PAGEOVERFLOW = 1

var TresholdPageOverFlow struct {
value int
mutex sync.Mutex
}

type CVService struct{
root string
Expand Down Expand Up @@ -46,12 +51,13 @@ func (s *CVService) GenerateTemplates() error {

//Compile the CV into PDF, and if they are too long regenrate theme with one less section
func (s *CVService) compileWithOverflowHandling(cvs []CV, params Params, template string) error {
threshold := GetOverflowThreshold()
maxNbPage, err := s.compiler.CompileTemplate(s.root)
if err != nil {
return fmt.Errorf("failed to compile template: %w", err)
}
for maxNbPage > THESHOLD_PAGEOVERFLOW {
slog.Info("Page overflow detected; adjusting layout and regenerating CVs")
for maxNbPage > threshold {
slog.Warn("Page overflow detected; adjusting layout and regenerating CVs")
if err := s.generateAllCVs(cvs, params, template, true); err != nil {
return err
}
Expand Down Expand Up @@ -207,3 +213,21 @@ func (s *BuilderService) GetService() CVService {
compiler: s.compiler,
}
}

// Set the threshold value dynamically
func SetOverflowThreshold(newThreshold int) {
TresholdPageOverFlow.mutex.Lock()
defer TresholdPageOverFlow.mutex.Unlock()
TresholdPageOverFlow.value = newThreshold
}

// Get the current threshold value
func GetOverflowThreshold() int {
TresholdPageOverFlow.mutex.Lock()
defer TresholdPageOverFlow.mutex.Unlock()
return TresholdPageOverFlow.value
}

func init() {
TresholdPageOverFlow.value = 1
}

0 comments on commit 229cfd1

Please sign in to comment.