Update key bindings for convenience
This commit is contained in:
parent
420eb47583
commit
81235aa774
14
README.md
14
README.md
|
@ -10,16 +10,16 @@ Supports mouse navigation as well as keyboard navigation.
|
||||||
## Key bindings
|
## Key bindings
|
||||||
### For kubectl
|
### For kubectl
|
||||||
You can customize these bindings or add extra bindings in `KEY_BINDINGS` variable of `kls` in a row #5:
|
You can customize these bindings or add extra bindings in `KEY_BINDINGS` variable of `kls` in a row #5:
|
||||||
- `1` or `Enter` - get yaml of resource
|
- `g` - get yaml of resource
|
||||||
- `2` - describe resource
|
- `d` - describe resource
|
||||||
- `3` - edit resource
|
- `e` - edit resource
|
||||||
- `4` - logs of pod
|
- `l` - logs of pod
|
||||||
- `5` - exec to pod
|
- `Enter` - exec to pod
|
||||||
- `6` - network debug of pod (with nicolaka/netshoot container attached)
|
- `n` - network debug of pod (with nicolaka/netshoot container attached)
|
||||||
- `delete` - delete resource
|
- `delete` - delete resource
|
||||||
|
|
||||||
### Other:
|
### Other:
|
||||||
- letters - enter filter mode and apply filter
|
- / - enter filter mode
|
||||||
- `Escape` - exit filter mode or `kls` itself
|
- `Escape` - exit filter mode or `kls` itself
|
||||||
- `Backspace` - remove letter from filter
|
- `Backspace` - remove letter from filter
|
||||||
- `TAB`, arrow keys, `PgUp`, `PgDn`, `Home`, `End` - navigation
|
- `TAB`, arrow keys, `PgUp`, `PgDn`, `Home`, `End` - navigation
|
||||||
|
|
62
kls
62
kls
|
@ -3,21 +3,20 @@ import subprocess, curses, time
|
||||||
|
|
||||||
# constants
|
# constants
|
||||||
KEY_BINDINGS = { # can be extended
|
KEY_BINDINGS = { # can be extended
|
||||||
"1": 'kubectl -n {namespace} get {api_resource} {resource} -o yaml | batcat -l yaml',
|
"y": 'kubectl -n {namespace} get {api_resource} {resource} -o yaml | batcat -l yaml',
|
||||||
"\n": 'kubectl -n {namespace} get {api_resource} {resource} -o yaml | batcat -l yaml', # Enter key
|
"d": 'kubectl -n {namespace} describe {api_resource} {resource} | batcat -l yaml',
|
||||||
"2": 'kubectl -n {namespace} describe {api_resource} {resource} | batcat -l yaml',
|
"e": 'kubectl -n {namespace} edit {api_resource} {resource}',
|
||||||
"3": 'kubectl -n {namespace} edit {api_resource} {resource}',
|
"l": 'kubectl -n {namespace} logs {resource} | batcat -l log',
|
||||||
"4": 'kubectl -n {namespace} logs {resource} | batcat -l log',
|
"\n": 'kubectl -n {namespace} exec -it {resource} sh',
|
||||||
"5": 'kubectl -n {namespace} exec -it {resource} sh',
|
"n": 'kubectl -n {namespace} debug {resource} -it --image=nicolaka/netshoot', # network debug
|
||||||
"6": 'kubectl -n {namespace} debug {resource} -it --image=nicolaka/netshoot',
|
|
||||||
"KEY_DC": 'kubectl -n {namespace} delete {api_resource} {resource}' # KEY_DC is the delete key
|
"KEY_DC": 'kubectl -n {namespace} delete {api_resource} {resource}' # KEY_DC is the delete key
|
||||||
}
|
}
|
||||||
BATCAT_STYLE = " --paging always --style numbers"
|
BATCAT_STYLE = " --paging always --style numbers"
|
||||||
# which api resources are on the top of menu?
|
# which api resources are on the top of menu?
|
||||||
TOP_API_RESOURCES = ["pods", "services", "configmaps", "secrets", "persistentvolumeclaims", "ingresses", "nodes",
|
TOP_API_RESOURCES = ["pods", "services", "configmaps", "secrets", "persistentvolumeclaims", "ingresses", "nodes",
|
||||||
"deployments", "statefulsets", "daemonsets", "storageclasses"]
|
"deployments", "statefulsets", "daemonsets", "storageclasses"]
|
||||||
HELP_TEXT = ("letters: filter mode, Esc: exit filter mode or exit kls, 1/Enter: get yaml, 2: describe, 3: edit, "
|
HELP_TEXT = ("/ : filter mode, Esc: exit filter mode or exit kls, Enter: exec into pod, d: describe, e: edit, "
|
||||||
"4: logs, 5: exec, 6: debug, arrows/TAB/PgUp/PgDn: navigation")
|
"l: logs, n: network debug, arrows/TAB/PgUp/PgDn: navigation")
|
||||||
MOUSE_ENABLED = False
|
MOUSE_ENABLED = False
|
||||||
SCREEN = curses.initscr() # screen initialization, needed for ROWS_HEIGHT working
|
SCREEN = curses.initscr() # screen initialization, needed for ROWS_HEIGHT working
|
||||||
HEADER_HEIGHT = 4 # in rows
|
HEADER_HEIGHT = 4 # in rows
|
||||||
|
@ -46,6 +45,7 @@ class Menu:
|
||||||
self.title = title
|
self.title = title
|
||||||
self.rows = rows # all rows
|
self.rows = rows # all rows
|
||||||
self.filter = "" # filter for rows
|
self.filter = "" # filter for rows
|
||||||
|
self.filter_mode = False # Tracks whether filter mode is active
|
||||||
self.filtered_rows = CircularList([x for x in self.rows if self.filter in x]) # filtered rows
|
self.filtered_rows = CircularList([x for x in self.rows if self.filter in x]) # filtered rows
|
||||||
self.visible_rows = lambda: self.filtered_rows[:rows_height] # visible rows
|
self.visible_rows = lambda: self.filtered_rows[:rows_height] # visible rows
|
||||||
self.visible_row_index = 0 # index of the selected visible row
|
self.visible_row_index = 0 # index of the selected visible row
|
||||||
|
@ -72,19 +72,22 @@ def draw_menu(menu: Menu):
|
||||||
menu.win.erase() # clear menu window
|
menu.win.erase() # clear menu window
|
||||||
draw_row(menu.win, menu.title, 1, 2, selected=True if menu == selected_menu else False) # draw title
|
draw_row(menu.win, menu.title, 1, 2, selected=True if menu == selected_menu else False) # draw title
|
||||||
draw_rows(menu) # draw menu rows
|
draw_rows(menu) # draw menu rows
|
||||||
draw_row(menu.win, f"/{menu.filter}" if menu.filter else "", curses.LINES - FOOTER_HEIGHT - 2, 2) # draw filter row
|
draw_row(menu.win, f"/{menu.filter}" if menu.filter_mode else "", curses.LINES - FOOTER_HEIGHT - 2, 2) # draw filter row
|
||||||
|
|
||||||
|
|
||||||
def refresh_third_menu(namespace: str, api_resource: str):
|
def refresh_third_menu(namespace, api_resource):
|
||||||
menu = menus[2]
|
menu = menus[2]
|
||||||
previous_menu_rows = menu.rows
|
previous_menu_rows = menu.rows
|
||||||
if api_resource and namespace:
|
if api_resource and namespace:
|
||||||
menu.rows = kubectl(f"-n {namespace} get {api_resource} --no-headers --ignore-not-found")
|
try:
|
||||||
index_before_update = menu.filtered_rows.index
|
menu.rows = kubectl(f"-n {namespace} get {api_resource} --no-headers --ignore-not-found")
|
||||||
menu.filtered_rows = CircularList([x for x in menu.rows if menu.filter in x]) # update filtered rows
|
except subprocess.CalledProcessError:
|
||||||
menu.filtered_rows.index = index_before_update
|
menu.rows = [] # Fallback to an empty list if the command fails
|
||||||
else:
|
else:
|
||||||
menu.rows = []
|
menu.rows = []
|
||||||
|
index_before_update = menu.filtered_rows.index
|
||||||
|
menu.filtered_rows = CircularList([x for x in menu.rows if menu.filter in x]) # update filtered rows
|
||||||
|
menu.filtered_rows.index = index_before_update
|
||||||
if menu.visible_row_index >= len(menu.visible_rows()):
|
if menu.visible_row_index >= len(menu.visible_rows()):
|
||||||
menu.visible_row_index = 0
|
menu.visible_row_index = 0
|
||||||
if previous_menu_rows != menu.rows:
|
if previous_menu_rows != menu.rows:
|
||||||
|
@ -94,7 +97,7 @@ def refresh_third_menu(namespace: str, api_resource: str):
|
||||||
def handle_key_bindings(key: str, namespace: str, api_resource: str, resource: str):
|
def handle_key_bindings(key: str, namespace: str, api_resource: str, resource: str):
|
||||||
if not resource:
|
if not resource:
|
||||||
return
|
return
|
||||||
if key in ("4", "5", "6") and api_resource != "pods" and not resource.startswith("pod/"):
|
if key in ("l", "\n", "n") and api_resource != "pods" and not resource.startswith("pod/"):
|
||||||
return
|
return
|
||||||
curses.def_prog_mode() # save the previous terminal state
|
curses.def_prog_mode() # save the previous terminal state
|
||||||
curses.endwin() # without this, there are problems after exiting vim
|
curses.endwin() # without this, there are problems after exiting vim
|
||||||
|
@ -108,16 +111,20 @@ def handle_key_bindings(key: str, namespace: str, api_resource: str, resource: s
|
||||||
|
|
||||||
|
|
||||||
def handle_filter_state(key: str, menu: Menu):
|
def handle_filter_state(key: str, menu: Menu):
|
||||||
if key in ["KEY_BACKSPACE", "\x08"] and not menu.filter:
|
if key == "/" and not menu.filter_mode: # Enter filter mode
|
||||||
return
|
menu.filter_mode = True
|
||||||
elif key == "\x1b" and not menu.filter:
|
menu.filter = ""
|
||||||
globals().update(selected_menu=None) # exit
|
elif key == "\x1b": # Escape key
|
||||||
elif key == "\x1b":
|
if menu.filter_mode: # Exit filter mode
|
||||||
menu.filter = "" # Escape key exits filter mode
|
menu.filter_mode = False
|
||||||
elif key in ["KEY_BACKSPACE", "\x08"]:
|
menu.filter = ""
|
||||||
menu.filter = menu.filter[:-1] # Backspace key deletes a character (\x08 is also Backspace)
|
else:
|
||||||
elif key.isalpha() or key == "-":
|
globals().update(selected_menu=None) # Exit program
|
||||||
menu.filter += key.lower()
|
elif menu.filter_mode: # Only process filter input in filter mode
|
||||||
|
if key in ["KEY_BACKSPACE", "\x08"]:
|
||||||
|
menu.filter = menu.filter[:-1] # Remove last character
|
||||||
|
elif key.isalnum() or key == "-": # Allow letters, numbers, and dashes
|
||||||
|
menu.filter += key.lower()
|
||||||
menu.visible_row_index = 0
|
menu.visible_row_index = 0
|
||||||
menu.filtered_rows = CircularList([x for x in menu.rows if menu.filter in x]) # update filtered rows
|
menu.filtered_rows = CircularList([x for x in menu.rows if menu.filter in x]) # update filtered rows
|
||||||
draw_menu(menu)
|
draw_menu(menu)
|
||||||
|
@ -125,6 +132,7 @@ def handle_filter_state(key: str, menu: Menu):
|
||||||
menus[2].visible_row_index = 0 # reset the visible row index of third menu before redrawing
|
menus[2].visible_row_index = 0 # reset the visible row index of third menu before redrawing
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def handle_mouse(menu: Menu):
|
def handle_mouse(menu: Menu):
|
||||||
if not MOUSE_ENABLED:
|
if not MOUSE_ENABLED:
|
||||||
return
|
return
|
||||||
|
@ -202,7 +210,7 @@ def catch_input(menu: Menu):
|
||||||
handle_mouse(menu)
|
handle_mouse(menu)
|
||||||
elif key in KEY_BINDINGS.keys():
|
elif key in KEY_BINDINGS.keys():
|
||||||
handle_key_bindings(key, namespace(), api_resource(), resource())
|
handle_key_bindings(key, namespace(), api_resource(), resource())
|
||||||
elif key in ["\x1b", "KEY_BACKSPACE", "\x08"] or key.isalpha() or key == "-": # \x1b - escape, \x08 - backspace
|
elif key in ["/", "\x1b", "KEY_BACKSPACE", "\x08"] or key.isalnum() or key == "-": # \x1b - escape, \x08 - backspace
|
||||||
handle_filter_state(key, menu)
|
handle_filter_state(key, menu)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue