-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from alanxoc3/dev
Fix GH-2. Editing default meta data.
- Loading branch information
Showing
3 changed files
with
57 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |