aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoe <rbo@gmx.us>2024-01-04 20:20:20 +0100
committerJoe <rbo@gmx.us>2024-01-04 20:20:20 +0100
commitc099d658545476a39855e55bca8ef9c42c9f43a3 (patch)
treee67b7805501c773d4bf34e94030d847ef3ce0376
parenttryna make it work (diff)
downloadhardflip-c099d658545476a39855e55bca8ef9c42c9f43a3.tar.gz
hardflip-c099d658545476a39855e55bca8ef9c42c9f43a3.tar.bz2
hardflip-c099d658545476a39855e55bca8ef9c42c9f43a3.tar.xz
hardflip-c099d658545476a39855e55bca8ef9c42c9f43a3.tar.zst
hardflip-c099d658545476a39855e55bca8ef9c42c9f43a3.zip
mixed is good
Diffstat (limited to '')
-rw-r--r--c_hardflip.go17
-rw-r--r--c_init.go2
-rw-r--r--c_ldirs.go18
-rw-r--r--c_lhosts.go28
-rw-r--r--i_events.go28
-rw-r--r--i_ui.go68
6 files changed, 89 insertions, 72 deletions
diff --git a/c_hardflip.go b/c_hardflip.go
index c2270d8..6e33d98 100644
--- a/c_hardflip.go
+++ b/c_hardflip.go
@@ -51,7 +51,13 @@
package main
-import "fmt"
+// import "fmt"
+
+type HardPtr interface {
+ is_dir() bool
+ get_self_dirs() *DirsNode
+ get_self_host() *HostNode
+}
// the main data structure, holds up everything important
type HardData struct {
@@ -60,12 +66,9 @@ type HardData struct {
ui HardUI
opts HardOpts
data_dir string
+ ptr HardPtr
}
-// type HardPtr interface {
-// is_dir() bool
-// }
-
func main() {
data_dir := c_get_data_dir()
opts := HardOpts{true, true, false}
@@ -76,10 +79,9 @@ func main() {
HardUI{},
opts,
data_dir,
+ nil,
}
-
- // var ptr HardPtr
// for ptr = ldirs.head; ptr != nil ; ptr = ptr.next {
// spaces := ""
// for i := 0; i < int(ptr.Depth - 1) * 2; i++ {
@@ -115,6 +117,5 @@ func main() {
// }
// PERF: test performance over a large amount of hosts with litems
- return
i_ui(&data)
}
diff --git a/c_init.go b/c_init.go
index 3c22e2b..06320aa 100644
--- a/c_init.go
+++ b/c_init.go
@@ -63,7 +63,7 @@ type HardOpts struct {
}
-var global_id uint64
+var global_id int
// this function recurses into the specified root directory in order to load
// every yaml file into memory
diff --git a/c_ldirs.go b/c_ldirs.go
index 0eb727a..82b3607 100644
--- a/c_ldirs.go
+++ b/c_ldirs.go
@@ -52,7 +52,7 @@
package main
type DirsNode struct {
- ID uint64
+ ID int
Name string
Parent *DirsNode
Depth uint16
@@ -85,7 +85,7 @@ func (ldirs *DirsList) add_back(node *DirsNode) {
}
// return the list node with the according id
-func (ldirs *DirsList) sel(id uint64) *DirsNode {
+func (ldirs *DirsList) sel(id int) *DirsNode {
curr := ldirs.head
if curr == nil {
@@ -115,10 +115,10 @@ func (ldirs *DirsList) path(node *DirsNode) string {
return path
}
-func (ldirs *DirsList) count() (uint64, uint64) {
+func (ldirs *DirsList) count() (int, int) {
curr := ldirs.head
- var count_dirs uint64
- var count_hosts uint64
+ var count_dirs int
+ var count_hosts int
for count_dirs = 0; curr != nil; count_dirs++ {
count_hosts += curr.lhost.count()
@@ -130,3 +130,11 @@ func (ldirs *DirsList) count() (uint64, uint64) {
func (dir *DirsNode) is_dir() bool {
return true
}
+
+func (dir *DirsNode) get_self_dirs() *DirsNode {
+ return dir
+}
+
+func (dir *DirsNode) get_self_host() *HostNode {
+ return nil
+}
diff --git a/c_lhosts.go b/c_lhosts.go
index bd02643..986fd71 100644
--- a/c_lhosts.go
+++ b/c_lhosts.go
@@ -54,7 +54,7 @@ package main
// 0: ssh
// 1: rdp
type HostNode struct {
- ID uint64
+ ID int
Protocol int8 `yaml:"type"`
Name string `yaml:"name"`
Host string `yaml:"host"`
@@ -104,13 +104,13 @@ func (lhost *HostList) add_back(node *HostNode) {
func (lhost *HostList) reset_id() {
curr := lhost.head
for i := 0; curr != nil; i++ {
- curr.ID = uint64(i)
+ curr.ID = i
curr = curr.next
}
}
// removes a host node from the list
-func (lhost *HostList) del(id uint64) {
+func (lhost *HostList) del(id int) {
if lhost.head == nil {
return
}
@@ -128,7 +128,7 @@ func (lhost *HostList) del(id uint64) {
}
// return the list node with the according id
-func (lhost *HostList) sel(id uint64) *HostNode {
+func (lhost *HostList) sel(id int) *HostNode {
curr := lhost.head
if curr == nil {
@@ -143,9 +143,9 @@ func (lhost *HostList) sel(id uint64) *HostNode {
return curr
}
-func (lhost *HostList) count() uint64 {
+func (lhost *HostList) count() int {
curr := lhost.head
- var count uint64
+ var count int
for count = 0; curr != nil; count++ {
curr = curr.next
@@ -156,3 +156,19 @@ func (lhost *HostList) count() uint64 {
func (host *HostNode) is_dir() bool {
return false
}
+
+func (host *HostNode) get_self_dirs() *DirsNode {
+ return nil
+}
+
+func (host *HostNode) get_self_host() *HostNode {
+ return host
+}
+
+func (host *HostNode) protocol_str() string {
+ switch host.Protocol {
+ case 0: return "SSH"
+ case 1: return "RDP"
+ default: return ""
+ }
+}
diff --git a/i_events.go b/i_events.go
index 436706d..7f585de 100644
--- a/i_events.go
+++ b/i_events.go
@@ -53,6 +53,7 @@ package main
import (
"os"
+
"github.com/gdamore/tcell/v2"
)
@@ -96,37 +97,38 @@ func i_events(data *HardData) {
os.Exit(0)
} else if event.Rune() == 'j' ||
event.Key() == tcell.KeyDown {
- if ui.sel.line < ui.sel_max - 1 {
+ if ui.line < ui.sel_max - 1 {
ui.inc_sel(1)
}
} else if event.Rune() == 'k' ||
event.Key() == tcell.KeyUp {
- if ui.sel.line > 0 {
+ if ui.line > 0 {
ui.inc_sel(-1)
}
} else if event.Rune() == 'g' {
- ui.sel.line = 0
+ ui.line = 0
} else if event.Rune() == 'G' {
- ui.sel.line = ui.sel_max - 1
+ ui.line = ui.sel_max - 1
} else if event.Rune() == 'D' &&
data.ldirs.head != nil &&
ui.sel_max != 0 {
ui.mode = DELETE_MODE
} else if event.Key() == tcell.KeyEnter {
- if ui.sel.host_ptr != nil {
+ if data.ptr != nil && data.ptr.is_dir() == false {
ui.s.Fini()
- c_exec(ui.sel.host_ptr)
+ c_exec(data.ptr.get_self_host())
if data.opts.Loop == false {
os.Exit(0)
+ } else {
+ if ui.s, err = tcell.NewScreen(); err != nil {
+ c_die("view", err)
+ }
+ if err := ui.s.Init(); err != nil {
+ c_die("view", err)
+ }
+ ui.s.SetStyle(ui.def_style)
}
}
- if ui.s, err = tcell.NewScreen(); err != nil {
- c_die("view", err)
- }
- if err := ui.s.Init(); err != nil {
- c_die("view", err)
- }
- ui.s.SetStyle(ui.def_style)
}
if event.Key() == tcell.KeyCtrlR {
i_reload_data(data)
diff --git a/i_ui.go b/i_ui.go
index 26bb9aa..41d2984 100644
--- a/i_ui.go
+++ b/i_ui.go
@@ -43,7 +43,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*
* hardflip: src/i_ui.go
- * Wed Dec 27 18:19:37 2023
+ * Thu 04 Jan 2024 12:03:11 PM CET
* Joe
*
* interfacing with the user
@@ -63,23 +63,17 @@ type HardUI struct {
list_start int
mode uint8
sel_max int
- count_dirs uint64
- count_hosts uint64
+ line int
+ count_dirs int
+ count_hosts int
def_style tcell.Style
dir_style tcell.Style
title_style tcell.Style
dim [2]int
- sel HardSelector
-}
-
-type HardSelector struct {
- line int
- dirs_ptr *DirsNode
- host_ptr *HostNode
}
func (ui *HardUI) inc_sel(n int) {
- ui.sel.line += n
+ ui.line += n
}
func i_draw_text(s tcell.Screen,
@@ -230,9 +224,9 @@ func i_draw_delete_box(ui HardUI, host *HostNode) {
// ui.def_style, "o")
}
-func i_host_panel_dirs(ui HardUI, opts HardOpts, dirs *DirsNode, line int) {
+func i_host_panel_dirs(ui HardUI, icons bool, dirs *DirsNode, line int) {
style := ui.dir_style
- if &ui.sel.dirs_ptr == &dirs {
+ if ui.line == dirs.ID {
style = style.Reverse(true)
}
text := ""
@@ -241,7 +235,7 @@ func i_host_panel_dirs(ui HardUI, opts HardOpts, dirs *DirsNode, line int) {
text += " "
}
}
- if opts.Icon == true {
+ if icons == true {
var fold_var uint8
if dirs.Folded == true {
fold_var = 1
@@ -259,17 +253,17 @@ func i_host_panel_dirs(ui HardUI, opts HardOpts, dirs *DirsNode, line int) {
style, text)
}
-func i_host_panel_host(ui HardUI, opts HardOpts,
+func i_host_panel_host(ui HardUI, icons bool,
dirs *DirsNode, host *HostNode, line int) {
style := ui.def_style
- if &ui.sel.host_ptr == &host {
+ if ui.line == host.ID {
style = style.Reverse(true)
}
text := ""
for i := 0; i < int(dirs.Depth) - 2; i++ {
text += " "
}
- if opts.Icon == true {
+ if icons == true {
text += HOST_ICONS[int(host.Protocol)]
}
text += host.Name
@@ -283,7 +277,7 @@ func i_host_panel_host(ui HardUI, opts HardOpts,
style, text)
}
-func i_host_panel(ui HardUI, opts HardOpts, ldirs *DirsList) {
+func i_host_panel(ui HardUI, icons bool, ldirs *DirsList) {
// TODO: this must work
i_draw_box(ui.s, 0, 0,
ui.dim[W] / 3, ui.dim[H] - 2,
@@ -293,17 +287,17 @@ func i_host_panel(ui HardUI, opts HardOpts, ldirs *DirsList) {
for host := dirs.lhost.head;
dirs.Folded == false && host != nil;
host = host.next {
- i_host_panel_host(ui, opts, dirs, host, line)
+ i_host_panel_host(ui, icons, dirs, host, line)
line++
}
dirs = dirs.next
for line = line; line < ui.dim[H] - 2 && dirs != nil; dirs = dirs.next {
- i_host_panel_dirs(ui, opts, dirs, line)
+ i_host_panel_dirs(ui, icons, dirs, line)
line++
for host := dirs.lhost.head;
dirs.Folded == false && host != nil;
host = host.next {
- i_host_panel_host(ui, opts, dirs, host, line)
+ i_host_panel_host(ui, icons, dirs, host, line)
line++
}
}
@@ -316,7 +310,7 @@ func i_host_panel(ui HardUI, opts HardOpts, ldirs *DirsList) {
i_draw_text(ui.s,
1, ui.dim[H] - 2, (ui.dim[W] / 3) - 1, ui.dim[H] - 2,
ui.def_style,
- " " + strconv.Itoa(int(ui.sel.line + 1)) + "/" +
+ " " + strconv.Itoa(int(ui.line + 1)) + "/" +
strconv.Itoa(int(ui.sel_max)) + " hosts ")
}
}
@@ -335,12 +329,8 @@ func i_info_panel(ui HardUI, lhost *HostList) {
if lhost.head == nil {
return
}
- host = ui.sel.host_ptr
- if host.Protocol == 0 {
- host_type = "SSH"
- } else if host.Protocol == 1 {
- host_type = "RDP"
- }
+ host = lhost.sel(ui.line)
+ host_type = host.protocol_str()
// name, type
i_draw_text(ui.s,
(ui.dim[W] / 3) + 4, curr_line, ui.dim[W] - 2, curr_line,
@@ -498,10 +488,10 @@ func i_info_panel(ui HardUI, lhost *HostList) {
}
}
-func i_get_sel_max(ldirs *DirsList) (int, uint64, uint64) {
+func i_get_sel_max(ldirs *DirsList) (int, int, int) {
count_dirs, count_hosts := ldirs.count()
- return int(count_dirs + count_hosts), count_dirs, count_hosts
+ return count_dirs + count_hosts, count_dirs, count_hosts
}
func i_ui(data *HardData) {
@@ -530,7 +520,7 @@ func i_ui(data *HardData) {
ui.dim[W], ui.dim[H], _ = term.GetSize(0)
ui.s.Clear()
i_bottom_text(*ui)
- i_host_panel(data.ui, data.opts, data.ldirs)
+ i_host_panel(data.ui, data.opts.Icon, data.ldirs)
// TODO: info panel
// i_info_panel(data.ui, data.lhost)
if data.ldirs.head.lhost.head == nil && data.ldirs.head.next == nil {
@@ -543,15 +533,15 @@ func i_ui(data *HardData) {
}
ui.s.Show()
i_events(data)
- if ui.sel.line >= ui.sel_max {
- ui.sel.line = ui.sel_max - 1
- } else if ui.sel.line < 0 {
- ui.sel.line = 0
+ if ui.line >= ui.sel_max {
+ ui.line = ui.sel_max - 1
+ } else if ui.line < 0 {
+ ui.line = 0
}
- if int(ui.sel.line) > ui.list_start + ui.dim[H] - 4 {
- ui.list_start = int(ui.sel.line + 1) - ui.dim[H] + 3
- } else if int(ui.sel.line) < ui.list_start {
- ui.list_start = int(ui.sel.line)
+ if int(ui.line) > ui.list_start + ui.dim[H] - 4 {
+ ui.list_start = int(ui.line + 1) - ui.dim[H] + 3
+ } else if int(ui.line) < ui.list_start {
+ ui.list_start = int(ui.line)
}
}
}