Created
March 16, 2025 10:30
-
-
Save monperrus/0d1b6e0efda64da3fadd77b28ccd9eaf to your computer and use it in GitHub Desktop.
a python ncurse app with two column
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import curses | |
| import time | |
| def main(stdscr): | |
| # Turn off cursor blinking | |
| curses.curs_set(0) | |
| # Color pair definitions | |
| curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE) | |
| curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_WHITE) | |
| # Get screen height and width | |
| height, width = stdscr.getmaxyx() | |
| # Calculate column width (half of screen width) | |
| col_width = width // 2 | |
| # Create windows for both columns | |
| left_col = curses.newwin(height, col_width, 0, 0) | |
| right_col = curses.newwin(height, col_width, 0, col_width) | |
| # Create borders for both columns | |
| left_col.box() | |
| right_col.box() | |
| # Add titles to columns | |
| left_col.addstr(0, 2, "Left Column", curses.A_BOLD) | |
| right_col.addstr(0, 2, "Right Column", curses.A_BOLD) | |
| # Sample data for both columns | |
| left_items = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"] | |
| right_items = ["Data A", "Data B", "Data C", "Data D", "Data E"] | |
| # Selected item index | |
| selected_left = 0 | |
| selected_right = 0 | |
| while True: | |
| # Display items in left column | |
| for idx, item in enumerate(left_items): | |
| if idx == selected_left: | |
| left_col.attron(curses.color_pair(1)) | |
| left_col.addstr(idx + 2, 2, item) | |
| left_col.attroff(curses.color_pair(1)) | |
| else: | |
| left_col.addstr(idx + 2, 2, item) | |
| # Display items in right column | |
| for idx, item in enumerate(right_items): | |
| if idx == selected_right: | |
| right_col.attron(curses.color_pair(1)) | |
| right_col.addstr(idx + 2, 2, item) | |
| right_col.attroff(curses.color_pair(1)) | |
| else: | |
| right_col.addstr(idx + 2, 2, item) | |
| # Add instructions at the bottom | |
| stdscr.addstr(height-1, 0, "Use arrow keys to navigate. Press 'q' to quit.") | |
| # Refresh the screen | |
| left_col.refresh() | |
| right_col.refresh() | |
| stdscr.refresh() | |
| # Get user input | |
| key = stdscr.getch() | |
| if key == ord('q'): | |
| break | |
| elif key == curses.KEY_UP: | |
| if selected_left > 0: | |
| selected_left -= 1 | |
| elif key == curses.KEY_DOWN: | |
| if selected_left < len(left_items) - 1: | |
| selected_left += 1 | |
| elif key == curses.KEY_LEFT: | |
| selected_right = selected_left | |
| elif key == curses.KEY_RIGHT: | |
| selected_left = selected_right | |
| # Clear the windows for next iteration | |
| left_col.clear() | |
| right_col.clear() | |
| # Redraw borders | |
| left_col.box() | |
| right_col.box() | |
| # Redraw titles | |
| left_col.addstr(0, 2, "Left Column", curses.A_BOLD) | |
| right_col.addstr(0, 2, "Right Column", curses.A_BOLD) | |
| def init(): | |
| curses.wrapper(main) | |
| if __name__ == '__main__': | |
| init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment