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) Eof() {}
func (ph *processHandler) NewFile() {} 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 // TODO: handle [x] numXnum
if !done || repTask.Is { if !flags.Done || flags.RepTask.Is {
if repTask.Is { if flags.RepTask.Is {
ph.Writeln(strings.Repeat("\t", indentLevel) + "- [ ] 0x" + strconv.Itoa(repTask.B) + lineStack[len(lineStack)-1]) ph.Writeln(strings.Repeat("\t", indentLevel) + "- [ ] 0x" + strconv.Itoa(flags.RepTask.B) + lineStack[len(lineStack)-1])
} else { } else {
ph.Writeln(line) ph.Writeln(line)
} }

View File

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

View File

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