Skip to content

Commit

Permalink
Add cell names in the descriptions of new habit cards (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
utkuufuk authored Jun 11, 2020
1 parent d3d268d commit 50c7a94
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
26 changes: 18 additions & 8 deletions internal/habits/habits.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ type source struct {
service *sheets.SpreadsheetsValuesService
}

type habit struct {
cellName string
state string
}

func GetSource(cfg config.Habits) source {
return source{cfg.SpreadsheetId, cfg.CredentialsFile, cfg.TokenFile, nil}
}
Expand All @@ -35,19 +40,24 @@ func (s source) FetchNewCards(ctx context.Context, cfg config.SourceConfig) ([]t
}

// fetchHabits retrieves the state of today's habits from the spreadsheet
func (s source) fetchHabits() (map[string]string, error) {
func (s source) fetchHabits() (map[string]habit, error) {
today := time.Now()
rangeName := fmt.Sprintf("%s %d!B1:Z%d", today.Month().String()[:3], today.Year(), today.Day()+3)
month := today.Month().String()[:3]
year := today.Year()
rowNo := today.Day() + 3

rangeName := fmt.Sprintf("%s %d!B1:Z%d", month, year, rowNo)
rows, err := s.readCells(rangeName)
if err != nil {
return nil, fmt.Errorf("could not read cells: %w", err)
}

states := make(map[string]string)
states := make(map[string]habit)
for i := 0; i < len(rows[0]); i++ {
name := fmt.Sprintf("%v", rows[0][i])
state := fmt.Sprintf("%v", rows[today.Day()+2][i])
states[name] = state
cellName := fmt.Sprintf("%s %d!%s%d", month, year, string('A'+1+i), rowNo)
states[name] = habit{cellName, state}
}

return states, nil
Expand All @@ -63,13 +73,13 @@ func (s source) readCells(rangeName string) ([][]interface{}, error) {
}

// toCards returns a slice of trello cards from the given habits which haven't been marked today
func toCards(habits map[string]string, label string) (cards []trello.Card, err error) {
for habit, state := range habits {
if state != "" {
func toCards(habits map[string]habit, label string) (cards []trello.Card, err error) {
for name, habit := range habits {
if habit.state != "" {
continue
}

c, err := trello.NewCard(fmt.Sprintf("%v", habit), label, "", nil)
c, err := trello.NewCard(fmt.Sprintf("%v", name), label, habit.cellName, nil)
if err != nil {
return nil, fmt.Errorf("could not create habit card: %w", err)
}
Expand Down
34 changes: 17 additions & 17 deletions internal/habits/habits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,48 @@ func TestToCards(t *testing.T) {
tt := []struct {
name string
label string
habits map[string]string
habits map[string]habit
numCards int
err error
}{
{
name: "blank habit name",
label: str,
habits: map[string]string{"": ""},
habits: map[string]habit{"": {}},
numCards: 0,
err: errors.New(""),
},
{
name: "missing label",
label: "",
habits: map[string]string{str: ""},
habits: map[string]habit{str: {}},
numCards: 0,
err: errors.New(""),
},
{
name: "marked habits",
label: str,
habits: map[string]string{
"a": "✔",
"b": "x",
"c": "✘",
"d": "–",
"e": "-",
habits: map[string]habit{
"a": {str, "✔"},
"b": {str, "x"},
"c": {str, "✘"},
"d": {str, "–"},
"e": {str, "-"},
},
numCards: 0,
err: nil,
},
{
name: "some marked some unhabits",
label: str,
habits: map[string]string{
"a": "✔",
"b": "x",
"c": "✘",
"d": "–",
"e": "-",
"f": "",
"g": "",
habits: map[string]habit{
"a": {str, "✔"},
"b": {str, "x"},
"c": {str, "✘"},
"d": {str, "–"},
"e": {str, "-"},
"f": {str, ""},
"g": {str, ""},
},
numCards: 2,
err: nil,
Expand Down

0 comments on commit 50c7a94

Please sign in to comment.