Projects STRLCPY hiphp Commits 5dd78d8c
🤬
Revision indexing in progress... (symbol navigation in revisions will be accurate after indexed)
  • ■ ■ ■ ■ ■ ■
    menu.py
    1  -#!/usr/bin/env python
    2  -# coding:utf-8
    3  -# | |
    4  -# --+----------------------------------------------------------+--
    5  -# | Code by : yasserbdj96 |
    6  -# | Email : [email protected] |
    7  -# | Github : https://github.com/yasserbdj96 |
    8  -# | BTC : bc1q2dks8w8uurca5xmfwv4jwl7upehyjjakr3xga9 |
    9  -# --+----------------------------------------------------------+--
    10  -# | all posts #yasserbdj96 ,all views my own. |
    11  -# --+----------------------------------------------------------+--
    12  -# | |
    13  - 
    14  -#START{
    15  -import curses
    16  -import json
    17  -import subprocess
    18  - 
    19  -def main_menu(stdscr):
    20  - curses.curs_set(0)
    21  - curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
    22  - stdscr.clear()
    23  - stdscr.refresh()
    24  - 
    25  - with open('menu_options.json', 'r') as f:
    26  - options = json.load(f)
    27  - 
    28  - option_names = list(options.keys())
    29  - selected_option = 0
    30  - 
    31  - while True:
    32  - stdscr.clear()
    33  - height, width = stdscr.getmaxyx()
    34  - 
    35  - for index, option in enumerate(option_names):
    36  - x = width//2 - len(option)//2
    37  - y = height//2 - len(option_names)//2 + index
    38  - if index == selected_option:
    39  - stdscr.attron(curses.color_pair(1))
    40  - stdscr.addstr(y, x, option)
    41  - stdscr.attroff(curses.color_pair(1))
    42  - 
    43  - stdscr.refresh()
    44  - 
    45  - key = stdscr.getch()
    46  - if key == curses.KEY_UP and selected_option > 0:
    47  - selected_option -= 1
    48  - elif key == curses.KEY_DOWN and selected_option < len(option_names) - 1:
    49  - selected_option += 1
    50  - elif key in [curses.KEY_ENTER, 10, 13]:
    51  - selected_option_name = option_names[selected_option]
    52  - selected_option_command = options[selected_option_name]
    53  - if selected_option_command == "exit()":
    54  - exit()
    55  - elif isinstance(selected_option_command, dict):
    56  - selected_option_names = list(selected_option_command.keys())
    57  - selected_option_names.remove("back") # remove the back option from the list
    58  - selected_submenu_option = 0
    59  - while True:
    60  - stdscr.clear()
    61  - height, width = stdscr.getmaxyx()
    62  - 
    63  - for index, option in enumerate(selected_option_names):
    64  - x = width//2 - len(option)//2
    65  - y = height//2 - len(selected_option_names)//2 + index
    66  - if index == selected_submenu_option:
    67  - stdscr.attron(curses.color_pair(1))
    68  - stdscr.addstr(y, x, option)
    69  - stdscr.attroff(curses.color_pair(1))
    70  - 
    71  - stdscr.refresh()
    72  - 
    73  - submenu_key = stdscr.getch()
    74  - if submenu_key == curses.KEY_UP and selected_submenu_option > 0:
    75  - selected_submenu_option -= 1
    76  - elif submenu_key == curses.KEY_DOWN and selected_submenu_option < len(selected_option_names) - 1:
    77  - selected_submenu_option += 1
    78  - elif submenu_key in [curses.KEY_ENTER, 10, 13]:
    79  - selected_submenu_name = selected_option_names[selected_submenu_option]
    80  - selected_submenu_command = selected_option_command[selected_submenu_name]
    81  - if selected_submenu_command == "back":
    82  - break # break out of the submenu loop and go back to main menu
    83  - else:
    84  - stdscr.clear()
    85  - stdscr.addstr(0, 0, f"Running: {selected_submenu_command}\n")
    86  - stdscr.refresh()
    87  - process = subprocess.Popen(selected_submenu_command, stdout=subprocess.PIPE, shell=True)
    88  - while True:
    89  - output = process.stdout.readline()
    90  - if output == b'' and process.poll() is not None:
    91  - break
    92  - if output:
    93  - stdscr.addstr(output.decode())
    94  - stdscr.refresh()
    95  - stdscr.addstr("Press any key to continue")
    96  - stdscr.refresh()
    97  - stdscr.getch()
    98  - break
    99  - selected_option = 0
    100  - 
    101  - else:
    102  - stdscr.clear()
    103  - stdscr.addstr(0, 0, f"Running: {selected_option_command}\n")
    104  - stdscr.refresh()
    105  - process = subprocess.Popen(selected_option_command, stdout=subprocess.PIPE, shell=True)
    106  - while True:
    107  - output = process.stdout.readline()
    108  - if output == b"":
    109  - break
    110  - stdscr.addstr(output.decode())
    111  - stdscr.refresh()
    112  - stdscr.addstr("Process completed. Press any key to continue.\n")
    113  - stdscr.refresh()
    114  - stdscr.getch()
    115  - 
    116  - 
    117  -curses.wrapper(main_menu)
    118  -#}END.
Please wait...
Page is in error, reload to recover