Complete declaration table functionality

This commit is contained in:
Riyyi
2024-07-11 21:34:56 +02:00
parent eb05e6d588
commit 9340a2d10e
7 changed files with 211 additions and 69 deletions
+99
View File
@@ -0,0 +1,99 @@
package src
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/atotto/clipboard"
)
type Location int
const (
NONE Location = iota // 0
HOME
OFFICE
VISIT
)
type Declaration struct {
month int
clock_in *regexp.Regexp
dates [31]Location
result string
}
// Constructor
func MakeDeclaration(month int) Declaration {
return Declaration{
month: month,
clock_in: Util.CompileRegex(`^\s*\|\s+[0-9]{4}-[0-9]{2}-[0-9]{2}\s+\|\s+IN\s+\|\s+[0-9]{2}:[0-9]{2}\s+\|\s+[a-zA-Z]+\s+\|`),
}
}
func (self *Declaration) Generate(line string, line_number int) string {
var err error
if self.clock_in.MatchString(line) {
err = self.parseLocation(line, line_number)
}
assert(err)
return line
}
func (self *Declaration) Result() string {
var result string
for _, date := range self.dates {
if date == HOME {
result += "x\t\t"
} else if date == OFFICE {
result +="\tx\t"
} else if date == VISIT {
result += "\t\tx"
}
result += "\n"
}
clipboard.WriteAll(result)
result = "-home--office--visit-" + result
result = result + "---------------------"
return result
}
// -----------------------------------------
func (self *Declaration) parseLocation(line string, line_number int) error {
data, err := Util.ParseLine(line, line_number, 4)
if err != nil { return err }
var month_string string = data[0][5:7]
month, err := strconv.Atoi(month_string)
if err != nil || month < 1 || month > 12 {
return fmt.Errorf("invalid month '%s' on line %d\n%s", month_string, line_number, line)
}
var day_string string = data[0][8:]
day, err := strconv.Atoi(day_string)
if err != nil || day < 1 || day > 31 {
return fmt.Errorf("invalid day '%s' on line %d\n%s", day_string, line_number, line)
}
if month == self.month {
var data_month = strings.ToLower(data[3])
if data_month == strings.ToLower("Home") {
self.dates[day - 1] = HOME
} else if data_month == strings.ToLower("Office") {
self.dates[day - 1] = OFFICE
} else if data_month == strings.ToLower("Visit") {
self.dates[day - 1] = VISIT
} else {
return fmt.Errorf("invalid location '%s' on line %d\n%s", data[3], line_number, line)
}
}
return nil
}
// | 2024-07-06 | IN | 08:30 | Office |
+18 -9
View File
@@ -3,7 +3,11 @@ package src
import "bufio"
import "os"
func Parse(path string, job func(line string, line_number int) string) {
var File file
type file struct {}
func (file) Parse(path string, job func(line string, line_number int) string, overwrite bool) {
// Input file
file, err := os.Open(path)
assert(err)
@@ -11,11 +15,14 @@ func Parse(path string, job func(line string, line_number int) string) {
var scanner *bufio.Scanner = bufio.NewScanner(file)
// Output file
output_file, err := os.Create(path + ".tmp")
assert(err)
defer output_file.Close()
var writer *bufio.Writer = bufio.NewWriter(output_file)
defer writer.Flush()
var writer *bufio.Writer
if overwrite {
output_file, err := os.Create(path + ".tmp")
assert(err)
defer output_file.Close()
writer = bufio.NewWriter(output_file)
defer writer.Flush()
}
var line string
var line_number int = 1
@@ -25,8 +32,10 @@ func Parse(path string, job func(line string, line_number int) string) {
line_number++
// Write line to output_file
_, err := writer.WriteString(line + "\n")
assert(err)
if writer != nil {
_, err := writer.WriteString(line + "\n")
assert(err)
}
}
// Detect table if it was at the end of the file
@@ -35,7 +44,7 @@ func Parse(path string, job func(line string, line_number int) string) {
err = scanner.Err()
assert(err)
// move file
// TODO: move file
}
// - [v] while looping, start writing a new file, .tmp, Q: write per line or per chunk? how big are the chunks?
+17 -47
View File
@@ -1,12 +1,8 @@
package src
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
)
import "errors"
import "fmt"
import "regexp"
type Date struct {
clock_in string
@@ -18,7 +14,7 @@ type Date struct {
last_description string
}
type Api struct {
type Process struct {
clock_in *regexp.Regexp
task_line *regexp.Regexp
clock_out *regexp.Regexp
@@ -26,16 +22,16 @@ type Api struct {
}
// Constructor
func MakeApi() Api {
return Api{
clock_in: compileRegex(`^\s*\| [0-9]{4}-[0-9]{2}-[0-9]{2} \|\s+IN\s+\|`),
task_line: compileRegex(`^\s*\| [0-9]{4}-[0-9]{2}-[0-9]{2} \| [0-9]{2}:[0-9]{2} \|`),
clock_out: compileRegex(`^\s*\| [0-9]{4}-[0-9]{2}-[0-9]{2} \|\s+OUT\s+\|`),
func MakeProcess() Process {
return Process{
clock_in: Util.CompileRegex(`^\s*\|\s+[0-9]{4}-[0-9]{2}-[0-9]{2}\s+\|\s+IN\s+\|`),
task_line: Util.CompileRegex(`^\s*\|\s+[0-9]{4}-[0-9]{2}-[0-9]{2}\s+\|\s+[0-9]{2}:[0-9]{2}\s+\|`),
clock_out: Util.CompileRegex(`^\s*\|\s+[0-9]{4}-[0-9]{2}-[0-9]{2}\s+\|\s+OUT\s+\|`),
dates: make(map[string]Date),
}
}
func (self *Api) Process(line string, line_number int) string {
func (self *Process) Process(line string, line_number int) string {
var err error
if self.clock_in.MatchString(line) {
err = self.parseClockIn(line, line_number)
@@ -52,34 +48,8 @@ func (self *Api) Process(line string, line_number int) string {
// -----------------------------------------
func compileRegex(pattern string) *regexp.Regexp {
regex, err := regexp.Compile(pattern)
assert(err)
return regex
}
func (self *Api) parseLine(line string, line_number int, size int) ([]string, error) {
var data []string = strings.Split(line, "|")
if len(data) != size + 2 {
return nil, errors.New("malformed line " + strconv.Itoa(line_number) + "\n" + line)
}
data = data[1:size + 1]
for i, value := range data {
data[i] = strings.TrimSpace(value)
if len(data[i]) == 0 {
return nil, errors.New("malformed line " + strconv.Itoa(line_number) + "\n" + line)
}
}
return data, nil
}
func (self *Api) parseClockIn(line string, line_number int) error {
data, err := self.parseLine(line, line_number, 4)
func (self *Process) parseClockIn(line string, line_number int) error {
data, err := Util.ParseLine(line, line_number, 4)
if err != nil { return err }
// Set clock_in, location
@@ -91,8 +61,8 @@ func (self *Api) parseClockIn(line string, line_number int) error {
return nil
}
func (self *Api) parseTask(line string, line_number int) error {
data, err := self.parseLine(line, line_number, 5)
func (self *Process) parseTask(line string, line_number int) error {
data, err := Util.ParseLine(line, line_number, 5)
if err != nil { return err }
var date Date = self.dates[data[0]]
@@ -123,8 +93,8 @@ func (self *Api) parseTask(line string, line_number int) error {
return nil
}
func (self *Api) parseClockOut(line string, line_number int) error {
data, err := self.parseLine(line, line_number, 3)
func (self *Process) parseClockOut(line string, line_number int) error {
data, err := Util.ParseLine(line, line_number, 3)
if err != nil { return err }
// Set clock_out
@@ -142,7 +112,7 @@ func (self *Api) parseClockOut(line string, line_number int) error {
return nil
}
func (self *Api) callApi(date string, from_time string, to_time string, item_id string, description string) error {
func (self *Process) callApi(date string, from_time string, to_time string, item_id string, description string) error {
fmt.Println("API |" + date + "|" + from_time + "|" + to_time + "|" + item_id + "|" + description)
// parse line
+38
View File
@@ -0,0 +1,38 @@
package src
import (
"errors"
"regexp"
"strconv"
"strings"
)
var Util util
type util struct {}
func (util) CompileRegex(pattern string) *regexp.Regexp {
regex, err := regexp.Compile(pattern)
assert(err)
return regex
}
func (util) ParseLine(line string, line_number int, size int) ([]string, error) {
var data []string = strings.Split(line, "|")
if len(data) != size + 2 {
return nil, errors.New("malformed line " + strconv.Itoa(line_number) + "\n" + line)
}
data = data[1:size + 1]
for i, value := range data {
data[i] = strings.TrimSpace(value)
if len(data[i]) == 0 {
return nil, errors.New("malformed line " + strconv.Itoa(line_number) + "\n" + line)
}
}
return data, nil
}