Skip to content

Commit

Permalink
Fix GH-2. Editing default meta data.
Browse files Browse the repository at this point in the history
Fixed/improved inserting cards after editing. Any card inserted while
editing is now added to the deck. In addition, those new cards copy the
Spaced Repetition meta information of the current card being reviewed.
  • Loading branch information
alanxoc3 committed May 31, 2020
1 parent c34933c commit 9510628
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 28 deletions.
10 changes: 10 additions & 0 deletions core/deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,20 @@ func (d *Deck) AddMeta(h string, m *Meta) {
d.Mmap[h] = m
}

func (d *Deck) AddMetaIfNil(h string, m *Meta) {
if _, ok := d.Mmap[h]; !ok {
d.AddMeta(h, m)
}
}

func (d *Deck) Len() int {
return len(d.refs)
}

func (d *Deck) IsEmpty() bool {
return d.Len() == 0
}

func (d *Deck) Swap(i, j int) {
d.refs[i], d.refs[j] = d.refs[j], d.refs[i]
}
Expand Down
13 changes: 10 additions & 3 deletions core/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ func (d *Deck) FilterNumber(param int) {
})
}

func (d *Deck) FilterOutFileDeck(path string, nd *Deck) {
func (d *Deck) FileIntersection(path string, other_deck *Deck) {
d.filter(func(i int) bool {
_, ok := nd.Cmap[d.refs[i]]
return d.GetCard(i).File == path && !ok
_, contains := other_deck.Cmap[d.refs[i]]
return d.GetCard(i).File == path && !contains
})
}

func (d *Deck) OuterLeftJoin(other_deck *Deck) {
d.filter(func(i int) bool {
_, contains := other_deck.Cmap[d.refs[i]]
return contains
})
}

Expand Down
62 changes: 37 additions & 25 deletions file/edit.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,58 @@
package file

import (
"fmt"
"os"
"os/exec"
"fmt"
"os"
"os/exec"

"github.com/alanxoc3/concards/core"
"github.com/alanxoc3/concards/core"
)

// Assumes the deck is sorted how you want it to be sorted.
func EditFile(d *core.Deck, cfg *Config) error {
h, c, _ := d.Top()
if c == nil {
return fmt.Errorf("Error: The deck is empty.")
if d.IsEmpty() {
return fmt.Errorf("Error: The deck is empty.")
}

f := c.File
cmd := exec.Command(cfg.Editor, f)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
// We need to get information for the top card first.
cur_hash, cur_card, cur_meta := d.Top()
file_name := cur_card.File

if err := cmd.Run(); err != nil {
return fmt.Errorf("Error: The editor returned an error code.")
}
// Save the contents of the file now.
deck_before := core.NewDeck()
ReadCardsToDeck(deck_before, file_name)

nd := core.NewDeck()
for k, v := range d.Mmap { nd.Mmap[k] = v }
ReadCardsToDeck(nd, f)
// Then edit the file.
cmd := exec.Command(cfg.Editor, file_name)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout

if !cfg.IsMemorize { nd.FilterOutMemorize() }
if !cfg.IsReview { nd.FilterOutReview() }
if !cfg.IsDone { nd.FilterOutDone() }
if err := cmd.Run(); err != nil {
return fmt.Errorf("Error: The editor returned an error code.")
}

// Save the contents of the file after.
deck_after := core.NewDeck()
ReadCardsToDeck(deck_after, file_name)

// Take out any card that was removed from the file.
d.FileIntersection(file_name, deck_after)

// Get only the cards that were created in the file.
deck_after.OuterLeftJoin(deck_before)

d.FilterOutFileDeck(f, nd)
// Change the insert index based on if the current card was removed or not.
card_index := 0
if h == d.TopHash() {
if cur_hash == d.TopHash() {
card_index = 1
}

for i := nd.Len() - 1; i >= 0; i-- {
d.InsertCard(nd.GetCard(i), card_index)
// Check if the current card was removed or not.
for i := deck_after.Len() - 1; i >= 0; i-- {
new_card := deck_after.GetCard(i)
d.InsertCard(new_card, card_index)
d.AddMetaIfNil(new_card.HashStr(), cur_meta)
}

return nil
return nil
}

0 comments on commit 9510628

Please sign in to comment.