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 | 6df427e26a4e4890ad9994940bb38cd96e5970e8 (patch) | |
tree | 2c5caadc228e706402c489ccd2561c34e690792f /src | |
parent | jump (diff) | |
download | hardflip-6df427e26a4e4890ad9994940bb38cd96e5970e8.tar.gz hardflip-6df427e26a4e4890ad9994940bb38cd96e5970e8.tar.bz2 hardflip-6df427e26a4e4890ad9994940bb38cd96e5970e8.tar.xz hardflip-6df427e26a4e4890ad9994940bb38cd96e5970e8.tar.zst hardflip-6df427e26a4e4890ad9994940bb38cd96e5970e8.zip |
refactor
Diffstat (limited to '')
-rw-r--r-- | src/c_defs.go | 1 | ||||
-rw-r--r-- | src/c_exec.go | 2 | ||||
-rw-r--r-- | src/e_events.go | 464 | ||||
-rw-r--r-- | src/e_keys.go | 484 | ||||
-rw-r--r-- | src/i_ui.go | 13 |
5 files changed, 495 insertions, 469 deletions
diff --git a/src/c_defs.go b/src/c_defs.go index 579df64..577f1ec 100644 --- a/src/c_defs.go +++ b/src/c_defs.go @@ -78,6 +78,7 @@ const ( WELCOME_MODE MKDIR_MODE INSERT_MODE + MODE_MAX = INSERT_MODE ) const ( diff --git a/src/c_exec.go b/src/c_exec.go index d36951b..371e28c 100644 --- a/src/c_exec.go +++ b/src/c_exec.go @@ -53,7 +53,6 @@ package main import ( "bytes" - // "fmt" "math/rand/v2" "os" "os/exec" @@ -230,7 +229,6 @@ func c_format_cmd(host *HostNode, opts HardOpts, } func c_redirect_ssh(host *HostNode, local_port uint16) error { - // TODO: here rdr_fmt := []string{} rdr_fmt = append(rdr_fmt, "ssh", "-f") rdr_fmt = append(rdr_fmt, "-L", diff --git a/src/e_events.go b/src/e_events.go index b089a03..8c7011e 100644 --- a/src/e_events.go +++ b/src/e_events.go @@ -53,8 +53,6 @@ package main import ( "os" - "strconv" - "strings" "github.com/gdamore/tcell/v2" "golang.org/x/term" @@ -280,7 +278,7 @@ func e_delete_host(data *HardData) error { return nil } -func e_readline(event *tcell.EventKey, buffer *string) { +func e_readline(event tcell.EventKey, buffer *string) { if len(*buffer) > 0 && (event.Key() == tcell.KeyBackspace || event.Key() == tcell.KeyBackspace2) { @@ -356,7 +354,7 @@ func e_set_protocol_defaults(data *HardData, in *HostNode) { } // screen events such as keypresses -func e_events(data *HardData) { +func e_events(data *HardData, fp [MODE_MAX + 1]key_event_mode_func) { ui := &data.ui event := ui.s.PollEvent() switch event := event.(type) { @@ -365,460 +363,12 @@ func e_events(data *HardData) { e_list_follow_cursor(data.litems, ui) ui.s.Sync() case *tcell.EventKey: - switch ui.mode { - case NORMAL_MODE: - e_normal_events(data, *event) + if ui.mode > MODE_MAX { + return + } else if brk := fp[ui.mode](data, *event); brk == true { + return + } else if ui.mode == NORMAL_MODE { e_list_follow_cursor(data.litems, ui) - case DELETE_MODE: - e_delete_events(data, *event) - case ERROR_MODE: - e_error_events(data, *event) - case WELCOME_MODE: - // TODO: here - if event.Key() == tcell.KeyEscape || - event.Key() == tcell.KeyCtrlC { - ui.s.Fini() - os.Exit(0) - } - if len(data.opts.GPG) == 0 { - if event.Rune() < '1' || event.Rune() > '9' { - break - } else { - data.opts.GPG = data.keys[event.Rune() - 48 - 1][0] - ui.s.HideCursor() - } - } else { - if event.Rune() == 'y' { - ui.mode = NORMAL_MODE - c_write_options(data.opts.file, data.opts, &data.load_err) - } else if event.Rune() == 'n' { - data.opts.GPG = "" - } - } - case INSERT_MODE: - 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 - 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 - data.insert = nil - ui.buff = "" - break - } - ui.s.HideCursor() - data.insert = &HostNode{} - e_set_protocol_defaults(data, data.insert) - data.insert.Name = ui.buff - ui.buff = "" - if data.litems.curr != nil { - data.insert.parent = data.litems.curr.path_node() - } else { - data.insert.parent = data.ldirs.head - } - } else { - e_readline(event, &data.ui.buff) - } - } else if data.insert != nil { - if data.insert_err != nil { - if event.Rune() != 0 || - event.Key() == tcell.KeyEscape || - event.Key() == tcell.KeyEnter { - data.insert_err = nil - } - } else if data.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 - 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 - } else if data.insert.Protocol == PROTOCOL_CMD && - data.ui.insert_sel == INS_PROTOCOL { - data.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 - } else if data.insert.Protocol == PROTOCOL_SSH && - data.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 - } - } 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 - } else if data.insert.Protocol == PROTOCOL_CMD && - data.ui.insert_sel == INS_CMD_CMD { - data.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 - } else if data.insert.Protocol == PROTOCOL_SSH && - data.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 - } - } else if event.Rune() == 'g' || - event.Rune() == 'h' || - event.Key() == tcell.KeyLeft { - data.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 - } 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 { - case INS_SSH_OK, - INS_RDP_OK + len(data.insert.Drive), - INS_CMD_OK, - INS_OS_OK: - data.ui.insert_sel_ok = false - i_insert_check_ok(data, data.insert) - if data.insert_err != nil { - break - } - i_insert_host(data, data.insert) - case INS_SSH_HOST, - INS_RDP_HOST, - INS_OS_HOST: - ui.buff = data.insert.Host - case INS_SSH_PORT, - INS_RDP_PORT: - if data.insert.Port > 0 { - ui.buff = strconv.Itoa(int(data.insert.Port)) - } - case INS_SSH_USER, - INS_RDP_USER, - INS_OS_USER: - ui.buff = data.insert.User - case INS_SSH_PASS, - INS_RDP_PASS, - INS_OS_PASS: - break - case INS_SSH_PRIV: ui.buff = data.insert.Priv - case INS_SSH_JUMP_HOST: ui.buff = data.insert.Jump.Host - case INS_SSH_JUMP_PORT: - if data.insert.Jump.Port > 0 { - ui.buff = strconv.Itoa(int( - data.insert.Jump.Port)) - } - case INS_SSH_JUMP_USER: ui.buff = data.insert.Jump.User - case INS_SSH_JUMP_PASS: break - case INS_SSH_JUMP_PRIV: ui.buff = data.insert.Jump.Priv - case INS_RDP_DOMAIN: ui.buff = data.insert.Domain - case INS_RDP_FILE: ui.buff = data.insert.RDPFile - case INS_RDP_SCREENSIZE: break - case INS_RDP_DYNAMIC: - data.ui.insert_sel_ok = false - if data.insert.Dynamic == true { - data.insert.Dynamic = false - } else { - data.insert.Dynamic = true - } - break - case INS_RDP_QUALITY: break - case INS_RDP_DRIVE + len(data.insert.Drive): break - 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 - if data.insert.Silent == true { - data.insert.Silent = false - } else { - data.insert.Silent = true - } - break - case INS_OS_USERDOMAINID: - ui.buff = data.insert.Stack.UserDomainID - case INS_OS_PROJECTID: - ui.buff = data.insert.Stack.ProjectID - case INS_OS_REGION: - ui.buff = data.insert.Stack.RegionName - case INS_OS_ENDTYPE: - ui.buff = data.insert.Stack.EndpointType - case INS_OS_INTERFACE: - ui.buff = data.insert.Stack.Interface - case INS_OS_IDAPI: - ui.buff = data.insert.Stack.IdentityAPI - case INS_OS_IMGAPI: - ui.buff = data.insert.Stack.ImageAPI - case INS_OS_NETAPI: - ui.buff = data.insert.Stack.NetworkAPI - case INS_OS_VOLAPI: - ui.buff = data.insert.Stack.VolumeAPI - case INS_SSH_NOTE, - INS_RDP_NOTE + len(data.insert.Drive), - INS_CMD_NOTE, - INS_OS_NOTE: - ui.buff = data.insert.Note - } - } - } else { - if event.Key() == tcell.KeyEscape || - event.Key() == tcell.KeyCtrlC { - data.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 + - 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]) - if len(data.insert.Drive) == 0 { - data.insert.Drive = nil - } - e_set_drive_keys(data) - } - data.ui.insert_sel_ok = false - break - } - switch data.ui.insert_sel { - case INS_PROTOCOL: - if event.Rune() < '1' || event.Rune() > '4' { - data.ui.insert_sel_ok = false - ui.buff = "" - ui.s.HideCursor() - break - } else { - name := data.insert.Name - parent := data.insert.parent - data.insert = nil - data.insert = &HostNode{} - data.insert.Name = name - data.insert.parent = parent - data.insert.Protocol = int8(event.Rune() - 48 - 1) - data.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.buff = "" - ui.s.HideCursor() - break - } else { - s := strings.Split( - RDP_SCREENSIZE[uint8(event.Rune() - 48 - 1)], - "x") - if len(s) != 2 { - return - } - tmp, _ := strconv.Atoi(s[W]) - data.insert.Width = uint16(tmp) - tmp, _ = strconv.Atoi(s[H]) - data.insert.Height = uint16(tmp) - data.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.buff = "" - ui.s.HideCursor() - break - } else { - data.insert.Quality = uint8(event.Rune() - 48 - 1) - data.ui.insert_sel_ok = false - ui.s.HideCursor() - } - case INS_RDP_DRIVE + len(data.insert.Drive): - if len(data.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.buff = "" - ui.s.HideCursor() - break - } - data.ui.drives_buff = ui.buff - ui.buff = "" - } else { - e_readline(event, &data.ui.buff) - } - } else { - if event.Key() == tcell.KeyEnter { - if len(ui.buff) == 0 { - data.ui.insert_sel_ok = false - data.ui.drives_buff = "" - ui.buff = "" - ui.s.HideCursor() - break - } - if len(data.insert.Drive) == 0 { - data.insert.Drive = make(map[string]string) - } - data.insert.Drive[ui.drives_buff] = ui.buff - e_set_drive_keys(data) - data.ui.insert_sel_ok = false - ui.drives_buff = "" - ui.buff = "" - ui.s.HideCursor() - } else { - e_readline(event, &data.ui.buff) - } - } - case INS_SSH_HOST, - INS_SSH_PORT, - INS_SSH_USER, - INS_SSH_PASS, - INS_SSH_PRIV, - INS_SSH_JUMP_HOST, - INS_SSH_JUMP_PORT, - INS_SSH_JUMP_USER, - INS_SSH_JUMP_PASS, - INS_SSH_JUMP_PRIV, - INS_SSH_NOTE, - INS_RDP_HOST, - INS_RDP_PORT, - INS_RDP_DOMAIN, - INS_RDP_USER, - INS_RDP_PASS, - INS_RDP_FILE, - INS_RDP_NOTE + len(data.insert.Drive), - INS_CMD_CMD, - INS_CMD_SHELL, - INS_CMD_NOTE, - INS_OS_HOST, - INS_OS_USER, - INS_OS_PASS, - INS_OS_USERDOMAINID, - INS_OS_PROJECTID, - INS_OS_REGION, - INS_OS_ENDTYPE, - INS_OS_INTERFACE, - INS_OS_IDAPI, - INS_OS_IMGAPI, - INS_OS_NETAPI, - INS_OS_VOLAPI, - INS_OS_NOTE: - if event.Key() == tcell.KeyEnter { - switch data.ui.insert_sel { - case INS_SSH_HOST, - INS_RDP_HOST, - INS_OS_HOST: - data.insert.Host = ui.buff - case INS_SSH_PORT, - INS_RDP_PORT: - tmp, _ := strconv.Atoi(ui.buff) - data.insert.Port = uint16(tmp) - case INS_SSH_USER, - INS_RDP_USER, - INS_OS_USER: - data.insert.User = ui.buff - case INS_SSH_PASS, - INS_RDP_PASS, - INS_OS_PASS: - data.insert.Pass, _ = c_encrypt_str(ui.buff, - data.opts.GPG) - case INS_SSH_PRIV: data.insert.Priv = ui.buff - case INS_SSH_JUMP_HOST: - data.insert.Jump.Host = ui.buff - if len(ui.buff) > 0 { - data.insert.Jump.Port = 22 - } else { - data.insert.Jump.Port = 0 - } - case INS_SSH_JUMP_PORT: - tmp, _ := strconv.Atoi(ui.buff) - data.insert.Jump.Port = uint16(tmp) - case INS_SSH_JUMP_USER: - data.insert.Jump.User = ui.buff - case INS_SSH_JUMP_PASS: - data.insert.Jump.Pass, _ = - c_encrypt_str(ui.buff, data.opts.GPG) - case INS_SSH_JUMP_PRIV: - data.insert.Jump.Priv = ui.buff - case INS_RDP_DOMAIN: - data.insert.Domain = ui.buff - case INS_RDP_FILE: - data.insert.RDPFile = ui.buff - case INS_CMD_CMD: - data.insert.Host = ui.buff - case INS_CMD_SHELL: - data.insert.Shell[0] = ui.buff - case INS_OS_USERDOMAINID: - data.insert.Stack.UserDomainID = ui.buff - case INS_OS_PROJECTID: - data.insert.Stack.ProjectID = ui.buff - case INS_OS_REGION: - data.insert.Stack.RegionName = ui.buff - case INS_OS_ENDTYPE: - data.insert.Stack.EndpointType = ui.buff - case INS_OS_INTERFACE: - data.insert.Stack.Interface = ui.buff - case INS_OS_IDAPI: - data.insert.Stack.IdentityAPI = ui.buff - case INS_OS_IMGAPI: - data.insert.Stack.ImageAPI = ui.buff - case INS_OS_NETAPI: - data.insert.Stack.NetworkAPI = ui.buff - case INS_OS_VOLAPI: - data.insert.Stack.VolumeAPI = ui.buff - case INS_SSH_NOTE, - INS_RDP_NOTE + len(data.insert.Drive), - INS_CMD_NOTE, - INS_OS_NOTE: - data.insert.Note = ui.buff - } - data.ui.insert_sel_ok = false - ui.buff = "" - ui.s.HideCursor() - } else { - e_readline(event, &data.ui.buff) - } - } - } - } - case MKDIR_MODE: - if event.Key() == tcell.KeyEscape || - event.Key() == tcell.KeyCtrlC { - ui.s.HideCursor() - ui.mode = NORMAL_MODE - ui.buff = "" - data.insert = nil - } else if event.Key() == tcell.KeyEnter { - e_mkdir(data, ui) - ui.s.HideCursor() - ui.mode = NORMAL_MODE - ui.buff = "" - } else { - e_readline(event, &data.ui.buff) - } } } } diff --git a/src/e_keys.go b/src/e_keys.go index 831a80f..4f45d85 100644 --- a/src/e_keys.go +++ b/src/e_keys.go @@ -53,11 +53,13 @@ package main import ( "os" + "strconv" + "strings" "github.com/gdamore/tcell/v2" ) -func e_normal_events(data *HardData, event tcell.EventKey) { +func e_normal_events(data *HardData, event tcell.EventKey) bool { if event.Key() == tcell.KeyCtrlC || event.Rune() == 'q' { data.ui.s.Fini() @@ -117,7 +119,7 @@ func e_normal_events(data *HardData, event tcell.EventKey) { e_fold_dir(data, curr) data.litems.curr = curr data.litems.draw = data.litems.curr - return + return true } else { if data.folds[curr.Dirs.Parent] == nil { parent := curr.Dirs.Parent @@ -129,7 +131,7 @@ func e_normal_events(data *HardData, event tcell.EventKey) { e_fold_dir(data, curr_new) data.litems.curr = curr_new data.litems.draw = data.litems.curr - return + return true } else { if data.folds[curr_new.Dirs] == nil { @@ -139,7 +141,7 @@ func e_normal_events(data *HardData, event tcell.EventKey) { } } } - return + return true } } } @@ -147,7 +149,7 @@ func e_normal_events(data *HardData, event tcell.EventKey) { event.Key() == tcell.KeyRight || event.Key() == tcell.KeyEnter { if data.litems.curr == nil { - return + return true } else if data.litems.curr.is_dir() == false { c_exec(data.litems.curr.Host, data.opts, &data.ui) } else if data.litems.curr.Dirs != nil && @@ -159,7 +161,7 @@ func e_normal_events(data *HardData, event tcell.EventKey) { } else if event.Rune() == ' ' { if data.litems.curr == nil || data.litems.curr.is_dir() == false { - return + return true } if data.litems.curr.Dirs != nil && data.folds[data.litems.curr.Dirs] == nil { @@ -180,13 +182,14 @@ func e_normal_events(data *HardData, event tcell.EventKey) { } else if event.Rune() == 'y' { if data.litems.curr == nil || data.litems.curr.is_dir() == true { - return + return true } data.yank = data.litems.curr } + return false } -func e_delete_events(data *HardData, event tcell.EventKey) { +func e_delete_events(data *HardData, event tcell.EventKey) bool { if event.Key() == tcell.KeyEscape || event.Key() == tcell.KeyCtrlC || event.Rune() == 'n' { @@ -195,15 +198,478 @@ func e_delete_events(data *HardData, event tcell.EventKey) { event.Rune() == 'y' { if err := e_delete_host(data); err == nil { data.ui.mode = NORMAL_MODE + return true } } + return false } -func e_error_events(data *HardData, event tcell.EventKey) { +func e_load_events(data *HardData, event tcell.EventKey) bool { + return true +} + +func e_error_events(data *HardData, event tcell.EventKey) bool { if event.Rune() != 0 || event.Key() == tcell.KeyEscape || event.Key() == tcell.KeyEnter { data.ui.mode = NORMAL_MODE data.load_err = nil } + return false +} + +func e_welcome_events(data *HardData, event tcell.EventKey) bool { + if event.Key() == tcell.KeyEscape || + event.Key() == tcell.KeyCtrlC { + data.ui.s.Fini() + os.Exit(0) + } + if len(data.opts.GPG) == 0 { + if event.Rune() < '1' || event.Rune() > '9' { + return true + } else { + data.opts.GPG = data.keys[event.Rune() - 48 - 1][0] + data.ui.s.HideCursor() + } + } else { + if event.Rune() == 'y' { + data.ui.mode = NORMAL_MODE + c_write_options(data.opts.file, data.opts, &data.load_err) + } else if event.Rune() == 'n' { + data.opts.GPG = "" + } + } + return false +} + +func e_insert_events(data *HardData, event tcell.EventKey) bool { + ui := &data.ui + + 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 + 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 + data.insert = nil + ui.buff = "" + return true + } + ui.s.HideCursor() + data.insert = &HostNode{} + e_set_protocol_defaults(data, data.insert) + data.insert.Name = ui.buff + ui.buff = "" + if data.litems.curr != nil { + data.insert.parent = data.litems.curr.path_node() + } else { + data.insert.parent = data.ldirs.head + } + } else { + e_readline(event, &data.ui.buff) + } + } else if data.insert != nil { + if data.insert_err != nil { + if event.Rune() != 0 || + event.Key() == tcell.KeyEscape || + event.Key() == tcell.KeyEnter { + data.insert_err = nil + } + } else if data.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 + 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 + } else if data.insert.Protocol == PROTOCOL_CMD && + data.ui.insert_sel == INS_PROTOCOL { + data.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 + } else if data.insert.Protocol == PROTOCOL_SSH && + data.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 + } + } 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 + } else if data.insert.Protocol == PROTOCOL_CMD && + data.ui.insert_sel == INS_CMD_CMD { + data.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 + } else if data.insert.Protocol == PROTOCOL_SSH && + data.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 + } + } else if event.Rune() == 'g' || + event.Rune() == 'h' || + event.Key() == tcell.KeyLeft { + data.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 + } 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 { + case INS_SSH_OK, + INS_RDP_OK + len(data.insert.Drive), + INS_CMD_OK, + INS_OS_OK: + data.ui.insert_sel_ok = false + i_insert_check_ok(data, data.insert) + if data.insert_err != nil { + return true + } + i_insert_host(data, data.insert) + case INS_SSH_HOST, + INS_RDP_HOST, + INS_OS_HOST: + ui.buff = data.insert.Host + case INS_SSH_PORT, + INS_RDP_PORT: + if data.insert.Port > 0 { + ui.buff = strconv.Itoa(int(data.insert.Port)) + } + case INS_SSH_USER, + INS_RDP_USER, + INS_OS_USER: + ui.buff = data.insert.User + case INS_SSH_PASS, + INS_RDP_PASS, + INS_OS_PASS: + return true + case INS_SSH_PRIV: ui.buff = data.insert.Priv + case INS_SSH_JUMP_HOST: ui.buff = data.insert.Jump.Host + case INS_SSH_JUMP_PORT: + if data.insert.Jump.Port > 0 { + ui.buff = strconv.Itoa(int( + data.insert.Jump.Port)) + } + case INS_SSH_JUMP_USER: ui.buff = data.insert.Jump.User + case INS_SSH_JUMP_PASS: return true + case INS_SSH_JUMP_PRIV: ui.buff = data.insert.Jump.Priv + case INS_RDP_DOMAIN: ui.buff = data.insert.Domain + 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 + if data.insert.Dynamic == true { + data.insert.Dynamic = false + } else { + data.insert.Dynamic = true + } + return true + case INS_RDP_QUALITY: return true + case INS_RDP_DRIVE + len(data.insert.Drive): return true + 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 + if data.insert.Silent == true { + data.insert.Silent = false + } else { + data.insert.Silent = true + } + return true + case INS_OS_USERDOMAINID: + ui.buff = data.insert.Stack.UserDomainID + case INS_OS_PROJECTID: + ui.buff = data.insert.Stack.ProjectID + case INS_OS_REGION: + ui.buff = data.insert.Stack.RegionName + case INS_OS_ENDTYPE: + ui.buff = data.insert.Stack.EndpointType + case INS_OS_INTERFACE: + ui.buff = data.insert.Stack.Interface + case INS_OS_IDAPI: + ui.buff = data.insert.Stack.IdentityAPI + case INS_OS_IMGAPI: + ui.buff = data.insert.Stack.ImageAPI + case INS_OS_NETAPI: + ui.buff = data.insert.Stack.NetworkAPI + case INS_OS_VOLAPI: + ui.buff = data.insert.Stack.VolumeAPI + case INS_SSH_NOTE, + INS_RDP_NOTE + len(data.insert.Drive), + INS_CMD_NOTE, + INS_OS_NOTE: + ui.buff = data.insert.Note + } + } + } else { + if event.Key() == tcell.KeyEscape || + event.Key() == tcell.KeyCtrlC { + data.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 + + 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]) + if len(data.insert.Drive) == 0 { + data.insert.Drive = nil + } + e_set_drive_keys(data) + } + data.ui.insert_sel_ok = false + return true + } + switch data.ui.insert_sel { + case INS_PROTOCOL: + if event.Rune() < '1' || event.Rune() > '4' { + data.ui.insert_sel_ok = false + ui.buff = "" + ui.s.HideCursor() + return true + } else { + name := data.insert.Name + parent := data.insert.parent + data.insert = nil + data.insert = &HostNode{} + data.insert.Name = name + data.insert.parent = parent + data.insert.Protocol = int8(event.Rune() - 48 - 1) + data.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.buff = "" + ui.s.HideCursor() + return true + } else { + s := strings.Split( + RDP_SCREENSIZE[uint8(event.Rune() - 48 - 1)], + "x") + if len(s) != 2 { + return true + } + tmp, _ := strconv.Atoi(s[W]) + data.insert.Width = uint16(tmp) + tmp, _ = strconv.Atoi(s[H]) + data.insert.Height = uint16(tmp) + data.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.buff = "" + ui.s.HideCursor() + return true + } else { + data.insert.Quality = uint8(event.Rune() - 48 - 1) + data.ui.insert_sel_ok = false + ui.s.HideCursor() + } + case INS_RDP_DRIVE + len(data.insert.Drive): + if len(data.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.buff = "" + ui.s.HideCursor() + return true + } + data.ui.drives_buff = ui.buff + ui.buff = "" + } else { + e_readline(event, &data.ui.buff) + } + } else { + if event.Key() == tcell.KeyEnter { + if len(ui.buff) == 0 { + data.ui.insert_sel_ok = false + data.ui.drives_buff = "" + ui.buff = "" + ui.s.HideCursor() + return true + } + if len(data.insert.Drive) == 0 { + data.insert.Drive = make(map[string]string) + } + data.insert.Drive[ui.drives_buff] = ui.buff + e_set_drive_keys(data) + data.ui.insert_sel_ok = false + ui.drives_buff = "" + ui.buff = "" + ui.s.HideCursor() + } else { + e_readline(event, &data.ui.buff) + } + } + case INS_SSH_HOST, + INS_SSH_PORT, + INS_SSH_USER, + INS_SSH_PASS, + INS_SSH_PRIV, + INS_SSH_JUMP_HOST, + INS_SSH_JUMP_PORT, + INS_SSH_JUMP_USER, + INS_SSH_JUMP_PASS, + INS_SSH_JUMP_PRIV, + INS_SSH_NOTE, + INS_RDP_HOST, + INS_RDP_PORT, + INS_RDP_DOMAIN, + INS_RDP_USER, + INS_RDP_PASS, + INS_RDP_FILE, + INS_RDP_NOTE + len(data.insert.Drive), + INS_CMD_CMD, + INS_CMD_SHELL, + INS_CMD_NOTE, + INS_OS_HOST, + INS_OS_USER, + INS_OS_PASS, + INS_OS_USERDOMAINID, + INS_OS_PROJECTID, + INS_OS_REGION, + INS_OS_ENDTYPE, + INS_OS_INTERFACE, + INS_OS_IDAPI, + INS_OS_IMGAPI, + INS_OS_NETAPI, + INS_OS_VOLAPI, + INS_OS_NOTE: + if event.Key() == tcell.KeyEnter { + switch data.ui.insert_sel { + case INS_SSH_HOST, + INS_RDP_HOST, + INS_OS_HOST: + data.insert.Host = ui.buff + case INS_SSH_PORT, + INS_RDP_PORT: + tmp, _ := strconv.Atoi(ui.buff) + data.insert.Port = uint16(tmp) + case INS_SSH_USER, + INS_RDP_USER, + INS_OS_USER: + data.insert.User = ui.buff + case INS_SSH_PASS, + INS_RDP_PASS, + INS_OS_PASS: + data.insert.Pass, _ = c_encrypt_str(ui.buff, + data.opts.GPG) + case INS_SSH_PRIV: data.insert.Priv = ui.buff + case INS_SSH_JUMP_HOST: + data.insert.Jump.Host = ui.buff + if len(ui.buff) > 0 { + data.insert.Jump.Port = 22 + } else { + data.insert.Jump.Port = 0 + } + case INS_SSH_JUMP_PORT: + tmp, _ := strconv.Atoi(ui.buff) + data.insert.Jump.Port = uint16(tmp) + case INS_SSH_JUMP_USER: + data.insert.Jump.User = ui.buff + case INS_SSH_JUMP_PASS: + data.insert.Jump.Pass, _ = + c_encrypt_str(ui.buff, data.opts.GPG) + case INS_SSH_JUMP_PRIV: + data.insert.Jump.Priv = ui.buff + case INS_RDP_DOMAIN: + data.insert.Domain = ui.buff + case INS_RDP_FILE: + data.insert.RDPFile = ui.buff + case INS_CMD_CMD: + data.insert.Host = ui.buff + case INS_CMD_SHELL: + data.insert.Shell[0] = ui.buff + case INS_OS_USERDOMAINID: + data.insert.Stack.UserDomainID = ui.buff + case INS_OS_PROJECTID: + data.insert.Stack.ProjectID = ui.buff + case INS_OS_REGION: + data.insert.Stack.RegionName = ui.buff + case INS_OS_ENDTYPE: + data.insert.Stack.EndpointType = ui.buff + case INS_OS_INTERFACE: + data.insert.Stack.Interface = ui.buff + case INS_OS_IDAPI: + data.insert.Stack.IdentityAPI = ui.buff + case INS_OS_IMGAPI: + data.insert.Stack.ImageAPI = ui.buff + case INS_OS_NETAPI: + data.insert.Stack.NetworkAPI = ui.buff + case INS_OS_VOLAPI: + data.insert.Stack.VolumeAPI = ui.buff + case INS_SSH_NOTE, + INS_RDP_NOTE + len(data.insert.Drive), + INS_CMD_NOTE, + INS_OS_NOTE: + data.insert.Note = ui.buff + } + data.ui.insert_sel_ok = false + ui.buff = "" + ui.s.HideCursor() + } else { + e_readline(event, &data.ui.buff) + } + } + } + } + return false +} + +func e_mkdir_events(data *HardData, 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 = "" + 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 = "" + } else { + e_readline(event, &data.ui.buff) + } + return false } diff --git a/src/i_ui.go b/src/i_ui.go index b19921b..8dc9721 100644 --- a/src/i_ui.go +++ b/src/i_ui.go @@ -649,6 +649,8 @@ func i_init_styles(ui *HardUI) { Foreground(tcell.ColorYellow).Dim(true).Bold(true) } +type key_event_mode_func func(*HardData, tcell.EventKey) bool + func i_ui(data_dir string) { home_dir, _ := os.UserHomeDir() ui := HardUI{} @@ -691,6 +693,15 @@ func i_ui(data_dir string) { data.ui.mode = WELCOME_MODE data.keys = c_get_secret_gpg_keyring() } + fp := [MODE_MAX + 1]key_event_mode_func{ + NORMAL_MODE: e_normal_events, + DELETE_MODE: e_delete_events, + LOAD_MODE: e_load_events, + ERROR_MODE: e_error_events, + WELCOME_MODE: e_welcome_events, + MKDIR_MODE: e_mkdir_events, + INSERT_MODE: e_insert_events, + } for { data.ui.s.Clear() i_draw_bottom_text(data.ui, data.opts, data.insert, data.insert_err) @@ -727,6 +738,6 @@ func i_ui(data_dir string) { } } data.ui.s.Show() - e_events(&data) + e_events(&data, fp) } } |