Fix shell

This commit is contained in:
Digital Studium 2024-04-11 23:57:07 +03:00
parent 2212c58a81
commit 4131bf0a6a
6 changed files with 62 additions and 36 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
.idea .idea
venv

View File

@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4"> <module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager"> <component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" /> <content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" /> <orderEntry type="jdk" jdkName="Python 3.11 (kls)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

View File

@ -3,5 +3,5 @@
<component name="Black"> <component name="Black">
<option name="sdkName" value="Python 3.11" /> <option name="sdkName" value="Python 3.11" />
</component> </component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (kls)" project-jdk-type="Python SDK" />
</project> </project>

BIN
a.out Executable file

Binary file not shown.

20
c_kls.c Normal file
View File

@ -0,0 +1,20 @@
#include <ncurses.h>
int main()
{
initscr(); /* Start curses mode */
printw("Hello World !!!\n"); /* Print Hello World */
refresh(); /* Print it on to the real screen */
def_shell_mode(); /* Save the tty modes */
endwin(); /* End curses mode temporarily */
system("vim"); /* Do whatever you like in cooked mode */
reset_shell_mode(); /* Return to the previous tty mode*/
/* stored by def_prog_mode() */
refresh(); /* Do refresh() to restore the */
/* Screen contents */
printw("Another String\n"); /* Back to curses use the full */
refresh(); /* capabilities of curses */
endwin(); /* End curses mode */
return 0;
}

73
kls
View File

