From 8102c5d23feb0fe4794b73022f57c8fd2d591e4e Mon Sep 17 00:00:00 2001
From: Joe <rbo@gmx.us>
Date: Fri, 19 Jan 2024 20:20:20 +0100
Subject: good error msgs

---
 c_defs.go   |  4 ++++
 c_utils.go  | 10 ++++++++++
 i_events.go | 40 +++++++++++++++++++++++-----------------
 i_ui.go     | 29 +++++++++++++++++++++++++++--
 4 files changed, 64 insertions(+), 19 deletions(-)

diff --git a/c_defs.go b/c_defs.go
index d6a55e4..4145613 100644
--- a/c_defs.go
+++ b/c_defs.go
@@ -54,6 +54,8 @@ package main
 const (
     W = 0
     H = 1
+	ERROR_MSG = 0
+	ERROR_ERR = 1
 	DATA_DIR_NAME = "hardflip"
 	VERSION = "v0.1"
 )
@@ -66,12 +68,14 @@ const (
 	DELETE_KEYS_HINTS = `(q)uit -
 (y)es -
 (n)o`
+	ERROR_KEYS_HINTS = "[Enter] Ok"
 )
 
 const (
 	NORMAL_MODE = 0
 	DELETE_MODE = 1
 	LOAD_MODE   = 2
+	ERROR_MODE  = 3
 )
 
 var (
diff --git a/c_utils.go b/c_utils.go
index 4af14de..72761d8 100644
--- a/c_utils.go
+++ b/c_utils.go
@@ -95,3 +95,13 @@ func c_die(str string, err error) {
 	fmt.Fprintf(os.Stderr, "\n")
 	os.Exit(1)
 }
+
+func c_error_mode(msg string, err error, ui *HardUI) {
+	ui.mode = ERROR_MODE
+	err_str := ""
+	if err != nil {
+		err_str = fmt.Sprintf("%v", err)
+	}
+	ui.err[ERROR_MSG] = msg
+	ui.err[ERROR_ERR] = err_str
+}
diff --git a/i_events.go b/i_events.go
index f2430d2..e0889f4 100644
--- a/i_events.go
+++ b/i_events.go
@@ -43,7 +43,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  *
  * hardflip: src/i_events.go
- * Thu Jan 18 18:01:23 2024
+ * Fri Jan 19 19:25:52 2024
  * Joe
  *
  * events in the code
@@ -62,7 +62,6 @@ func i_list_follow_cursor(litems *ItemsList, ui *HardUI) {
 	if litems.draw == nil || litems.curr == nil {
 		return
 	}
-	// HACK: find workaround to kill ids
 	scrolloff := 4
 	if litems.last.ID - (ui.dim[H] - 4) <= litems.draw.ID {
 		scrolloff = 0
@@ -174,12 +173,16 @@ func i_reload_data(data *HardData) {
 	data.folds = make(map[*DirsNode]*ItemsList)
 }
 
-func i_delete_dir(data *HardData) {
+func i_delete_dir(data *HardData) error {
 	if data.litems.curr == nil || data.litems.curr.Dirs == nil {
-		return
+		return nil
 	}
 	curr := data.litems.curr
 	dir_path := data.data_dir + data.litems.curr.Dirs.path()
+	if err := os.RemoveAll(dir_path); err != nil {
+		c_error_mode("can't remove " + dir_path, err, &data.ui)
+		return err
+	}
 	if data.folds[curr.Dirs] == nil {
 		i_fold_dir(data, curr)
 	}
@@ -203,29 +206,25 @@ func i_delete_dir(data *HardData) {
 		data.litems.curr = curr.prev
 	}
 	data.litems.reset_id()
-	if err := os.RemoveAll(dir_path); err != nil {
-		data.ui.s.Fini()
-		c_die("can't remove " + dir_path, err)
-	}
+	return nil
 }
 
-func i_delete_host(data *HardData) {
+func i_delete_host(data *HardData) error {
 	if data.litems.curr == nil {
-		return
+		return nil
 	}
 	if data.litems.curr.is_dir() == true {
-		i_delete_dir(data)
-		return
+		return i_delete_dir(data)
 	}
 	host := data.litems.curr.Host
 	if host == nil {
-		return
+		return nil
 	}
 	file_path := data.data_dir + host.Parent.path() + host.Filename
 
 	if err := os.Remove(file_path); err != nil {
-		data.ui.s.Fini()
-		c_die("can't remove " + file_path, err)
+		c_error_mode("can't remove " + file_path, err, &data.ui)
+		return err
 	}
 	tmp := data.litems.curr.prev
 	host.Parent.lhost.del(host)
@@ -234,6 +233,7 @@ func i_delete_host(data *HardData) {
 		tmp = data.litems.head
 	}
 	data.litems.curr = tmp
+	return nil
 }
 
 // screen events such as keypresses
@@ -333,8 +333,14 @@ func i_events(data *HardData) {
 			   event.Rune() == 'q' ||
 			   event.Rune() == 'n' {
 				ui.mode = NORMAL_MODE
-			} else if event.Rune() == 'y' {
-				i_delete_host(data)
+			} else if event.Key() == tcell.KeyEnter ||
+					  event.Rune() == 'y' {
+				if err := i_delete_host(data); err == nil {
+					ui.mode = NORMAL_MODE
+				}
+			}
+		case ERROR_MODE:
+			if event.Key() == tcell.KeyEnter {
 				ui.mode = NORMAL_MODE
 			}
 		}	
diff --git a/i_ui.go b/i_ui.go
index b0c2414..05e5141 100644
--- a/i_ui.go
+++ b/i_ui.go
@@ -43,7 +43,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  *
  * hardflip: src/i_ui.go
- * Fri Jan 19 18:45:35 2024
+ * Fri Jan 19 19:23:24 2024
  * Joe
  *
  * interfacing with the user
@@ -66,6 +66,7 @@ type HardUI struct {
 	dir_style    tcell.Style
 	title_style  tcell.Style
 	dim          [2]int
+	err          [2]string
 }
 
 func i_left_right(text_len int, ui *HardUI) (int, int) {
@@ -179,6 +180,10 @@ func i_draw_bottom_text(ui HardUI) {
 		text = NORMAL_KEYS_HINTS
 	case DELETE_MODE:
 		text = DELETE_KEYS_HINTS
+	case LOAD_MODE:
+		text = ""
+	case ERROR_MODE:
+		text = ERROR_KEYS_HINTS
 	}
 	i_draw_text(ui.s,
 		0, ui.dim[H] - 1, ui.dim[W], ui.dim[H] - 1,
@@ -220,7 +225,24 @@ func i_draw_delete_msg(ui HardUI, item *ItemsNode) {
 	    ui.def_style.Bold(true), file)
 }
 
-func i_draw_err_box() {
+func i_draw_error_msg(ui HardUI) {
+	lines := 2
+	if len(ui.err[ERROR_ERR]) == 0 {
+		lines = 1
+	}
+	i_draw_msg(ui.s, lines, ui.dim, " Delete ")
+	left, right := i_left_right(len(ui.err[ERROR_MSG]), &ui)
+	line := ui.dim[H] - 2 - 2
+	if len(ui.err[ERROR_ERR]) == 0 {
+		line += 1
+	}
+	i_draw_text(ui.s, left, line, right, line, ui.def_style, ui.err[ERROR_MSG])
+	if len(ui.err[ERROR_ERR]) > 0 {
+		left, right = i_left_right(len(ui.err[ERROR_ERR]), &ui)
+		line += 1
+		i_draw_text(ui.s, left, line, right, line,
+			ui.def_style, ui.err[ERROR_ERR])
+	}
 }
 
 func i_draw_scrollhint(ui HardUI, litems *ItemsList) {
@@ -334,6 +356,9 @@ func i_ui(data_dir string, opts HardOpts) {
 		if data.ui.mode == DELETE_MODE {
 			i_draw_delete_msg(data.ui, data.litems.curr)
 		}
+		if data.ui.mode == ERROR_MODE {
+			i_draw_error_msg(data.ui)
+		}
 		data.ui.s.Show()
 		i_events(&data)
 	}
-- 
cgit v1.2.3