Skip to content

Commit

Permalink
feat: ask for confirmation before deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
dhth committed Jul 26, 2024
1 parent e6c1ab0 commit 8be3a57
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 22 deletions.
8 changes: 8 additions & 0 deletions cmd/assets/guide/actions-deleting-a-task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
You can permanently delete a task by pressing `<ctrl+x>`. This will put omm in
an "awaiting confirmation" mode, where you can press `<ctrl+x>` again to proceed
with the deletion, or press any other key to cancel it.

This is omm's default behaviour. If you'd rather omm not ask for confirmation,
you can set the `--confirm-before-deletion` flag to `false`.

Go ahead, create a test task, and permanently delete it.
15 changes: 8 additions & 7 deletions cmd/assets/guide/config-a-sample-toml-config.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
Here's a sample TOML configuration file:

```toml
db_path = "~/.local/share/omm/omm-w.db"
tl_color = "#b8bb26"
atl_color = "#fabd2f"
title = "work"
list_density = "spacious"
show_context = false
editor = "vi -u NONE"
db_path = "~/.local/share/omm/omm-w.db"
tl_color = "#b8bb26"
atl_color = "#fabd2f"
title = "work"
list_density = "spacious"
show_context = false
editor = "vi -u NONE"
confirm_before_deletion = false
```
1 change: 1 addition & 0 deletions cmd/guide.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var (
{summary: "actions: markdown in context"},
{summary: "actions: filtering tasks"},
{summary: "actions: quick filtering via a list"},
{summary: "actions: deleting a task"},
{summary: "domain: task bookmarks"},
{summary: "domain: task priorities"},
{summary: "actions: updating task details"},
Expand Down
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func NewRootCommand() (*cobra.Command, error) {
editorFlagInp string
editorCmd string
showContextFlagInp bool
confirmBeforeDeletion bool
)

rootCmd := &cobra.Command{
Expand Down Expand Up @@ -245,6 +246,7 @@ Tip: Quickly add a task using 'omm "task summary goes here"'.
TaskListTitle: taskListTitle,
TextEditorCmd: strings.Fields(editorCmd),
ShowContext: showContextFlagInp,
ConfirmBeforeDeletion: confirmBeforeDeletion,
}

ui.RenderUI(db, config)
Expand Down Expand Up @@ -332,6 +334,7 @@ Error: %s`, author, repoIssuesUrl, guideErr)
TextEditorCmd: strings.Fields(editorCmd),
ShowContext: true,
Guide: true,
ConfirmBeforeDeletion: true,
}

ui.RenderUI(db, config)
Expand Down Expand Up @@ -393,6 +396,7 @@ Error: %s`, author, repoIssuesUrl, err)
rootCmd.Flags().StringVar(&listDensityFlagInp, "list-density", ui.CompactDensityVal, fmt.Sprintf("type of density for the list; possible values: [%s, %s]", ui.CompactDensityVal, ui.SpaciousDensityVal))
rootCmd.Flags().StringVar(&editorFlagInp, "editor", "vi", "editor command to run when adding/editing context to a task")
rootCmd.Flags().BoolVar(&showContextFlagInp, "show-context", false, "whether to start omm with a visible task context pane or not; this can later be toggled on/off in the TUI")
rootCmd.Flags().BoolVar(&confirmBeforeDeletion, "confirm-before-deletion", true, "whether to ask for confirmation before deleting a task")

tasksCmd.Flags().Uint8VarP(&printTasksNum, "num", "n", printTasksDefault, "number of tasks to print")
tasksCmd.Flags().StringVarP(&configPath, "config-path", "c", defaultConfigPath, fmt.Sprintf("location of omm's TOML config file%s", configPathAdditionalCxt))
Expand Down
1 change: 1 addition & 0 deletions internal/ui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ type Config struct {
Guide bool
DBPath string
ShowContext bool
ConfirmBeforeDeletion bool
}
1 change: 1 addition & 0 deletions internal/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type model struct {
contextMdRenderer *glamour.TermRenderer
taskDetailsMdRenderer *glamour.TermRenderer
prefixSearchUse prefixUse
showDeletePrompt bool
}

func (m model) Init() tea.Cmd {
Expand Down
10 changes: 7 additions & 3 deletions internal/ui/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
formHelpColor = "#928374"
formColor = "#928374"
helpMsgColor = "#928374"
promptColor = "#fb4934"
helpViewTitleColor = "#83a598"
helpTitleColor = "#83a598"
sBSuccessMsgColor = "#d3869b"
Expand Down Expand Up @@ -60,12 +61,15 @@ var (
sBSuccessMsgStyle = statusBarMsgStyle.
Foreground(lipgloss.Color(sBSuccessMsgColor))

helpMsgStyle = statusBarMsgStyle.
Foreground(lipgloss.Color(helpMsgColor))

promptStyle = statusBarMsgStyle.
Foreground(lipgloss.Color(promptColor))

formStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color(formColor))

formHelpStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color(formHelpColor))

helpMsgStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color(helpMsgColor))
)
73 changes: 64 additions & 9 deletions internal/ui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,20 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

case tea.KeyMsg:
if m.cfg.ConfirmBeforeDeletion && m.showDeletePrompt && msg.String() != "ctrl+x" {
m.showDeletePrompt = false

switch m.activeView {
case taskListView:
m.taskList.Title = m.cfg.TaskListTitle
m.taskList.Styles.Title = m.taskList.Styles.Title.Background(lipgloss.Color(m.cfg.TaskListColor))
case archivedTaskListView:
m.archivedTaskList.Title = "archived"
m.archivedTaskList.Styles.Title = m.archivedTaskList.Styles.Title.Background(lipgloss.Color(m.cfg.ArchivedTaskListColor))
}
return m, tea.Batch(cmds...)
}

switch keypress := msg.String(); keypress {

case "Q":
Expand Down Expand Up @@ -593,17 +607,57 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

case "ctrl+x":
if m.activeView != taskListView && m.activeView != archivedTaskListView {
break
}

quit := false
switch m.activeView {
case taskListView:
if len(m.taskList.Items()) == 0 {
quit = true
break
}

if m.taskList.IsFiltered() {
m.errorMsg = "Cannot delete items when the task list is filtered"
quit = true
break
}
case archivedTaskListView:
if len(m.archivedTaskList.Items()) == 0 {
quit = true
break
}

if m.archivedTaskList.IsFiltered() {
m.errorMsg = "Cannot delete items when the task list is filtered"
quit = true
break
}
}

if quit {
break
}

if m.cfg.ConfirmBeforeDeletion && !m.showDeletePrompt {
m.showDeletePrompt = true

switch m.activeView {
case taskListView:
m.taskList.Title = "delete ?"
m.taskList.Styles.Title = m.taskList.Styles.Title.Background(lipgloss.Color(promptColor))
case archivedTaskListView:
m.archivedTaskList.Title = "delete ?"
m.archivedTaskList.Styles.Title = m.archivedTaskList.Styles.Title.Background(lipgloss.Color(promptColor))
}

break
}

switch m.activeView {
case taskListView:
index := m.taskList.Index()
t, ok := m.taskList.SelectedItem().(types.Task)
if !ok {
Expand All @@ -612,17 +666,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
cmd = deleteTask(m.db, t.ID, index, true)
cmds = append(cmds, cmd)

case archivedTaskListView:
if len(m.archivedTaskList.Items()) == 0 {
break
}

if m.archivedTaskList.IsFiltered() {
m.errorMsg = "Cannot delete items when the task list is filtered"
break
if m.cfg.ConfirmBeforeDeletion {
m.showDeletePrompt = false
m.taskList.Title = m.cfg.TaskListTitle
m.taskList.Styles.Title = m.taskList.Styles.Title.Background(lipgloss.Color(m.cfg.TaskListColor))
}

case archivedTaskListView:
index := m.archivedTaskList.Index()
task, ok := m.archivedTaskList.SelectedItem().(types.Task)
if !ok {
Expand All @@ -632,6 +682,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

cmd = deleteTask(m.db, task.ID, index, false)
cmds = append(cmds, cmd)
if m.cfg.ConfirmBeforeDeletion {
m.showDeletePrompt = false
m.archivedTaskList.Title = "archived"
m.archivedTaskList.Styles.Title = m.archivedTaskList.Styles.Title.Background(lipgloss.Color(m.cfg.ArchivedTaskListColor))
}
}

case "ctrl+p":
Expand Down
7 changes: 4 additions & 3 deletions internal/ui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ func (m model) View() string {
var content string
var context string
var statusBar string
var helpMsg string
var listEmpty bool

if m.showHelpIndicator && (m.activeView != helpView) {
helpMsg = helpMsgStyle.Render(" Press ? for help")
statusBar += helpMsgStyle.Render("Press ? for help")
}

statusBar = helpMsg
if m.showDeletePrompt {
statusBar += promptStyle.Render("press ctrl+x again to delete, any other key to cancel")
}

if m.errorMsg != "" && m.successMsg != "" {
statusBar += fmt.Sprintf("%s%s",
Expand Down

0 comments on commit 8be3a57

Please sign in to comment.