process and processHandler now use flag struct for more easy adding of additional line flags

This commit is contained in:
Dan Ballard 2018-05-21 14:02:22 -07:00
parent edb3d69cfd
commit 08624d672c
3 changed files with 30 additions and 29 deletions

View File

@ -40,11 +40,11 @@ func (ph *processHandler) Writeln(line string) {
func (ph *processHandler) Eof() {}
func (ph *processHandler) NewFile() {}
func (ph *processHandler) ProcessLine(line string, indentLevel int, headerStack []string, lineStack []string, todo bool, done bool, repTask process.RepTask) {
func (ph *processHandler) ProcessLine(line string, indentLevel int, headerStack []string, lineStack []string, flags process.Flags) {
// TODO: handle [x] numXnum
if !done || repTask.Is {
if repTask.Is {
ph.Writeln(strings.Repeat("\t", indentLevel) + "- [ ] 0x" + strconv.Itoa(repTask.B) + lineStack[len(lineStack)-1])
if !flags.Done || flags.RepTask.Is {
if flags.RepTask.Is {
ph.Writeln(strings.Repeat("\t", indentLevel) + "- [ ] 0x" + strconv.Itoa(flags.RepTask.B) + lineStack[len(lineStack)-1])
} else {
ph.Writeln(line)
}

View File

@ -36,40 +36,40 @@ func (ph *processHandler) Eof() {
func (ph *processHandler) handleHeaderPrint() {
for i, header := range ph.headers {
if ! header.printed {
if !header.printed {
ph.Writeln("\t" + strings.Repeat("#", i+1) + " " + header.text)
ph.headers[i].printed = true
}
}
}
func (ph *processHandler) ProcessLine(line string, indentLevel int, headerStack []string, lineStack []string, todo bool, done bool, repTask process.RepTask) {
func (ph *processHandler) ProcessLine(line string, indentLevel int, headerStack []string, lineStack []string, flags process.Flags) {
if strings.Trim(line, " \t\n\r") == "" {
return
}
if line[0] == '#' {
last := headerStack[len(headerStack)-1]
if len(headerStack) > len(ph.headers) {
ph.headers = append(ph.headers, header{ last, false })
ph.headers = append(ph.headers, header{last, false})
} else if len(headerStack) == len(ph.headers) {
ph.headers[len(ph.headers)-1] = header{last, false}
} else if len(headerStack) < len(ph.headers) {
ph.headers = ph.headers[: len(headerStack)]
ph.headers = ph.headers[:len(headerStack)]
ph.headers[len(ph.headers)-1] = header{last, false}
}
}
// inc count of todo items (rep tasks shouldnt count towards outstanding todo, unless done)
if todo && !repTask.Is {
if flags.Todo && !flags.RepTask.Is {
ph.totalCount += 1
}
if done {
if flags.Done {
ph.handleHeaderPrint()
ph.doneCount += 1
repStr := ""
if repTask.Is {
repStr = strconv.Itoa(repTask.A * repTask.B)
if flags.RepTask.Is {
repStr = strconv.Itoa(flags.RepTask.A * flags.RepTask.B)
// inc todo count here since we did a thing, its done, and we dont want a higher done count than total
ph.totalCount += 1
}

View File

@ -21,11 +21,14 @@ type RepTask struct {
}
type Flags struct {
Todo bool
Done bool
RepTask RepTask
}
type ProcessHandler interface {
Writeln(line string)
ProcessLine(line string, indentLevel int, headerStack []string, lineStack []string, todo bool, done bool, repTask RepTask)
ProcessLine(line string, indentLevel int, headerStack []string, lineStack []string, flags Flags)
Eof()
NewFile()
}
@ -102,50 +105,48 @@ func ProcessFile(ph ProcessHandler, fileName string) {
}
}
todo := false
done := false
var repTask RepTask
var flags Flags
if indentLevel < len(lineStack)-1 {
lineStack = lineStack[:indentLevel+1]
}
if indentLevel == len(lineStack)-1 {
lineStack[len(lineStack)-1], todo, done, repTask = getText(line, indentLevel, indentPattern)
lineStack[len(lineStack)-1], flags = getText(line, indentLevel, indentPattern)
}
if indentLevel >= len(lineStack) {
row := ""
row, todo, done, repTask = getText(line, indentLevel, indentPattern)
row, flags = getText(line, indentLevel, indentPattern)
lineStack = append(lineStack, row)
}
ph.ProcessLine(line, indentLevel, headerStack, lineStack, todo, done, repTask)
ph.ProcessLine(line, indentLevel, headerStack, lineStack, flags)
}
ph.Eof()
}
func getText(str string, indentLevel int, indentPattern string) (text string, todo bool, done bool, repTask RepTask) {
func getText(str string, indentLevel int, indentPattern string) (text string, flags Flags) {
//fmt.Printf("indentLevel: %v str: '%s'\n", indentLevel, str )
if len(str) < (indentLevel*4 + 2) {
return "", false, false, RepTask{false, 0, 0}
return "", Flags{false, false, RepTask{false, 0, 0}}
}
str = strings.TrimLeft(str, strings.Repeat(indentPattern, indentLevel))
text = str[2:]
done = false
todo = false
repTask.Is = false
flags.Done = false
flags.Todo = false
flags.RepTask.Is = false
if text[0] == '[' {
todo = true
flags.Todo = true
if text[1] == 'x' || text[1] == 'X' {
done = true
flags.Done = true
}
if len(text) > 4 {
text = text[4:]
}
if repTaskRegExp.MatchString(text) {
repTask.Is = true
flags.RepTask.Is = true
matches := repTaskRegExp.FindStringSubmatch(text)
repTask.A, _ = strconv.Atoi(matches[1])
repTask.B, _ = strconv.Atoi(matches[2])
flags.RepTask.A, _ = strconv.Atoi(matches[1])
flags.RepTask.B, _ = strconv.Atoi(matches[2])
loc := repTaskRegExp.FindIndex([]byte(text))
text = text[loc[1]:]
}