diff --git a/operation/chunk.go b/operation/chunk.go index f631111..484523a 100644 --- a/operation/chunk.go +++ b/operation/chunk.go @@ -1,6 +1,10 @@ package operation -import "github.com/aziis98/cabret" +import ( + "log" + + "github.com/aziis98/cabret" +) func init() { registerType("chunk", &Chunk{}) @@ -8,13 +12,13 @@ func init() { // Chunk is a list operation that will group incoming items in groups of the given size type Chunk struct { - Count int + Size int SkipRemaining bool } func (op *Chunk) Configure(options map[string]any) error { var err error - op.Count, err = getKey[int](options, "size") + op.Size, err = getKey[int](options, "size") if err != nil { return err } @@ -27,16 +31,29 @@ func (op *Chunk) Configure(options map[string]any) error { } func (op *Chunk) ProcessList(items []cabret.Content) ([]cabret.Content, error) { - totalPages := len(items) / op.Count + totalChunks := len(items) / op.Size - chunks := make([][]cabret.Content, totalPages, totalPages+1) + chunks := make([][]cabret.Content, totalChunks, totalChunks+1) - for i := 0; i < totalPages; i++ { - chunks = append(chunks, items[i*op.Count:(i+1)*op.Count]) + for i := 0; i < totalChunks; i++ { + chunks = append(chunks, items[i*op.Size:(i+1)*op.Size]) } if !op.SkipRemaining { - chunks = append(chunks, items[totalPages*op.Count:]) + if len(items)%op.Size != 0 { + lastExtraChunk := items[totalChunks*op.Size:] + chunks = append(chunks, lastExtraChunk) + + if len(lastExtraChunk) != len(items)%op.Size { + panic("ehm last chunk and modulus should be equal") + } + + log.Printf(`[operation.Chunk] chunked items in %d chunk(s), last with %d items`, totalChunks+1, len(lastExtraChunk)) + } else { + log.Printf(`[operation.Chunk] chunked items in %d chunk(s)`, totalChunks) + } + } else { + log.Printf(`[operation.Chunk] chunked items in %d chunk(s)`, totalChunks) } result := make([]cabret.Content, len(chunks)) @@ -44,9 +61,9 @@ func (op *Chunk) ProcessList(items []cabret.Content) ([]cabret.Content, error) { result[i] = cabret.Content{ Type: cabret.MetadataOnly, Metadata: cabret.Map{ - "Page": i + 1, - "TotalPages": totalPages, - "Items": chunk, + "Index": i, + "Total": totalChunks, + "Items": chunk, }, } } diff --git a/operation/program.go b/operation/program.go index 722c1a0..5cb1497 100644 --- a/operation/program.go +++ b/operation/program.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io" + "log" "os/exec" "github.com/aziis98/cabret" @@ -117,6 +118,8 @@ func (op *Program) ProcessItem(item cabret.Content) (*cabret.Content, error) { return nil, err } + log.Printf(`[operation.Program] running external program "%s" with "%s" input`, op.ShellCommand, op.IOFormat) + cmd := exec.Command("sh", "-c", op.ShellCommand) cmd.Stdin = r var buf bytes.Buffer diff --git a/operation/slice.go b/operation/slice.go index ac93172..8aac759 100644 --- a/operation/slice.go +++ b/operation/slice.go @@ -1,6 +1,8 @@ package operation import ( + "log" + "github.com/aziis98/cabret" ) @@ -42,9 +44,13 @@ func (op *Slice) ProcessList(items []cabret.Content) ([]cabret.Content, error) { to = to + len(items) + 1 } if from < 0 { - reverse(items) from = from + len(items) + 1 from, to = to, from + reverse(items) + + log.Printf(`[operation.Slice] slicing and reversing items from %d to %d`, from, to) + } else { + log.Printf(`[operation.Slice] slicing items from %d to %d`, from, to) } return items[from:to], nil diff --git a/operation/template.go b/operation/template.go index a73efe6..18f6210 100644 --- a/operation/template.go +++ b/operation/template.go @@ -5,6 +5,7 @@ import ( "fmt" "html/template" "io" + "log" "github.com/aziis98/cabret" ) @@ -41,6 +42,8 @@ func (op *Template) ProcessList(items []cabret.Content) ([]cabret.Content, error tmpl := t.String() + log.Printf(`[operation.Template] rendering template`) + var data bytes.Buffer switch op.Engine { case "html":