Skip to content

Commit

Permalink
Added type conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
cgxeiji committed Nov 2, 2018
1 parent 51fc15b commit cea032c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
37 changes: 37 additions & 0 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,43 @@ func (e *Entry) GetKey() string {
return e.Key
}

// Convert changes the type of an entry, parsing all the fields from one
// type to the other.
// If the entry does not exits, it defaults to misc entry.
// TODO: create a ErrFieldNotFound error
func Convert(e *Entry, entryType string) *Entry {
to := NewEntry(entryType)
to.Key = e.Key
to.Attach(e.File)

// Check for the new required fields in the required and
// optional fields of the old entry
for field := range to.Required {
if value, ok := e.Required[field]; ok {
to.Required[field] = value
delete(e.Required, field)
} else if value, ok := e.Optional[field]; ok {
to.Required[field] = value
delete(e.Optional, field)
}
}

// Dump of remaining required fields of the old entry to
// optional fields of the new entry
for field, value := range e.Required {
if value != "" {
to.Optional[field] = value
}
}
for field, value := range e.Optional {
if value != "" {
to.Optional[field] = value
}
}

return to
}

// Bib returns a string with all the information of the entry
// in BibLaTex format.
func (e *Entry) Bib() string {
Expand Down
8 changes: 8 additions & 0 deletions scholar/cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package cmd

import (
"github.com/cgxeiji/scholar"
"github.com/spf13/cobra"
)

Expand All @@ -42,15 +43,22 @@ TODO:
attach(entry, addAttach)
return
}
if editType != "" {
entry = scholar.Convert(entry, editType)
update(entry)
}
edit(entry)
update(entry)
},
}

var editType string

func init() {
rootCmd.AddCommand(editCmd)

editCmd.Flags().StringVarP(&addAttach, "attach", "a", "", "attach a file to the entry")
editCmd.Flags().StringVarP(&editType, "type", "t", "", "change the type of the entry")

// Here you will define your flags and configuration settings.

Expand Down
17 changes: 13 additions & 4 deletions scholar/cmd/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func guiShowInfo(g *gocui.Gui, v *gocui.View) error {
entry := showList[cy]

maxX, maxY := g.Size()
if v, err := g.SetView("info", maxX/10, maxY/10, maxX/10*9, maxY/10*9); err != nil {
if v, err := g.SetView("info", maxX/10, maxY/10, maxX*9/10, maxY*9/10); err != nil {
if err != gocui.ErrUnknownView {
return err
}
Expand Down Expand Up @@ -429,9 +429,18 @@ func formatEntry(entry *scholar.Entry, width int) string {
}

func formatEntryInfo(w io.Writer, e *scholar.Entry) {
fmt.Fprintf(w, "Title:\n \033[32;1m%s\033[0m\nAuthor(s):\n \033[31;1m%s\033[0m\nDate:\n \033[33;1m%s\033[0m\n",
e.Required["title"],
e.Required["author"],
fmt.Fprintf(w, "\033[32;7m[%s]\033[0m\n",
strings.ToTitle(e.Type))
fmt.Fprintf(w, "Title:\n \033[32;1m%s\033[0m\n",
e.Required["title"])
aus := strings.Split(e.Required["author"], " and ")
fmt.Fprintf(w, "Author(s):\n")
for _, au := range aus {
fmt.Fprintf(w, " \033[31;1m%s\033[0m\n",
au)
}

fmt.Fprintf(w, "Date:\n \033[33;1m%s\033[0m\n",
e.Required["date"])

var fields []string
Expand Down

0 comments on commit cea032c

Please sign in to comment.