summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tjp.go (renamed from cmd/tjp.go)24
1 files changed, 19 insertions, 5 deletions
diff --git a/cmd/tjp.go b/tjp.go
index 1efd5f3..ba9c3b0 100644
--- a/cmd/tjp.go
+++ b/tjp.go
@@ -66,35 +66,49 @@ func addMODEntry(entry string) {
}
func listPersonalEntries(orientation string) {
- // Assume rows is a valid *sql.Rows from a database query
var rows *sql.Rows
- // Define variables with sql.Null* types
var id sql.NullInt64
var dateAdded sql.NullTime
var entry sql.NullString
var comment sql.NullString
- rows, err := db.Query("SELECT id, date_added, entry, comment FROM journal_entries WHERE type = 2 ORDER BY id ASC")
+ rows, err := db.Query("SELECT id, date_added, entry, comment FROM journal_entries WHERE type = 2 ORDER BY date_added ASC, id ASC")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
fmt.Println("Personal Entries:")
+ var previousDateAdded sql.NullTime // Track the previous date only
+ previousDateAdded.Time = time.Time{} // Initialize to zero value
+
// Scan for each row
for rows.Next() {
if err := rows.Scan(&id, &dateAdded, &entry, &comment); err != nil {
log.Fatal(err)
}
+
+ // Check if previousDateAdded is valid to avoid printing dash before first entry
+ if previousDateAdded.Valid && dateAdded.Valid && !sameDay(previousDateAdded.Time, dateAdded.Time) {
+ fmt.Println("----") // Print a dashed line between different day entries
+ }
+
if orientation == "vertical" {
formatVertically(id, dateAdded, entry, comment)
} else if orientation == "horizontal" {
- formatHorizontally(id, dateAdded, entry, comment)
+ formatHorizontally(dateAdded, entry, comment)
}
+
+ previousDateAdded = dateAdded // Update previousDateAdded to current date
}
}
+// Function to determine if two times are on the same day
+func sameDay(t1, t2 time.Time) bool {
+ return t1.Year() == t2.Year() && t1.YearDay() == t2.YearDay()
+}
+
func formatVertically(id sql.NullInt64, dateAdded sql.NullTime, entry sql.NullString, comment sql.NullString) {
fmt.Printf("ID: %s\n", formatInt64(id))
fmt.Printf("Date Added: %s\n", formatTime(dateAdded))
@@ -102,7 +116,7 @@ func formatVertically(id sql.NullInt64, dateAdded sql.NullTime, entry sql.NullSt
fmt.Printf("Comment: %s\n", formatString(comment))
}
-func formatHorizontally(id sql.NullInt64, dateAdded sql.NullTime, entry sql.NullString, comment sql.NullString) {
+func formatHorizontally(dateAdded sql.NullTime, entry sql.NullString, comment sql.NullString) {
fmt.Printf("%s | %s | %s\n", formatShortTime(dateAdded), formatString(entry), formatString(comment))
}