Skip to content

Commit

Permalink
feat: allow tweaks to time while saving
Browse files Browse the repository at this point in the history
  • Loading branch information
dhth committed Jun 9, 2024
1 parent 1e71d8e commit b6115b7
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 28 deletions.
2 changes: 1 addition & 1 deletion internal/ui/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ LIMIT 1

default:
secsSpent := int(endTs.Sub(beginTs).Seconds())
err := updateActiveTLInDB(db, activeTaskLogId, activeTaskId, endTs, secsSpent, comment)
err := updateActiveTLInDB(db, activeTaskLogId, activeTaskId, beginTs, endTs, secsSpent, comment)
if err != nil {
return trackingToggledMsg{err: err}
} else {
Expand Down
1 change: 0 additions & 1 deletion internal/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ type model struct {
activeTaskId int
tasklogSaveType tasklogSaveType
message string
errorMessage string
messages []string
showHelpIndicator bool
terminalHeight int
Expand Down
5 changes: 3 additions & 2 deletions internal/ui/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func insertNewTLInDB(db *sql.DB, taskId int, beginTs time.Time) error {
return nil
}

func updateActiveTLInDB(db *sql.DB, taskLogId int, taskId int, endTs time.Time, secsSpent int, comment string) error {
func updateActiveTLInDB(db *sql.DB, taskLogId int, taskId int, beginTs, endTs time.Time, secsSpent int, comment string) error {

tx, err := db.Begin()
if err != nil {
Expand All @@ -37,6 +37,7 @@ func updateActiveTLInDB(db *sql.DB, taskLogId int, taskId int, endTs time.Time,
stmt, err := tx.Prepare(`
UPDATE task_log
SET active = 0,
begin_ts = ?,
end_ts = ?,
comment = ?
WHERE id = ?
Expand All @@ -47,7 +48,7 @@ AND active = 1;
}
defer stmt.Close()

_, err = stmt.Exec(endTs, comment, taskLogId)
_, err = stmt.Exec(beginTs, endTs, comment, taskLogId)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/render_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (t *task) updateDesc() {
var timeSpent string

if t.secsSpent != 0 {
timeSpent = "time spent: " + humanizeDuration(t.secsSpent)
timeSpent = "worked on for " + humanizeDuration(t.secsSpent)
} else {
timeSpent = "no time spent"
}
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (e taskLogEntry) Description() string {
secsSpent := int(e.endTS.Sub(e.beginTS).Seconds())
timeSpentStr := humanizeDuration(secsSpent)

timeStr := fmt.Sprintf("began %s (spent %s)", RightPadTrim(humanize.Time(e.beginTS), 30), timeSpentStr)
timeStr := fmt.Sprintf("%s (spent %s)", RightPadTrim(humanize.Time(e.beginTS), 30), timeSpentStr)

return fmt.Sprintf("%s %s", RightPadTrim("["+e.taskSummary+"]", 60), timeStr)
}
Expand Down
66 changes: 51 additions & 15 deletions internal/ui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
m.message = ""
m.errorMessage = ""

switch msg := msg.(type) {
case tea.KeyMsg:
Expand Down Expand Up @@ -47,33 +46,57 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Batch(cmds...)
}
case askForCommentView:
m.activeView = activeTaskListView
if m.trackingInputs[entryComment].Value() != "" {
m.activeTLEndTS = time.Now()
cmds = append(cmds, toggleTracking(m.db, m.activeTaskId, m.activeTLBeginTS, m.activeTLEndTS, m.trackingInputs[entryComment].Value()))
m.trackingInputs[entryComment].SetValue("")
beginTS, err := time.ParseInLocation(string(timeFormat), m.trackingInputs[entryBeginTS].Value(), time.Local)
if err != nil {
m.message = err.Error()
return m, tea.Batch(cmds...)
}
m.activeTLBeginTS = beginTS

endTS, err := time.ParseInLocation(string(timeFormat), m.trackingInputs[entryEndTS].Value(), time.Local)

if err != nil {
m.message = err.Error()
return m, tea.Batch(cmds...)
}
m.activeTLEndTS = endTS

if m.trackingInputs[entryComment].Value() == "" {
m.message = "Comment cannot be empty"
return m, tea.Batch(cmds...)
}

if m.activeTLEndTS.Sub(m.activeTLBeginTS).Seconds() <= 0 {
m.message = "time spent is zero"
return m, tea.Batch(cmds...)
}

cmds = append(cmds, toggleTracking(m.db, m.activeTaskId, m.activeTLBeginTS, m.activeTLEndTS, m.trackingInputs[entryComment].Value()))
for i := range m.trackingInputs {
m.trackingInputs[i].SetValue("")
}
m.activeView = activeTaskListView
return m, tea.Batch(cmds...)

case manualTasklogEntryView:
beginTS, err := time.ParseInLocation(string(timeFormat), m.trackingInputs[entryBeginTS].Value(), time.Local)
if err != nil {
m.errorMessage = err.Error()
m.message = err.Error()
return m, tea.Batch(cmds...)
}

endTS, err := time.ParseInLocation(string(timeFormat), m.trackingInputs[entryEndTS].Value(), time.Local)

if err != nil {
m.errorMessage = err.Error()
m.message = err.Error()
return m, tea.Batch(cmds...)
}

comment := m.trackingInputs[entryComment].Value()

if len(comment) == 0 {
m.errorMessage = "Comment cannot be empty"
m.message = "Comment cannot be empty"
return m, tea.Batch(cmds...)

}

for i := range m.trackingInputs {
Expand Down Expand Up @@ -116,7 +139,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.activeView = inactiveTaskListView
case inactiveTaskListView:
m.activeView = activeTaskListView
case manualTasklogEntryView:
case askForCommentView, manualTasklogEntryView:
switch m.trackingFocussedField {
case entryBeginTS:
m.trackingFocussedField = entryEndTS
Expand All @@ -138,7 +161,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.activeView = inactiveTaskListView
case inactiveTaskListView:
m.activeView = taskLogView
case manualTasklogEntryView:
case askForCommentView, manualTasklogEntryView:
switch m.trackingFocussedField {
case entryBeginTS:
m.trackingFocussedField = entryComment
Expand All @@ -163,14 +186,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
return m, tea.Batch(cmds...)
case askForCommentView:
m.trackingInputs[entryComment], cmd = m.trackingInputs[entryComment].Update(msg)
cmds = append(cmds, cmd)
for i := range m.trackingInputs {
m.trackingInputs[i], cmd = m.trackingInputs[i].Update(msg)
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
case manualTasklogEntryView:
for i := range m.trackingInputs {
m.trackingInputs[i], cmd = m.trackingInputs[i].Update(msg)
cmds = append(cmds, cmd)
}
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}

Expand Down Expand Up @@ -304,7 +329,18 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, toggleTracking(m.db, task.id, m.activeTLBeginTS, m.activeTLEndTS, ""))
} else if m.lastChange == insertChange {
m.activeView = askForCommentView
m.activeTLEndTS = time.Now()

beginTimeStr := m.activeTLBeginTS.Format(timeFormat)
currentTimeStr := m.activeTLEndTS.Format(timeFormat)

m.trackingInputs[entryBeginTS].SetValue(beginTimeStr)
m.trackingInputs[entryEndTS].SetValue(currentTimeStr)
m.trackingFocussedField = entryComment

for i := range m.trackingInputs {
m.trackingInputs[i].Blur()
}
m.trackingInputs[m.trackingFocussedField].Focus()
}
}
Expand Down
27 changes: 20 additions & 7 deletions internal/ui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ func (m model) View() string {
if m.message != "" {
statusBar = Trim(m.message, 120)
}
var errorMsg string
if m.errorMessage != "" {
errorMsg = "error: " + Trim(m.errorMessage, 120)
}

var activeMsg string
if m.tasksFetched && m.trackingActive {
var taskSummaryMsg string
Expand Down Expand Up @@ -67,20 +64,37 @@ func (m model) View() string {
content += "\n"
}
case askForCommentView:
formHeadingText := "Saving task entry. Enter the following details:"

content = fmt.Sprintf(
`
%s
%s
%s
%s
%s
%s
%s
%s
`,
formContextStyle.Render(formHeadingText),
formFieldNameStyle.Render("Begin Time (format: 2006/01/02 15:04)"),
m.trackingInputs[entryBeginTS].View(),
formFieldNameStyle.Render("End Time (format: 2006/01/02 15:04)"),
m.trackingInputs[entryEndTS].View(),
formFieldNameStyle.Render(RightPadTrim("Comment:", 16)),
m.trackingInputs[entryComment].View(),
formContextStyle.Render("Press enter to submit"),
)
for i := 0; i < m.terminalHeight-20+10; i++ {
for i := 0; i < m.terminalHeight-20; i++ {
content += "\n"
}
case manualTasklogEntryView:
Expand Down Expand Up @@ -140,11 +154,10 @@ func (m model) View() string {
helpMsg = " " + helpMsgStyle.Render("Press ? for help")
}

footerStr := fmt.Sprintf("%s%s%s%s",
footerStr := fmt.Sprintf("%s%s%s",
toolNameStyle.Render("hours"),
helpMsg,
activeMsg,
errorMsg,
)
footer = footerStyle.Render(footerStr)

Expand Down

0 comments on commit b6115b7

Please # to comment.