From 1b348ade03ecf708c13a37f353ac1533f7c0d747 Mon Sep 17 00:00:00 2001
From: Joe <rbo@gmx.us>
Date: Tue, 9 Jan 2024 20:20:20 +0100
Subject: folding functionally

---
 c_ldirs.go  |  9 ---------
 c_lhosts.go | 11 +----------
 c_litems.go | 55 +++++++++++++++++++++++++++++++++++++------------------
 i_ui.go     |  9 +++++----
 4 files changed, 43 insertions(+), 41 deletions(-)

diff --git a/c_ldirs.go b/c_ldirs.go
index b32db51..d707686 100644
--- a/c_ldirs.go
+++ b/c_ldirs.go
@@ -116,12 +116,3 @@ func (dir *DirsNode) count_hosts() int {
 	}
 	return dir.lhost.last.ID + 1
 }
-
-func (dir *DirsNode) folded_parents() bool {
-	for ptr := dir.Parent; ptr.Parent != nil; ptr = ptr.Parent {
-		if ptr.Folded == true {
-			return true
-		}
-	}
-	return false
-}
diff --git a/c_lhosts.go b/c_lhosts.go
index 80fbdbc..691625f 100644
--- a/c_lhosts.go
+++ b/c_lhosts.go
@@ -43,7 +43,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  *
  * hardflip: src/c_lhosts.go
- * Tue Jan 09 12:07:53 2024
+ * Tue Jan 09 12:59:11 2024
  * Joe
  *
  * the hosts linked list
@@ -166,12 +166,3 @@ func (host *HostNode) protocol_str() string {
 	default: return ""
 	}
 }
-
-func (host *HostNode) folded_parents() bool {
-	for ptr := host.Parent; ptr.Parent != nil; ptr = ptr.Parent {
-		if ptr.Folded == true {
-			return true
-		}
-	}
-	return false
-}
diff --git a/c_litems.go b/c_litems.go
index c5e93ff..6b64e8c 100644
--- a/c_litems.go
+++ b/c_litems.go
@@ -147,32 +147,51 @@ func (item *ItemsNode) is_dir() bool {
 }
 
 func (litems *ItemsList) inc(jump int) {
-	item := litems.curr
-	if item == nil {
+	new_item := litems.curr
+
+	if new_item == nil || jump == 0 {
 		return
-	}
-	if jump == 0 {
-		litems.curr = item
 	} else if jump == 1 {
-		if item.next != nil {
-			litems.curr = item.next
+		if new_item.next != nil {
+			new_item = new_item.next
 		}
-		litems.curr = item
 	} else if jump == -1 {
-		if item.prev != nil {
-			litems.curr = item.prev
+		if new_item.prev != nil {
+			new_item = new_item.prev
 		}
-		litems.curr = item
-	}
-	new_item := item
-	if jump > 0 {
-		for i := 0; new_item.next != nil && i < jump; i++ {
+	} else {
+		for i := 0; jump > +1 && new_item.next != nil && i < jump; i++ {
 			new_item = new_item.next
 		}
-		litems.curr = new_item
+		for i := 0; jump < -1 && new_item.prev != nil && i > jump; i-- {
+			new_item = new_item.prev
+		}
 	}
-	for i := 0; new_item.prev != nil && i > jump; i-- {
-		new_item = new_item.prev
+	for new_item.folded_parents() == true &&
+		new_item.next != nil &&
+		new_item.prev != nil {
+		if jump > 0 {
+			new_item = new_item.next
+		} else {
+			new_item = new_item.prev
+		}
 	}
 	litems.curr = new_item
+	// FIX: still will select the last
+}
+
+func (item *ItemsNode) folded_parents() bool {
+	var ptr *DirsNode
+
+	if item.is_dir() == false {
+		ptr = item.Host.Parent
+	} else {
+		ptr = item.Dirs.Parent
+	}
+	for ; ptr.Parent != nil; ptr = ptr.Parent {
+		if ptr.Folded == true {
+			return true
+		}
+	}
+	return false
 }
diff --git a/i_ui.go b/i_ui.go
index b4ffc20..b36fde3 100644
--- a/i_ui.go
+++ b/i_ui.go
@@ -283,9 +283,10 @@ func i_host_panel(ui HardUI, icons bool, litems *ItemsList) {
 	line := 1
 	ptr := litems.draw_start
 	for ptr = ptr; ptr != nil && line < ui.dim[H] - 2; ptr = ptr.next {
-		if ptr.is_dir() == false &&
-		   ptr.Host.folded_parents() == false &&
-		   ptr.Host != nil  {
+		if ptr.folded_parents() == true {
+			continue
+		}
+		if ptr.is_dir() == false && ptr.Host != nil  {
 			i_host_panel_host(ui,
 				icons,
 				ptr.Host.Parent.Depth,
@@ -293,7 +294,7 @@ func i_host_panel(ui HardUI, icons bool, litems *ItemsList) {
 				litems.curr.Host,
 				line)
 			line++
-		} else if ptr.Dirs != nil && ptr.Dirs.folded_parents() == false {
+		} else if ptr.Dirs != nil {
 			i_host_panel_dirs(ui, icons,
 				ptr.Dirs,
 				litems.curr.Dirs,
-- 
cgit v1.2.3