Files
cheat/internal/sheets/filter.go
Christopher Allen Lane cc85a4bdb1 chore: bump version to 4.5.0
Bug fixes:
- Fix inverted pager detection logic (returned error instead of path)
- Fix repo.Clone ignoring destination directory parameter
- Fix sheet loading using append on pre-sized slices
- Clean up partial files on copy failure
- Trim whitespace from editor config

Security:
- Add path traversal protection for cheatsheet names

Performance:
- Move regex compilation outside search loop
- Replace string concatenation with strings.Join in search

Build:
- Remove go:generate; embed config and usage as string literals
- Parallelize release builds
- Add fuzz testing infrastructure

Testing:
- Improve test coverage from 38.9% to 50.2%
- Add fuzz tests for search, filter, tags, and validation

Documentation:
- Fix inaccurate code examples in HACKING.md
- Add missing --conf and --all options to man page
- Add ADRs for path traversal, env parsing, and search parallelization
- Update CONTRIBUTING.md to reflect project policy

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-14 19:56:19 -05:00

56 lines
1.4 KiB
Go

package sheets
import (
"strings"
"unicode/utf8"
"github.com/cheat/cheat/internal/sheet"
)
// Filter filters cheatsheets that do not match `tag(s)`
func Filter(
cheatpaths []map[string]sheet.Sheet,
tags []string,
) []map[string]sheet.Sheet {
// buffer a map of filtered cheatsheets
filtered := make([]map[string]sheet.Sheet, 0, len(cheatpaths))
// iterate over each cheatpath
for _, cheatsheets := range cheatpaths {
// create a map of cheatsheets for each cheatpath. The filtering will be
// applied to each cheatpath individually.
pathFiltered := make(map[string]sheet.Sheet)
// iterate over each cheatsheet that exists on each cheatpath
for title, sheet := range cheatsheets {
// assume that the sheet should be kept (ie, should not be filtered)
keep := true
// iterate over each tag. If the sheet does not match *all* tags, filter
// it out.
for _, tag := range tags {
trimmed := strings.TrimSpace(tag)
if trimmed == "" || !utf8.ValidString(trimmed) || !sheet.Tagged(trimmed) {
keep = false
}
}
// if the sheet does match all tags, it passes the filter
if keep {
pathFiltered[title] = sheet
}
}
// the sheets on this individual cheatpath have now been filtered. Now,
// store those alongside the sheets on the other cheatpaths that also made
// it passed the filter.
filtered = append(filtered, pathFiltered)
}
// return the filtered cheatsheets on all paths
return filtered
}