tag : private methods : get used to camelCase
This commit is contained in:
parent
41b01c172b
commit
2505bd4cf9
1 changed files with 35 additions and 35 deletions
70
tag/tag.go
70
tag/tag.go
|
|
@ -143,7 +143,7 @@ func (s Set) Intersect(t Set) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// reads files from stdin if present, otherwise from the glob pattern:
|
// reads files from stdin if present, otherwise from the glob pattern:
|
||||||
func Filelist(glob string) []string {
|
func getFilelist(glob string) []string {
|
||||||
filelist, err := pipe.GetStdin()
|
filelist, err := pipe.GetStdin()
|
||||||
// otherwise get from the glob:
|
// otherwise get from the glob:
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -156,7 +156,7 @@ func Filelist(glob string) []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: only accepts one kind of syntax at a time
|
// TODO: only accepts one kind of syntax at a time
|
||||||
func ParseQuery(query string) Query {
|
func parseQuery(query string) Query {
|
||||||
// initialize for the single tag case:
|
// initialize for the single tag case:
|
||||||
q := Query{
|
q := Query{
|
||||||
Op: SINGLE,
|
Op: SINGLE,
|
||||||
|
|
@ -181,13 +181,13 @@ func ParseQuery(query string) Query {
|
||||||
return q
|
return q
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseHeader(content *string) string {
|
func parseHeader(content *string) string {
|
||||||
// returns complete string if not found:
|
// returns complete string if not found:
|
||||||
header, _, _ := strings.Cut(*content, "\n\n")
|
header, _, _ := strings.Cut(*content, "\n\n")
|
||||||
return header
|
return header
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseTags(content *string) (tags []string) {
|
func parseTags(content *string) (tags []string) {
|
||||||
res := tagRegexp.FindAllStringSubmatch(*content, -1)
|
res := tagRegexp.FindAllStringSubmatch(*content, -1)
|
||||||
for i := range res {
|
for i := range res {
|
||||||
// group submatch is indexed at 1:
|
// group submatch is indexed at 1:
|
||||||
|
|
@ -197,7 +197,7 @@ func ParseTags(content *string) (tags []string) {
|
||||||
return tags
|
return tags
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseDate(content *string) (time.Time, error) {
|
func parseDate(content *string) (time.Time, error) {
|
||||||
res := dateRegexp.FindStringSubmatch(*content)
|
res := dateRegexp.FindStringSubmatch(*content)
|
||||||
if len(res) < 2 {
|
if len(res) < 2 {
|
||||||
return time.Time{}, errors.New("failed to find date string")
|
return time.Time{}, errors.New("failed to find date string")
|
||||||
|
|
@ -205,11 +205,11 @@ func ParseDate(content *string) (time.Time, error) {
|
||||||
return time.Parse(DATE_FORMAT, res[1])
|
return time.Parse(DATE_FORMAT, res[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseContent(filename string, content *string) Entry {
|
func parseContent(filename string, content *string) Entry {
|
||||||
base := filepath.Base(filename)
|
base := filepath.Base(filename)
|
||||||
header := ParseHeader(content)
|
header := parseHeader(content)
|
||||||
date, _ := ParseDate(&header)
|
date, _ := parseDate(&header)
|
||||||
tags := ParseTags(&header)
|
tags := parseTags(&header)
|
||||||
return Entry{
|
return Entry{
|
||||||
base,
|
base,
|
||||||
date,
|
date,
|
||||||
|
|
@ -218,7 +218,7 @@ func ParseContent(filename string, content *string) Entry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Entries(filelist []string) []Entry {
|
func getEntries(filelist []string) []Entry {
|
||||||
// NOTE: size 0, capacity specified:
|
// NOTE: size 0, capacity specified:
|
||||||
entries := make([]Entry, 0, len(filelist))
|
entries := make([]Entry, 0, len(filelist))
|
||||||
for _, f := range filelist {
|
for _, f := range filelist {
|
||||||
|
|
@ -227,14 +227,14 @@ func Entries(filelist []string) []Entry {
|
||||||
log.Fatal(fmt.Errorf("gag: error opening file: %s\n%w", f, err))
|
log.Fatal(fmt.Errorf("gag: error opening file: %s\n%w", f, err))
|
||||||
}
|
}
|
||||||
s := string(dat)
|
s := string(dat)
|
||||||
e := ParseContent(f, &s)
|
e := parseContent(f, &s)
|
||||||
entries = append(entries, e)
|
entries = append(entries, e)
|
||||||
}
|
}
|
||||||
return entries
|
return entries
|
||||||
}
|
}
|
||||||
|
|
||||||
// maps tags to a set of filenames
|
// maps tags to a set of filenames
|
||||||
func Tagmap(entries []Entry) map[string]Set {
|
func makeTagmap(entries []Entry) map[string]Set {
|
||||||
tagmap := map[string]Set{}
|
tagmap := map[string]Set{}
|
||||||
for _, e := range entries {
|
for _, e := range entries {
|
||||||
for _, tag := range e.tags {
|
for _, tag := range e.tags {
|
||||||
|
|
@ -249,7 +249,7 @@ func Tagmap(entries []Entry) map[string]Set {
|
||||||
}
|
}
|
||||||
|
|
||||||
// shrinks the entries to only include files within a date range.
|
// shrinks the entries to only include files within a date range.
|
||||||
func Date(entries []Entry, date string) []Entry {
|
func dateRange(entries []Entry, date string) []Entry {
|
||||||
// deleting from the old slice would be less efficient than appending to a new one:
|
// deleting from the old slice would be less efficient than appending to a new one:
|
||||||
ranged := make([]Entry, 0, len(entries))
|
ranged := make([]Entry, 0, len(entries))
|
||||||
from, to := time.Time{}, time.Time{}
|
from, to := time.Time{}, time.Time{}
|
||||||
|
|
@ -272,7 +272,7 @@ func Date(entries []Entry, date string) []Entry {
|
||||||
}
|
}
|
||||||
|
|
||||||
// produce a Set reduced to the files covered by combined queries
|
// produce a Set reduced to the files covered by combined queries
|
||||||
func ProcessQueries(tagmap map[string]Set, query Query) Set {
|
func processQueries(tagmap map[string]Set, query Query) Set {
|
||||||
set := Set{}
|
set := Set{}
|
||||||
// sanity check:
|
// sanity check:
|
||||||
if len(query.Tags) < 1 {
|
if len(query.Tags) < 1 {
|
||||||
|
|
@ -306,8 +306,8 @@ func ProcessQueries(tagmap map[string]Set, query Query) Set {
|
||||||
}
|
}
|
||||||
|
|
||||||
// inverts the filelist using the full list from entries. works with intersected queries as long as
|
// inverts the filelist using the full list from entries. works with intersected queries as long as
|
||||||
// ProcessQueries is called first.
|
// processQueries is called first.
|
||||||
func Invert(entries []Entry, files Set) Set {
|
func invert(entries []Entry, files Set) Set {
|
||||||
set := Set{}
|
set := Set{}
|
||||||
for _, e := range entries {
|
for _, e := range entries {
|
||||||
if _, ok := files[e.filename]; !ok {
|
if _, ok := files[e.filename]; !ok {
|
||||||
|
|
@ -318,7 +318,7 @@ func Invert(entries []Entry, files Set) Set {
|
||||||
}
|
}
|
||||||
|
|
||||||
// adjacencies is a map from tag to a map of other tags occuring in the given files.
|
// adjacencies is a map from tag to a map of other tags occuring in the given files.
|
||||||
func Adjacencies(entries []Entry, files Set) map[string]map[string]Set {
|
func makeAdjacencies(entries []Entry, files Set) map[string]map[string]Set {
|
||||||
adjacencies := map[string]map[string]Set{}
|
adjacencies := map[string]map[string]Set{}
|
||||||
|
|
||||||
for _, e := range entries {
|
for _, e := range entries {
|
||||||
|
|
@ -348,7 +348,7 @@ func Adjacencies(entries []Entry, files Set) map[string]map[string]Set {
|
||||||
}
|
}
|
||||||
|
|
||||||
// reduces adjacencies to a single map[tag]Set not including the query tags
|
// reduces adjacencies to a single map[tag]Set not including the query tags
|
||||||
func ReduceAdjacencies(adjacencies map[string]map[string]Set, query Query, invert bool) map[string]Set {
|
func reduceAdjacencies(adjacencies map[string]map[string]Set, query Query, invert bool) map[string]Set {
|
||||||
reduced := map[string]Set{}
|
reduced := map[string]Set{}
|
||||||
if invert {
|
if invert {
|
||||||
// TODO: something's wrong here...
|
// TODO: something's wrong here...
|
||||||
|
|
@ -378,7 +378,7 @@ func ReduceAdjacencies(adjacencies map[string]map[string]Set, query Query, inver
|
||||||
}
|
}
|
||||||
|
|
||||||
// prints out the intersected tagmap
|
// prints out the intersected tagmap
|
||||||
func SprintFiles(files Set) string {
|
func sprintFiles(files Set) string {
|
||||||
ordered_files := make([]string, len(files))
|
ordered_files := make([]string, len(files))
|
||||||
copy(ordered_files, slices.Collect(maps.Keys(files)))
|
copy(ordered_files, slices.Collect(maps.Keys(files)))
|
||||||
slices.Sort(ordered_files)
|
slices.Sort(ordered_files)
|
||||||
|
|
@ -386,7 +386,7 @@ func SprintFiles(files Set) string {
|
||||||
return fmt.Sprintln(strings.Join(ordered_files, "\n"))
|
return fmt.Sprintln(strings.Join(ordered_files, "\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func OrderedTags(tagmap map[string]Set, query Query) []TagCount {
|
func orderedTags(tagmap map[string]Set, query Query) []TagCount {
|
||||||
ordered_tags := []TagCount{}
|
ordered_tags := []TagCount{}
|
||||||
// TODO: there's code smell about this whole approach.
|
// TODO: there's code smell about this whole approach.
|
||||||
if query.Op == WILD {
|
if query.Op == WILD {
|
||||||
|
|
@ -408,8 +408,8 @@ func OrderedTags(tagmap map[string]Set, query Query) []TagCount {
|
||||||
// and original query tags.
|
// and original query tags.
|
||||||
//
|
//
|
||||||
// format is a TOML syntax possibly useful elsewhere.
|
// format is a TOML syntax possibly useful elsewhere.
|
||||||
func Print(w io.Writer, entries []Entry, tagmap map[string]Set, files Set, adjacencies map[string]Set, query Query, verbose bool) {
|
func printFiles(w io.Writer, entries []Entry, tagmap map[string]Set, files Set, adjacencies map[string]Set, query Query, verbose bool) {
|
||||||
f := SprintFiles(files)
|
f := sprintFiles(files)
|
||||||
if !verbose {
|
if !verbose {
|
||||||
fmt.Print(f)
|
fmt.Print(f)
|
||||||
return
|
return
|
||||||
|
|
@ -418,7 +418,7 @@ func Print(w io.Writer, entries []Entry, tagmap map[string]Set, files Set, adjac
|
||||||
filesstr += f
|
filesstr += f
|
||||||
|
|
||||||
tags := fmt.Sprintln("[tags]")
|
tags := fmt.Sprintln("[tags]")
|
||||||
otags := OrderedTags(tagmap, query)
|
otags := orderedTags(tagmap, query)
|
||||||
tsb := strings.Builder{}
|
tsb := strings.Builder{}
|
||||||
// 20 * ' ' + '= 000' = 25
|
// 20 * ' ' + '= 000' = 25
|
||||||
tsb.Grow(len(otags) * 25)
|
tsb.Grow(len(otags) * 25)
|
||||||
|
|
@ -428,7 +428,7 @@ func Print(w io.Writer, entries []Entry, tagmap map[string]Set, files Set, adjac
|
||||||
tags += tsb.String()
|
tags += tsb.String()
|
||||||
|
|
||||||
adj := fmt.Sprintln("[adjacencies]")
|
adj := fmt.Sprintln("[adjacencies]")
|
||||||
oadj := OrderedTags(adjacencies, Query{WILD, []string{}})
|
oadj := orderedTags(adjacencies, Query{WILD, []string{}})
|
||||||
asb := strings.Builder{}
|
asb := strings.Builder{}
|
||||||
// 20 * ' ' + '= 000 : 000' = 31
|
// 20 * ' ' + '= 000 : 000' = 31
|
||||||
asb.Grow(len(oadj) * 31)
|
asb.Grow(len(oadj) * 31)
|
||||||
|
|
@ -449,25 +449,25 @@ func Print(w io.Writer, entries []Entry, tagmap map[string]Set, files Set, adjac
|
||||||
}
|
}
|
||||||
|
|
||||||
func tag(opts options) {
|
func tag(opts options) {
|
||||||
queries := ParseQuery(opts.Query.Val)
|
queries := parseQuery(opts.Query.Val)
|
||||||
filelist := Filelist(GLOB)
|
filelist := getFilelist(GLOB)
|
||||||
entries := Entries(filelist)
|
entries := getEntries(filelist)
|
||||||
|
|
||||||
// we shrink the entries list immediately if we want a date range:
|
// we shrink the entries list immediately if we want a date range:
|
||||||
if opts.Date.Val != "" {
|
if opts.Date.Val != "" {
|
||||||
entries = Date(entries, opts.Date.Val)
|
entries = dateRange(entries, opts.Date.Val)
|
||||||
}
|
}
|
||||||
tagmap := Tagmap(entries)
|
tagmap := makeTagmap(entries)
|
||||||
|
|
||||||
// ProcessQueries must precede Invert because we want Invert to respect combined tags:
|
// processQueries must precede invert because we want invert to respect combined tags:
|
||||||
files := ProcessQueries(tagmap, queries)
|
files := processQueries(tagmap, queries)
|
||||||
if opts.Invert.Val {
|
if opts.Invert.Val {
|
||||||
files = Invert(entries, files)
|
files = invert(entries, files)
|
||||||
}
|
}
|
||||||
// NOTE: the full Adjacencies map may one day be useful on its own
|
// NOTE: the full makeAdjacencies map may one day be useful on its own
|
||||||
adjacencies := ReduceAdjacencies(Adjacencies(entries, files), queries, opts.Invert.Val)
|
adjacencies := reduceAdjacencies(makeAdjacencies(entries, files), queries, opts.Invert.Val)
|
||||||
|
|
||||||
Print(os.Stdout, entries, tagmap, files, adjacencies, queries, opts.Verbose.Val)
|
printFiles(os.Stdout, entries, tagmap, files, adjacencies, queries, opts.Verbose.Val)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Tag(args []string) {
|
func Tag(args []string) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue