| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- // Data and interface definitions for bugs
- package buggo
- import (
- "fmt"
- "os"
- "path/filepath"
- "strings"
- "github.com/charmbracelet/lipgloss"
- )
- // Bug Definitions ------------------------------------------------------------
- // ----------------------------------------------------------------------------
- type Bug struct {
- Title string // The title of the bug in human readable format
- Description Field // The description of the bug
- Status Field // The status of the bug
- Tags VariadicField // A slice of VariadicFields
- Blockedby VariadicField // A slice of VariadicFields
- Path string // The path to the bug
- // machineTitle string // The machine parseable bug title
- }
- // Renders a bug as text
- // DEPRECATED
- func (b Bug) View() string {
- headerStyle := lipgloss.NewStyle().
- Width(120).
- Foreground(lipgloss.Color("#00FF00"))
- subtitleStyle := lipgloss.NewStyle().
- Width(120).
- Faint(true).
- Underline(true).
- Italic(true)
- tagsStyle := lipgloss.NewStyle().
- Width(50).
- Italic(true)
- var tags string
- for _, tag := range b.Tags.Fields {
- tags = tags + string(tag.Data) + ", "
- }
- var blockers string
- for _, blocker := range b.Blockedby.Fields {
- blockers = blockers + string(blocker.Data) + ", "
- }
- return fmt.Sprintf("%s\n%s\n---\nTags:\t%s\nBlockedby:\t%s\n---\n%s",
- headerStyle.Render(b.Title),
- subtitleStyle.Render("status:\t"+b.Status.Data),
- tagsStyle.Render(tags),
- tagsStyle.Render(blockers),
- b.Description.Data,
- )
- }
- // Constrcutor for Bugs
- func (b Bug) New(title string, status Field, tags VariadicField, blockedby VariadicField, path string) (bug Bug, err error) { // TODO Implement
- return Bug{}, err
- }
- // Constrcutor for Bugs that loads relevant data from disk
- func (b Bug) NewFromPath(path string) (bug Bug, err error) {
- // Required Fields
- description := &Field{Path: "/description"}
- status := &Field{Path: "/status"}
- requiredFields := []*Field{description, status}
- for _, field := range requiredFields {
- data, err := readPath(path + field.Path)
- if err != nil {
- return Bug{}, err
- }
- field.Data = data
- }
- // Variadic Fields
- tags := VariadicField{Path: "/tags"}
- blockers := VariadicField{Path: "/blockedby"}
- tags, _ = VariadicField.NewFromPath(tags, path)
- blockers, _ = VariadicField.NewFromPath(blockers, path)
- // we can ignore the errors, as loadVariadicField already gracefully handles
- //them.
- // title from path
- title := parsePathToHuman(path)
- return Bug{
- Title: title,
- Description: *description,
- Status: *status,
- Tags: tags,
- Blockedby: blockers,
- Path: path,
- }, err
- }
- // Field Definitions ----------------------------------------------------------
- // ----------------------------------------------------------------------------
- // A struct representing data that is tied to data on disk
- type Field struct {
- Path string
- Data string
- }
- // Constructor for Fields
- func (f Field) New(data string, path string) Field { return Field{Data: data, Path: path} }
- // VariadicField Definitions --------------------------------------------------
- // ----------------------------------------------------------------------------
- // VariadicFields hold lists of Field objects.
- type VariadicField struct {
- Path string // The associated path on disk of Fields represented by Variadic Field
- Fields []Field // The underlying slice of Field objects
- }
- // Constructor for VariadicFields
- func (vf VariadicField) New(fields []Field, path string) VariadicField {
- return VariadicField{Fields: fields, Path: path}
- }
- // Constructor for VariadicFields that loads relevant data from disk
- func (vf VariadicField) NewFromPath(pathOnDisk string) (v VariadicField, err error) {
- rootPath := pathOnDisk + "/" + vf.Path
- files, err := os.ReadDir(rootPath)
- if err != nil {
- return vf, err
- }
- for _, file := range files {
- data, err := readPath(rootPath + "/" + file.Name())
- if err != nil {
- return vf, err
- }
- vf.Fields = append(vf.Fields, Field{Data: data, Path: file.Name()})
- }
- return vf, err
- }
- // Util Definitions -----------------------------------------------------------
- // ----------------------------------------------------------------------------
- // Parses human readable strings as path strings
- func parseHumanToPath(humanReadable string) string {
- var out string
- out = strings.ReplaceAll(humanReadable, "-", "\\replace/")
- out = strings.ReplaceAll(out, " ", "-")
- out = strings.ReplaceAll(out, "\\replace/", "--")
- return out
- }
- // Parses machine parseable paths as human readable strings
- func parsePathToHuman(path string) string {
- _, last := filepath.Split(filepath.Clean(path))
- last = strings.ReplaceAll(last, "--", "\\replace/")
- last = strings.ReplaceAll(last, "-", " ")
- last = strings.ReplaceAll(last, "\\replace/", "-")
- return last
- }
|