@ -2,8 +2,9 @@
import curses import curses
import subprocess import subprocess
stdscr = None screen = None
curses.set_escdelay(1) # в curses зачем-то сделали задержку на срабатывание Escape, уменьшаем её до 1 милисекунды (до 0 нельзя) # в curses зачем-то сделали задержку на срабатывание Escape, уменьшаем её до 1 милисекунды (до 0 нельзя)
curses.set_escdelay(1)
running = True running = True
# состояния меню # состояния меню
@ -15,12 +16,12 @@ NOT_SELECTED_WITH_SEARCH = 4
# я не знаю, что делается в этой функции. # я не знаю, что делается в этой функции.
def init_screen(): def init_screen():
global stdscr global screen
stdscr = curses.initscr() screen = curses.initscr()
stdscr.refresh() screen.refresh()
curses.noecho() curses.noecho()
curses.cbreak() curses.cbreak()
stdscr.keypad(True) screen.keypad(True)
curses.curs_set(0) curses.curs_set(0)
@ -66,24 +67,8 @@ menu3 = Menu("Resources", pods, 0 + curses.COLS // 3 * 2, NOT_SELECTED_WITHOUT_S
menus = [menu1, menu2, menu3] menus = [menu1, menu2, menu3]
# def run_command(command, menu, rows=None): def run_command(command):
# namespace = menu1.rows[menu1.row] subprocess.call(eval(command), shell=True)
# api_resource = menu2.rows[menu2.row]
# resource = menu3.rows[menu3.row]
# subprocess.call(eval(command), shell=True)
# init_screen()
# for menu in menus:
# menu.win.clear()
# menu.win.box()
# menu.win.addstr(1, 2, menu.name)
# menu.win.addstr(curses.LINES - 2, 2, menu.filter)
# if rows and menu.name == "Resources":
# menu.rows = rows
# menu.row = 0
# for index, row in enumerate(menu.rows):
# menu.win.addstr(index + 3, 2, row)
# menu.win.addstr(menu.row + 3, 2, menu.rows[menu.row], curses.A_REVERSE | curses.A_ITALIC)
# menu.win.addstr(1, 2, menu.name, curses.A_REVERSE | curses.A_ITALIC)
def update_menu3(): def update_menu3():
@ -108,6 +93,7 @@ def draw_menu(menu):
draw_rows(menu) # рисуем строки меню draw_rows(menu) # рисуем строки меню
draw_search_box(menu) # рисуем строку поиска draw_search_box(menu) # рисуем строку поиска
def draw_header(menu): def draw_header(menu):
if menu.state in [1, 2]: if menu.state in [1, 2]:
menu.win.addstr(1, 2, menu.name, curses.A_REVERSE | curses.A_ITALIC) menu.win.addstr(1, 2, menu.name, curses.A_REVERSE | curses.A_ITALIC)
@ -140,7 +126,7 @@ def draw_search_box(menu):
def catch_input(menu): def catch_input(menu):
global running global running
key_pressed = stdscr.getkey() key_pressed = screen.getkey()
match key_pressed: match key_pressed:
case "/": case "/":
menu.state = SELECTED_WITH_SEARCH menu.state = SELECTED_WITH_SEARCH
@ -173,6 +159,25 @@ def catch_input(menu):
navigate_vertically("down", menu) navigate_vertically("down", menu)
case "KEY_UP": case "KEY_UP":
navigate_vertically("up", menu) navigate_vertically("up", menu)
case 'g':
if menu3.state in [1, 2]:
# update_menu3()
if menu3.rows:
if not menu3.rows[menu3.row].startswith("No resources"):
curses.def_shell_mode()
curses.endwin()
command = "f'kubectl -n {menu1.rows[menu1.row]} get {menu2.rows[menu2.row]} {menu3.rows[menu3.row]} -o yaml | batcat -l yaml --paging always --style numbers'"
run_command(command)
curses.reset_shell_mode()
# case 'd':
# if current_menu == 2 and not menus[2].rows[menus[2].row].startswith("No resources"):
# run_command("f'kubectl -n {namespace} describe {api_resource} {resource} | batcat -l yaml --paging always --style numbers'", current_menu)
# case 'l':
# if current_menu == 2 and not menus[2].rows[menus[2].row].startswith("No resources") and menus[1].rows[menus[1].row] == "pods":
# run_command("f'kubectl -n {namespace} logs {resource} | batcat -l log --paging always --style numbers'", current_menu)
# case 'e':
# if current_menu == 2 and not menus[2].rows[menus[2].row].startswith("No resources"):
# run_command("f'kubectl edit {api_resource} -n {namespace} {resource}'", current_menu)
case _: case _:
if menu.state == SELECTED_WITH_SEARCH and key_pressed.isalpha() or key_pressed == "-": # объекты в кубе не могут иметь иных символов кроме a-z и - if menu.state == SELECTED_WITH_SEARCH and key_pressed.isalpha() or key_pressed == "-": # объекты в кубе не могут иметь иных символов кроме a-z и -
menu.filter += key_pressed menu.filter += key_pressed
@ -213,9 +218,9 @@ def navigate_vertically(direction, menu):
menu.win.addstr(menu.row + 3, 2, filtered_rows[menu.row], curses.A_REVERSE | curses.A_ITALIC) # и выделяем её menu.win.addstr(menu.row + 3, 2, filtered_rows[menu.row], curses.A_REVERSE | curses.A_ITALIC) # и выделяем её
def main(stdscr): def main(screen):
global running global running
stdscr.refresh() screen.refresh()
# начальный экран # начальный экран
for menu in menus: for menu in menus:
draw_menu(menu) draw_menu(menu)
@ -407,12 +412,12 @@ def main(stdscr):
# navigate_vertically("down", current_menu) # navigate_vertically("down", current_menu)
# case "KEY_UP": # case "KEY_UP":
# navigate_vertically("up", current_menu) # navigate_vertically("up", current_menu)
# main(stdscr) main(screen)
curses.wrapper(main) # curses.wrapper(main)
# curses.nocbreak() curses.nocbreak()
# stdscr.keypad(False) screen.keypad(False)
# curses.echo() curses.echo()
# curses.endwin() curses.endwin()
# subprocess.call(["clear"]) subprocess.call(["clear"])
# #