Rework to match other .txt projects

This commit is contained in:
Brian Buller 2023-08-23 11:22:19 -05:00
parent d7904bcba2
commit c0e6b72ed3
2 changed files with 34 additions and 32 deletions

View File

@ -7,15 +7,15 @@ import (
)
type Project struct {
Id int // Internal project id
Original string // Original raw project text
Name string // The name of the project
Directory string // The directory part of the project text
Notes string // The filename of the notes file, defaults to Directory+/+notes.md
Repo string // The directory to the git repository, defaults to Directory+/+Name
Contexts []string
ProjectTags []string
AdditionalTags map[string]string
Id int `json:"id"` // Internal project id
Original string `json:"original"` // Original raw project text
Name string `json:"name"` // The name of the project
Directory string `json:"directory"` // The directory part of the project text
Notes string `json:"notes"` // The filename of the notes file, defaults to Directory+/+notes.md
Repo string `json:"repo"` // The directory to the git repository, defaults to Directory+/+Name
Contexts []string `json:"contexts"`
ProjectTags []string `json:"projectTags"`
AdditionalTags map[string]string `json:"additionalTags"`
}
// ParseProject parses the input text string into a Project struct

View File

@ -9,18 +9,20 @@ import (
"strings"
)
type ProjectList []*Project
type ProjectList struct {
Projects []*Project `json:"projects"`
}
func NewProjectList() *ProjectList {
return &ProjectList{}
}
func (projectlist *ProjectList) Size() int {
return len([]*Project(*projectlist))
return len(projectlist.Projects)
}
func (projectlist *ProjectList) GetProjectSlice() []*Project {
return []*Project(*projectlist)
return projectlist.Projects
}
func (projectlist *ProjectList) GetProjectsWithContext(context string) *ProjectList {
@ -39,9 +41,9 @@ func (projectlist *ProjectList) GetProjectsWithProjectTag(projectTag string) *Pr
// bool), and returns a new ProjectList. The original ProjectList is not modified.
func (projectlist *ProjectList) Filter(predicate func(*Project) bool) *ProjectList {
var newList ProjectList
for _, t := range *projectlist {
for _, t := range projectlist.Projects {
if predicate(t) {
newList = append(newList, t)
newList.AddProject(t)
}
}
return &newList
@ -52,21 +54,21 @@ func (projectlist *ProjectList) AddProject(project *Project) {
// The new project will be id 1
project.Id = 1
// All other projects get their id incremented
for _, p := range *projectlist {
for _, p := range projectlist.Projects {
p.Id++
}
// Now prepend the project to the slice
*projectlist = append(*projectlist, &Project{})
copy((*projectlist)[1:], (*projectlist)[0:])
(*projectlist)[0] = project
projectlist.Projects = append(projectlist.Projects, &Project{})
copy(projectlist.Projects[1:], projectlist.Projects[0:])
projectlist.Projects[0] = project
}
// GetProject returns the Project with the given project 'id' form the ProjectList.
// Returns an error if Project could not be found.
func (projectlist *ProjectList) GetProject(id int) (*Project, error) {
for i := range *projectlist {
if ([]*Project(*projectlist))[i].Id == id {
return ([]*Project(*projectlist))[i], nil
for i := range projectlist.Projects {
if projectlist.Projects[i].Id == id {
return projectlist.Projects[i], nil
}
}
return nil, errors.New("project not found")
@ -77,9 +79,9 @@ func (projectlist *ProjectList) GetProject(id int) (*Project, error) {
func (projectlist *ProjectList) RemoveProjectById(id int) error {
var newList ProjectList
found := false
for _, p := range *projectlist {
for _, p := range projectlist.Projects {
if p.Id != id {
newList = append(newList, p)
newList.AddProject(p)
} else {
found = true
}
@ -96,9 +98,9 @@ func (projectlist *ProjectList) RemoveProjectById(id int) error {
func (projectlist *ProjectList) RemoveProject(project Project) error {
var newList ProjectList
found := false
for _, p := range *projectlist {
for _, p := range projectlist.Projects {
if p.String() != project.String() {
newList = append(newList, p)
newList.AddProject(p)
} else {
found = true
}
@ -129,7 +131,7 @@ func (projectlist *ProjectList) ArchiveProjectToFile(project Project, filename s
// LoadFromFile loads a ProjectList from *os.File.
// Note: This will clear the current TimerList and overwrite it's contents with whatever is in *os.File.
func (projectlist *ProjectList) LoadFromFile(file *os.File) error {
*projectlist = []*Project{} // Empty projectlist
projectlist.Projects = []*Project{} // Empty projectlist
projectId := 1
scanner := bufio.NewScanner(file)
for scanner.Scan() {
@ -143,7 +145,7 @@ func (projectlist *ProjectList) LoadFromFile(file *os.File) error {
return err
}
project.Id = projectId
*projectlist = append(*projectlist, project)
projectlist.AddProject(project)
projectId++
}
if err := scanner.Err(); err != nil {
@ -178,19 +180,19 @@ func (projectlist *ProjectList) WriteToFilename(filename string) error {
// String returns a complete list of projects in project.txt format.
func (projectlist ProjectList) String() string {
var ret string
for _, project := range projectlist {
for _, project := range projectlist.Projects {
ret += fmt.Sprintf("%s\n", project.String())
}
return ret
}
// LoadFromFile loads and returns a ProjectList from *os.File.
func LoadFromFile(file *os.File) (ProjectList, error) {
func LoadFromFile(file *os.File) (*ProjectList, error) {
projectlist := ProjectList{}
if err := projectlist.LoadFromFile(file); err != nil {
return nil, err
}
return projectlist, nil
return &projectlist, nil
}
// WriteToFile writes a ProjectList to *os.File.
@ -199,12 +201,12 @@ func WriteToFile(projectlist *ProjectList, file *os.File) error {
}
// LoadFromFilename loads and returns a ProjectList from a file (most likely called "project.txt")
func LoadFromFilename(filename string) (ProjectList, error) {
func LoadFromFilename(filename string) (*ProjectList, error) {
projectlist := ProjectList{}
if err := projectlist.LoadFromFilename(filename); err != nil {
return nil, err
}
return projectlist, nil
return &projectlist, nil
}
// WriteToFilename write a ProjectList to the specified file (most likely called "project.txt")