diff options
-rw-r--r-- | c_lhosts.go | 44 | ||||
-rw-r--r-- | i_events.go | 96 | ||||
-rw-r--r-- | i_ui.go | 5 |
3 files changed, 88 insertions, 57 deletions
diff --git a/c_lhosts.go b/c_lhosts.go index ac5cfcd..9425231 100644 --- a/c_lhosts.go +++ b/c_lhosts.go @@ -39,7 +39,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * hardflip: src/c_lhosts.go - * Tue Dec 19 18:51:12 2023 + * Thu, 21 Dec 2023 11:51:44 +0100 * Joe * * the hosts linked list @@ -94,23 +94,31 @@ func (lhost *HostList) add_back(node *HostNode) { curr.next = new_node } -// not used - removes a host node from the list -// func (lhost *HostList) del(id uint64) { -// if lhost.head == nil { -// return -// } -// if lhost.head.ID == id { -// lhost.head = lhost.head.next -// return -// } -// curr := lhost.head -// for curr.next != nil && curr.next.ID != id { -// curr = curr.next -// } -// if curr.next != nil { -// curr.next = curr.next.next -// } -// } +func (lhost *HostList) reset_id() { + curr := lhost.head + for i := 0; curr != nil; i++ { + curr.ID = uint64(i) + curr = curr.next + } +} + +// removes a host node from the list +func (lhost *HostList) del(id uint64) { + if lhost.head == nil { + return + } + if lhost.head.ID == id { + lhost.head = lhost.head.next + return + } + curr := lhost.head + for curr.next != nil && curr.next.ID != id { + curr = curr.next + } + if curr.next.ID == id { + curr.next = curr.next.next + } +} // return the list node with the according id func (lhost *HostList) sel(id uint64) *HostNode { diff --git a/i_events.go b/i_events.go index ba1b910..5177434 100644 --- a/i_events.go +++ b/i_events.go @@ -39,7 +39,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * hardflip: src/i_events.go - * Wed Dec 20 16:35:04 2023 + * Thu Dec 21 12:03:59 2023 * Joe * * the hosts linked list @@ -62,6 +62,22 @@ func i_reload_data(data *HardData) { } } +func i_delete_host(data *HardData) { + ui := &data.ui + + data.lhost.del(data.ui.sel) + data.lhost.reset_id() + ui.sel_max = data.lhost.count() + if ui.sel >= ui.sel_max { + ui.sel = ui.sel_max - 1 + } + // file_path := data.data_dir + "/" + host.Folder + host.Filename + // if err := os.Remove(file_path); err != nil { + // c_die("can't remove " + file_path, err) + // } + // i_reload_data(data) +} + // screen events such as keypresses func i_events(data *HardData) { var err error @@ -71,43 +87,55 @@ func i_events(data *HardData) { case *tcell.EventResize: ui.s.Sync() case *tcell.EventKey: - if event.Key() == tcell.KeyEscape || - event.Key() == tcell.KeyCtrlC || - event.Rune() == 'q' { - ui.s.Fini() - os.Exit(0) - } else if event.Rune() == 'j' || - event.Key() == tcell.KeyDown { - if ui.sel < ui.sel_max - 1 { - ui.sel += 1 - } - } else if event.Rune() == 'k' || - event.Key() == tcell.KeyUp { - if ui.sel > 0 { - ui.sel -= 1 - } - } else if event.Rune() == 'g' { - ui.sel = 0 - } else if event.Rune() == 'G' { - ui.sel = ui.sel_max - 1 - } else if event.Rune() == 'D' { - ui.mode = DELETE_MODE - } else if event.Key() == tcell.KeyEnter { - ui.s.Fini() - c_exec(ui.sel, data.lhost) - if data.opts.loop == false { + switch ui.mode { + case NORMAL_MODE: + if event.Key() == tcell.KeyCtrlC || + event.Rune() == 'q' { + ui.s.Fini() os.Exit(0) + } else if event.Rune() == 'j' || + event.Key() == tcell.KeyDown { + if ui.sel < ui.sel_max - 1 { + ui.sel += 1 + } + } else if event.Rune() == 'k' || + event.Key() == tcell.KeyUp { + if ui.sel > 0 { + ui.sel -= 1 + } + } else if event.Rune() == 'g' { + ui.sel = 0 + } else if event.Rune() == 'G' { + ui.sel = ui.sel_max - 1 + } else if event.Rune() == 'D' { + ui.mode = DELETE_MODE + } else if event.Key() == tcell.KeyEnter { + ui.s.Fini() + c_exec(ui.sel, data.lhost) + if data.opts.loop == false { + os.Exit(0) + } + 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 event.Key() == tcell.KeyCtrlR { + i_reload_data(data) } - if err := ui.s.Init(); err != nil { - c_die("view", err) + case DELETE_MODE: + if event.Key() == tcell.KeyEscape || + event.Key() == tcell.KeyCtrlC || + event.Rune() == 'q' || + event.Rune() == 'n' { + ui.mode = NORMAL_MODE + } else if event.Rune() == 'y' { + i_delete_host(data) + ui.mode = NORMAL_MODE } - ui.s.SetStyle(ui.def_style) - } - if event.Key() == tcell.KeyCtrlR { - i_reload_data(data) } } } @@ -151,7 +151,6 @@ func i_draw_zhosts_box(ui HardUI) { } func i_draw_delete_box(ui HardUI, host *HostNode) { - // file_path := data.data_dir + "/" + host.Folder + host.Filename text := "Really delete this host?" file := host.Folder + host.Filename max_len := len(text) @@ -205,10 +204,6 @@ func i_draw_delete_box(ui HardUI, host *HostNode) { i_draw_text(ui.s, left + 7, top, right, top, ui.def_style, "o") - // if err := os.Remove(file_path); err != nil { - // c_die("can't remove " + file_path, err) - // } - // i_reload_data(data, sel, sel_max) } func i_host_panel(ui HardUI, lhost *HostList) { |