diff options
author | Joe <rbo@gmx.us> | 2024-04-24 20:20:20 +0200 |
---|---|---|
committer | Joe <rbo@gmx.us> | 2024-04-24 20:20:20 +0200 |
commit | ba8be3d21c183089b9463a9783bdc568d71b6948 (patch) | |
tree | 92d2ec6eb3f57edceeb4b50515615c05e71f8056 | |
parent | todo (diff) | |
download | hardflip-ba8be3d21c183089b9463a9783bdc568d71b6948.tar.gz hardflip-ba8be3d21c183089b9463a9783bdc568d71b6948.tar.bz2 hardflip-ba8be3d21c183089b9463a9783bdc568d71b6948.tar.xz hardflip-ba8be3d21c183089b9463a9783bdc568d71b6948.tar.zst hardflip-ba8be3d21c183089b9463a9783bdc568d71b6948.zip |
go
-rw-r--r-- | src/c_defs.go | 4 | ||||
-rw-r--r-- | src/c_lhosts.go | 2 | ||||
-rw-r--r-- | src/e_events.go | 26 | ||||
-rw-r--r-- | src/e_keys.go | 205 | ||||
-rw-r--r-- | src/i_ui.go | 49 |
5 files changed, 162 insertions, 124 deletions
diff --git a/src/c_defs.go b/src/c_defs.go index 577f1ec..a1b5946 100644 --- a/src/c_defs.go +++ b/src/c_defs.go @@ -43,7 +43,7 @@ * POSSIBILITY OF SUCH DAMAGE. * * hardflip: src/c_defs.go - * Thu Apr 11 17:55:54 2024 + * Wed Apr 24 15:11:37 2024 * Joe * * constants @@ -63,6 +63,8 @@ const ( NORMAL_KEYS_HINTS = `!a/i: insert host - m: mkdir - !s: search - +y: yank - +p: paste - [C-r]: reload !?: help` ERROR_KEYS_HINTS = "[Enter]: ok" diff --git a/src/c_lhosts.go b/src/c_lhosts.go index 3510ee1..66d58e3 100644 --- a/src/c_lhosts.go +++ b/src/c_lhosts.go @@ -43,7 +43,7 @@ * POSSIBILITY OF SUCH DAMAGE. * * hardflip: src/c_lhosts.go - * Thu Feb 01 16:22:33 2024 + * Wed Apr 24 14:31:44 2024 * Joe * * the hosts linked list diff --git a/src/e_events.go b/src/e_events.go index 8c7011e..eb17250 100644 --- a/src/e_events.go +++ b/src/e_events.go @@ -353,9 +353,33 @@ func e_set_protocol_defaults(data *HardData, in *HostNode) { } } +func e_paste_prepare_item(yank *ItemsNode) ItemsNode { + new_host := &HostNode{} + *new_host = *yank.Host + new_host.Name += " (copy)" + if yank.Host.Drive != nil { + new_host.Drive = make(map[string]string, len(yank.Host.Drive)) + for k, v := range yank.Host.Drive { + new_host.Drive[k] = v + } + } + if yank.Host.Shell != nil { + new_host.Shell = make([]string, len(yank.Host.Shell)) + copy(new_host.Shell, yank.Host.Shell) + } + return ItemsNode{Dirs: nil, Host: new_host} +} + +func e_paste_item(litems *ItemsList, item ItemsNode) { + curr := litems.curr +} + // screen events such as keypresses func e_events(data *HardData, fp [MODE_MAX + 1]key_event_mode_func) { ui := &data.ui + if len(ui.msg_buff) != 0 { + ui.msg_buff = "" + } event := ui.s.PollEvent() switch event := event.(type) { case *tcell.EventResize: @@ -365,7 +389,7 @@ func e_events(data *HardData, fp [MODE_MAX + 1]key_event_mode_func) { case *tcell.EventKey: if ui.mode > MODE_MAX { return - } else if brk := fp[ui.mode](data, *event); brk == true { + } else if brk := fp[ui.mode](data, ui, *event); brk == true { return } else if ui.mode == NORMAL_MODE { e_list_follow_cursor(data.litems, ui) diff --git a/src/e_keys.go b/src/e_keys.go index a4e52cb..3a60a98 100644 --- a/src/e_keys.go +++ b/src/e_keys.go @@ -43,7 +43,7 @@ * POSSIBILITY OF SUCH DAMAGE. * * hardflip: src/e_keys.go - * Wed Apr 24 12:16:05 2024 + * Wed Apr 24 16:51:45 2024 * Joe * * events in the keys @@ -52,6 +52,7 @@ package main import ( + "fmt" "os" "strconv" "strings" @@ -59,10 +60,10 @@ import ( "github.com/gdamore/tcell/v2" ) -func e_normal_events(data *HardData, event tcell.EventKey) bool { +func e_normal_events(data *HardData, ui *HardUI, event tcell.EventKey) bool { if event.Key() == tcell.KeyCtrlC || event.Rune() == 'q' { - data.ui.s.Fini() + ui.s.Fini() os.Exit(0) } else if event.Rune() == 'j' || event.Key() == tcell.KeyDown { @@ -72,10 +73,10 @@ func e_normal_events(data *HardData, event tcell.EventKey) bool { data.litems.inc(-1) } else if event.Key() == tcell.KeyCtrlD || event.Key() == tcell.KeyPgDn { - data.litems.inc(+(data.ui.dim[H] / 3)) + data.litems.inc(+(ui.dim[H] / 3)) } else if event.Key() == tcell.KeyCtrlU || event.Key() == tcell.KeyPgUp { - data.litems.inc(-(data.ui.dim[H] / 3)) + data.litems.inc(-(ui.dim[H] / 3)) } else if event.Key() == tcell.KeyCtrlF { // TODO: maybe keymap these } else if event.Key() == tcell.KeyCtrlB { @@ -100,7 +101,7 @@ func e_normal_events(data *HardData, event tcell.EventKey) bool { } else if event.Rune() == 'D' && data.litems.head != nil && data.litems.curr != nil { - data.ui.mode = DELETE_MODE + ui.mode = DELETE_MODE } else if event.Rune() == 'H' { for curr := data.litems.last; curr != nil; curr = curr.prev { if curr.is_dir() == true && data.folds[curr.Dirs] == nil { @@ -151,7 +152,7 @@ func e_normal_events(data *HardData, event tcell.EventKey) bool { if data.litems.curr == nil { return true } else if data.litems.curr.is_dir() == false { - c_exec(data.litems.curr.Host, data.opts, &data.ui) + c_exec(data.litems.curr.Host, data.opts, ui) } else if data.litems.curr.Dirs != nil && data.folds[data.litems.curr.Dirs] == nil { e_fold_dir(data, data.litems.curr) @@ -171,58 +172,66 @@ func e_normal_events(data *HardData, event tcell.EventKey) bool { } } else if event.Rune() == 'a' || event.Rune() == 'i' { - data.ui.mode = INSERT_MODE - data.ui.insert_sel = 0 - data.ui.insert_sel_ok = false + ui.mode = INSERT_MODE + ui.insert_sel = 0 + ui.insert_sel_ok = false } else if event.Key() == tcell.KeyCtrlR { e_reload_data(data) } else if event.Rune() == 'm' || event.Key() == tcell.KeyF7 { - data.ui.mode = MKDIR_MODE - } else if event.Rune() == 'y' { - if data.litems.curr == nil || - data.litems.curr.is_dir() == true { - return true - } + ui.mode = MKDIR_MODE + } else if event.Rune() == 'y' && + (data.litems.curr == nil || + data.litems.curr.is_dir() == true) == false { data.yank = data.litems.curr + ui.msg_buff = "yanked " + data.yank.Host.Name + + " (" + data.yank.Host.parent.path() + data.yank.Host.filename + ")" + } else if event.Rune() == 'p' && data.yank != nil { + // TODO: here + new_item := e_paste_prepare_item(data.yank) + e_paste_item(data.litems, new_item) + data.yank = nil + ui.msg_buff = "pasted " + new_item.Host.Name } - // TODO: here paste return false } -func e_delete_events(data *HardData, event tcell.EventKey) bool { +func e_delete_events(data *HardData, ui *HardUI, event tcell.EventKey) bool { if event.Key() == tcell.KeyEscape || event.Key() == tcell.KeyCtrlC || event.Rune() == 'n' { - data.ui.mode = NORMAL_MODE + ui.mode = NORMAL_MODE } else if event.Key() == tcell.KeyEnter || event.Rune() == 'y' { + if data.yank == data.litems.curr { + data.yank = nil + } if err := e_delete_host(data); err == nil { - data.ui.mode = NORMAL_MODE + ui.mode = NORMAL_MODE return true } } return false } -func e_load_events(data *HardData, event tcell.EventKey) bool { +func e_load_events(data *HardData, ui *HardUI, event tcell.EventKey) bool { return true } -func e_error_events(data *HardData, event tcell.EventKey) bool { +func e_error_events(data *HardData, ui *HardUI, event tcell.EventKey) bool { if event.Rune() != 0 || event.Key() == tcell.KeyEscape || event.Key() == tcell.KeyEnter { - data.ui.mode = NORMAL_MODE + ui.mode = NORMAL_MODE data.load_err = nil } return false } -func e_welcome_events(data *HardData, event tcell.EventKey) bool { +func e_welcome_events(data *HardData, ui *HardUI, event tcell.EventKey) bool { if event.Key() == tcell.KeyEscape || event.Key() == tcell.KeyCtrlC { - data.ui.s.Fini() + ui.s.Fini() os.Exit(0) } if len(data.opts.GPG) == 0 { @@ -230,11 +239,11 @@ func e_welcome_events(data *HardData, event tcell.EventKey) bool { return true } else { data.opts.GPG = data.keys[event.Rune() - 48 - 1][0] - data.ui.s.HideCursor() + ui.s.HideCursor() } } else { if event.Rune() == 'y' { - data.ui.mode = NORMAL_MODE + ui.mode = NORMAL_MODE c_write_options(data.opts.file, data.opts, &data.load_err) } else if event.Rune() == 'n' { data.opts.GPG = "" @@ -243,41 +252,39 @@ func e_welcome_events(data *HardData, event tcell.EventKey) bool { return false } -func e_mkdir_events(data *HardData, event tcell.EventKey) bool { +func e_mkdir_events(data *HardData, ui *HardUI, event tcell.EventKey) bool { if event.Key() == tcell.KeyEscape || event.Key() == tcell.KeyCtrlC { - data.ui.s.HideCursor() - data.ui.mode = NORMAL_MODE - data.ui.buff = "" + ui.s.HideCursor() + ui.mode = NORMAL_MODE + ui.buff = "" data.insert = nil } else if event.Key() == tcell.KeyEnter { - e_mkdir(data, &data.ui) - data.ui.s.HideCursor() - data.ui.mode = NORMAL_MODE - data.ui.buff = "" + e_mkdir(data, ui) + ui.s.HideCursor() + ui.mode = NORMAL_MODE + ui.buff = "" } else { - e_readline(event, &data.ui.buff) + e_readline(event, &ui.buff) } return false } -func e_insert_events(data *HardData, event tcell.EventKey) bool { - ui := &data.ui - +func e_insert_events(data *HardData, ui *HardUI, event tcell.EventKey) bool { if data.insert == nil { if event.Key() == tcell.KeyEscape || event.Key() == tcell.KeyCtrlC { ui.s.HideCursor() - data.ui.mode = NORMAL_MODE - data.ui.insert_sel = 0 + ui.mode = NORMAL_MODE + ui.insert_sel = 0 data.insert = nil ui.buff = "" } else if event.Key() == tcell.KeyEnter { if ui.buff == "" { ui.s.HideCursor() - data.ui.mode = NORMAL_MODE - data.ui.insert_sel = 0 - data.ui.insert_sel_ok = false + ui.mode = NORMAL_MODE + ui.insert_sel = 0 + ui.insert_sel_ok = false data.insert = nil ui.buff = "" return true @@ -293,7 +300,7 @@ func e_insert_events(data *HardData, event tcell.EventKey) bool { data.insert.parent = data.ldirs.head } } else { - e_readline(event, &data.ui.buff) + e_readline(event, &ui.buff) } } else if data.insert != nil { if data.insert_err != nil { @@ -302,71 +309,71 @@ func e_insert_events(data *HardData, event tcell.EventKey) bool { event.Key() == tcell.KeyEnter { data.insert_err = nil } - } else if data.ui.insert_sel_ok == false { + } else if ui.insert_sel_ok == false { if event.Key() == tcell.KeyEscape || event.Key() == tcell.KeyCtrlC || event.Rune() == 'q' { ui.s.HideCursor() - data.ui.mode = NORMAL_MODE - data.ui.insert_sel = 0 + ui.mode = NORMAL_MODE + ui.insert_sel = 0 data.insert = nil ui.buff = "" } else if event.Rune() == 'j' || event.Key() == tcell.KeyDown || event.Key() == tcell.KeyTab { if data.insert.Protocol == PROTOCOL_RDP && - data.ui.insert_sel == INS_PROTOCOL { - data.ui.insert_sel = INS_RDP_HOST + ui.insert_sel == INS_PROTOCOL { + ui.insert_sel = INS_RDP_HOST } else if data.insert.Protocol == PROTOCOL_CMD && - data.ui.insert_sel == INS_PROTOCOL { - data.ui.insert_sel = INS_CMD_CMD + ui.insert_sel == INS_PROTOCOL { + ui.insert_sel = INS_CMD_CMD } else if data.insert.Protocol == PROTOCOL_OS && - data.ui.insert_sel == INS_PROTOCOL { - data.ui.insert_sel = INS_OS_HOST + ui.insert_sel == INS_PROTOCOL { + ui.insert_sel = INS_OS_HOST } else if data.insert.Protocol == PROTOCOL_SSH && - data.ui.insert_sel == INS_SSH_JUMP_HOST && + ui.insert_sel == INS_SSH_JUMP_HOST && len(data.insert.Jump.Host) == 0 { - data.ui.insert_sel = INS_SSH_NOTE - } else if data.ui.insert_sel < data.ui.insert_sel_max { - data.ui.insert_sel += 1 + ui.insert_sel = INS_SSH_NOTE + } else if ui.insert_sel < ui.insert_sel_max { + ui.insert_sel += 1 } } else if event.Rune() == 'k' || event.Key() == tcell.KeyUp { if data.insert.Protocol == PROTOCOL_RDP && - data.ui.insert_sel == INS_RDP_HOST { - data.ui.insert_sel = INS_PROTOCOL + ui.insert_sel == INS_RDP_HOST { + ui.insert_sel = INS_PROTOCOL } else if data.insert.Protocol == PROTOCOL_CMD && - data.ui.insert_sel == INS_CMD_CMD { - data.ui.insert_sel = INS_PROTOCOL + ui.insert_sel == INS_CMD_CMD { + ui.insert_sel = INS_PROTOCOL } else if data.insert.Protocol == PROTOCOL_OS && - data.ui.insert_sel == INS_OS_HOST { - data.ui.insert_sel = INS_PROTOCOL + ui.insert_sel == INS_OS_HOST { + ui.insert_sel = INS_PROTOCOL } else if data.insert.Protocol == PROTOCOL_SSH && - data.ui.insert_sel == INS_SSH_NOTE && + ui.insert_sel == INS_SSH_NOTE && len(data.insert.Jump.Host) == 0 { - data.ui.insert_sel = INS_SSH_JUMP_HOST - } else if data.ui.insert_sel > INS_PROTOCOL { - data.ui.insert_sel -= 1 + ui.insert_sel = INS_SSH_JUMP_HOST + } else if ui.insert_sel > INS_PROTOCOL { + ui.insert_sel -= 1 } } else if event.Rune() == 'g' || event.Rune() == 'h' || event.Key() == tcell.KeyLeft { - data.ui.insert_sel = INS_PROTOCOL + ui.insert_sel = INS_PROTOCOL } else if event.Rune() == 'G' || event.Rune() == 'l' || event.Key() == tcell.KeyRight { - data.ui.insert_sel = data.ui.insert_sel_max + ui.insert_sel = ui.insert_sel_max } else if event.Rune() == 'i' || event.Rune() == 'a' || event.Rune() == ' ' || event.Key() == tcell.KeyEnter { - data.ui.insert_sel_ok = true - switch data.ui.insert_sel { + ui.insert_sel_ok = true + switch ui.insert_sel { case INS_SSH_OK, INS_RDP_OK + len(data.insert.Drive), INS_CMD_OK, INS_OS_OK: - data.ui.insert_sel_ok = false + ui.insert_sel_ok = false i_insert_check_ok(data, data.insert) if data.insert_err != nil { return true @@ -403,7 +410,7 @@ func e_insert_events(data *HardData, event tcell.EventKey) bool { case INS_RDP_FILE: ui.buff = data.insert.RDPFile case INS_RDP_SCREENSIZE: return true case INS_RDP_DYNAMIC: - data.ui.insert_sel_ok = false + ui.insert_sel_ok = false if data.insert.Dynamic == true { data.insert.Dynamic = false } else { @@ -415,7 +422,7 @@ func e_insert_events(data *HardData, event tcell.EventKey) bool { case INS_CMD_CMD: ui.buff = data.insert.Host case INS_CMD_SHELL: ui.buff = data.insert.Shell[0] case INS_CMD_SILENT: - data.ui.insert_sel_ok = false + ui.insert_sel_ok = false if data.insert.Silent == true { data.insert.Silent = false } else { @@ -450,33 +457,33 @@ func e_insert_events(data *HardData, event tcell.EventKey) bool { } else { if event.Key() == tcell.KeyEscape || event.Key() == tcell.KeyCtrlC { - data.ui.insert_sel_ok = false + ui.insert_sel_ok = false ui.buff = "" ui.drives_buff = "" ui.s.HideCursor() } if len(data.insert.Drive) > 0 && - (data.ui.insert_sel >= INS_RDP_DRIVE && - data.ui.insert_sel < INS_RDP_DRIVE + + (ui.insert_sel >= INS_RDP_DRIVE && + ui.insert_sel < INS_RDP_DRIVE + len(data.insert.Drive)) { if event.Rune() == 'y' || event.Rune() == 'Y' || event.Key() == tcell.KeyEnter { delete(data.insert.Drive, data.insert.drive_keys[ - data.ui.insert_sel - INS_RDP_DRIVE]) + ui.insert_sel - INS_RDP_DRIVE]) if len(data.insert.Drive) == 0 { data.insert.Drive = nil } e_set_drive_keys(data) } - data.ui.insert_sel_ok = false + ui.insert_sel_ok = false return true } - switch data.ui.insert_sel { + switch ui.insert_sel { case INS_PROTOCOL: if event.Rune() < '1' || event.Rune() > '4' { - data.ui.insert_sel_ok = false + ui.insert_sel_ok = false ui.buff = "" ui.s.HideCursor() return true @@ -488,13 +495,13 @@ func e_insert_events(data *HardData, event tcell.EventKey) bool { data.insert.Name = name data.insert.parent = parent data.insert.Protocol = int8(event.Rune() - 48 - 1) - data.ui.insert_sel_ok = false + ui.insert_sel_ok = false ui.s.HideCursor() e_set_protocol_defaults(data, data.insert) } case INS_RDP_SCREENSIZE: if event.Rune() < '1' || event.Rune() > '7' { - data.ui.insert_sel_ok = false + ui.insert_sel_ok = false ui.buff = "" ui.s.HideCursor() return true @@ -509,40 +516,40 @@ func e_insert_events(data *HardData, event tcell.EventKey) bool { data.insert.Width = uint16(tmp) tmp, _ = strconv.Atoi(s[H]) data.insert.Height = uint16(tmp) - data.ui.insert_sel_ok = false + ui.insert_sel_ok = false ui.s.HideCursor() } case INS_RDP_QUALITY: if event.Rune() < '1' || event.Rune() > '3' { - data.ui.insert_sel_ok = false + ui.insert_sel_ok = false ui.buff = "" ui.s.HideCursor() return true } else { data.insert.Quality = uint8(event.Rune() - 48 - 1) - data.ui.insert_sel_ok = false + ui.insert_sel_ok = false ui.s.HideCursor() } case INS_RDP_DRIVE + len(data.insert.Drive): - if len(data.ui.drives_buff) == 0 { + if len(ui.drives_buff) == 0 { if event.Key() == tcell.KeyEnter { if len(ui.buff) == 0 { - data.ui.insert_sel_ok = false - data.ui.drives_buff = "" + ui.insert_sel_ok = false + ui.drives_buff = "" ui.buff = "" ui.s.HideCursor() return true } - data.ui.drives_buff = ui.buff + ui.drives_buff = ui.buff ui.buff = "" } else { - e_readline(event, &data.ui.buff) + e_readline(event, &ui.buff) } } else { if event.Key() == tcell.KeyEnter { if len(ui.buff) == 0 { - data.ui.insert_sel_ok = false - data.ui.drives_buff = "" + ui.insert_sel_ok = false + ui.drives_buff = "" ui.buff = "" ui.s.HideCursor() return true @@ -552,12 +559,12 @@ func e_insert_events(data *HardData, event tcell.EventKey) bool { } data.insert.Drive[ui.drives_buff] = ui.buff e_set_drive_keys(data) - data.ui.insert_sel_ok = false + ui.insert_sel_ok = false ui.drives_buff = "" ui.buff = "" ui.s.HideCursor() } else { - e_readline(event, &data.ui.buff) + e_readline(event, &ui.buff) } } case INS_SSH_HOST, @@ -595,7 +602,7 @@ func e_insert_events(data *HardData, event tcell.EventKey) bool { INS_OS_VOLAPI, INS_OS_NOTE: if event.Key() == tcell.KeyEnter { - switch data.ui.insert_sel { + switch ui.insert_sel { case INS_SSH_HOST, INS_RDP_HOST, INS_OS_HOST: @@ -663,11 +670,11 @@ func e_insert_events(data *HardData, event tcell.EventKey) bool { INS_OS_NOTE: data.insert.Note = ui.buff } - data.ui.insert_sel_ok = false + ui.insert_sel_ok = false ui.buff = "" ui.s.HideCursor() } else { - e_readline(event, &data.ui.buff) + e_readline(event, &ui.buff) } } } diff --git a/src/i_ui.go b/src/i_ui.go index 8dc9721..1407e98 100644 --- a/src/i_ui.go +++ b/src/i_ui.go @@ -68,6 +68,7 @@ type HardUI struct { err [2]string buff string drives_buff string + msg_buff string insert_sel int insert_sel_max int insert_sel_ok bool @@ -178,31 +179,35 @@ func i_draw_bottom_text(ui HardUI, opts HardOpts, insert *HostNode, insert_err []error) { text := "" + if len(ui.msg_buff) > 0 { + text = ui.msg_buff + } else { switch ui.mode { - case NORMAL_MODE: - text = NORMAL_KEYS_HINTS - case DELETE_MODE: - text = CONFIRM_KEYS_HINTS - case LOAD_MODE: - text = "Loading..." - case ERROR_MODE: - text = ERROR_KEYS_HINTS - case WELCOME_MODE: - if len(opts.GPG) == 0 { - text = "" - } else { + case NORMAL_MODE: + text = NORMAL_KEYS_HINTS + case DELETE_MODE: text = CONFIRM_KEYS_HINTS - } - case INSERT_MODE: - if insert == nil { - text = "" - } else if insert_err != nil { + case LOAD_MODE: + text = "Loading..." + case ERROR_MODE: text = ERROR_KEYS_HINTS - } else { - text = INSERT_KEYS_HINTS + case WELCOME_MODE: + if len(opts.GPG) == 0 { + text = "" + } else { + text = CONFIRM_KEYS_HINTS + } + case INSERT_MODE: + if insert == nil { + text = "" + } else if insert_err != nil { + text = ERROR_KEYS_HINTS + } else { + text = INSERT_KEYS_HINTS + } + default: + text = "" } - default: - text = "" } i_draw_text(ui.s, 1, ui.dim[H] - 1, ui.dim[W] - 1, ui.dim[H] - 1, @@ -649,7 +654,7 @@ func i_init_styles(ui *HardUI) { Foreground(tcell.ColorYellow).Dim(true).Bold(true) } -type key_event_mode_func func(*HardData, tcell.EventKey) bool +type key_event_mode_func func(*HardData, *HardUI, tcell.EventKey) bool func i_ui(data_dir string) { home_dir, _ := os.UserHomeDir() |