#!/usr/bin/env python ## BASIC CURSES # Basic.py ## # This is my old chat client GUI but with all the unnecessaries removed. # ## ### Verson History? Anyone? ... Buehler? ## Thursday, April 5th, 2006 # Mmhmm. Today was mostly spent stripping my Chat GUI down to a # basic level of user interaction. I removed the network code, # the chatting code (though it may make a return), and removed # the two chat windows from existence. As of right now, the # system merely waits for input from the user, and allows the # user to select menus, although not their options yet. # # My goals for this coming Saturday are to work on user interactions, # such as allowing users to select menu options with the arrow keys. I # would also like to create some method of tabs, to allow the user to # switch between several pages of information, and a pop-up window # function for option windows and prompts. And if time allows, I would # like to create some means for browsing and navigating through # information within the tabs, including scrolling. # # My other goal is to figure out what I'm doing aside from creating a # GUI. As of right now I'm looking at PySoulseek, a peer-to-peer program # written in Python that allows sharing music with others and also # serves as a music player. This is a large program, and I think I'm # over-estimating my time, but I could at least try and implement the # mp3 player. There's really a lot out there in terms of python and # networking, and I'm really limited by my knowledge of Python outside # of the simple networking and interface work I've been doing. ## ## Wednesday, March 28th, 2006 # This was my first time playing around with curses since Spring Break, # and I mostly did minor tweaking. I fixed up the chat section of the # GUI, and updated several of the GUI-related functions. But the # biggest thing I did was add a menu bar and actually got it working. # This involved figuring out how to identify when some pressed Ctrl-f, # which was tougher than it sounds, at least for me. I ended up # converting the Ascii code into printable format, and then checked for # what printed out when I hit Ctrl-f, which was ^F, simple enough. So # with that fixed, I'm about ready to go. ## ### import curses, curses.ascii, socket, traceback, os, sys, select ## INTERFACE MODULES ## def redraw_line(window, width, height, x_after_question, y): ## Clears away part of a line x = width-1 while (x > x_after_question): window.delch(y, x) x -= 1 window.delch(y, x) def fill_line(window,width,header,letter,color,y): ## Creates the title line for windows x = 0 while (x < width): window.addch(y,x,letter,curses.color_pair(color)) x += 1 window.addstr(y,(width-len(header))/2,header,curses.color_pair(color)) window.noutrefresh() def draw_borders(window,width,height): ## Draws the borders of the windows window.vline(0,0,'|',height-1) window.vline(0,width-1,'|',height-1) window.hline(height-1,0,'+',width-1) window.hline(height-1,1,'-',width-1) window.hline(height-1,width-1,'+',1) window.noutrefresh() def create_menu_bar(window,width): x = 2 y = 0 draw_borders(window,width,1) option = "File" create_menu_option(window,width,y,x,0,option1) x += len(option) + 2 option = "Edit" create_menu_option(window,width,y,x,0,option2) x += len(option) + 2 option = "Chat Options" create_menu_option(window,width,y,x,1,option3) x += len(option) + 2 window.refresh() def create_menu_option(window,width,y,x,highlighted_letter,option): window.addstr(y,x,option,curses.color_pair(0)) window.addch(y,x+highlighted_letter,option[highlighted_letter],\ curses.color_pair(3)) def hightlight_menu_option(window,y,x,option): # Highlight Text window.addstr(y,x,option,curses.color_pair(2)) # Draw Option Menu option_menu = curses.newwin(10,10,y+2,x) draw_borders(option_menu,10,10) # Draw Menu Options option_menu.addstr(0,1,"Option 1",curses.color_pair(0)) option_menu.addch(0,8,'1',curses.color_pair(3)) option_menu.addstr(1,1,"Option 2",curses.color_pair(0)) option_menu.addch(1,8,'2',curses.color_pair(3)) option_menu.addstr(2,1,"Option 3",curses.color_pair(0)) option_menu.addch(2,8,'3',curses.color_pair(3)) option_menu.refresh() def draw_screen(): # Fill Title Lines fill_line(stdscr, 80, program_title,' ',2,0) # Drawing Windows draw_borders(stdscr, 80, 30) # Creates Menu Bar create_menu_bar(menu_bar,menu_bar_width) """ ## CHAT MODULES ## # Chat (Window1, Window2, Window Width, Window Height, Question to Ask) def chat(window,output_s_win,menu_bar,stdscr,width,height,question): ## Asks the user a question, returns the response # Initializes Chat Variables window.keypad(1) # Allows for special keys, e.g. backspace y = height/2 orig_x = 2 response = '' next_line = 0 window.addstr(y, orig_x, question, curses.color_pair(1)) window.addch(' ') x_after_question = orig_x + len(question) x = x_after_question # Moves x past the question redraw_line(window,width,height,x_after_question,y) while(1): window.addch(y, width-1, '|') window.move(y,x) input = window.getch(y, x) if input == ord('\n') and x > x_after_question: redraw_line(window,width,height,x_after_question,y) draw_borders(window,width,height) x = x_after_question break elif x == width-3: redraw_line(window,width,height,x_after_question,y) draw_borders(window,width,height) x = x_after_question window.addch(y, x_after_question-1,'/',curses.color_pair(1)) next_line = 1 elif input == curses.KEY_BACKSPACE and x > x_after_question: x -= 1 # Moves input to last space response = response[0:-1] # Removes the last char window.delch(y, width-1) # Deletes right border window.delch(y, x) window.vline(1,width-1,'|',1) # Redraws Border elif next_line == 1 and \ input == curses.KEY_BACKSPACE and x == x_after_question: window.addstr(response) x += len(response) window.addch(y, x_after_question-1,' ') next_line = 0 elif curses.ascii.unctrl(input) == "^R": draw_screen() elif curses.ascii.unctrl(input) == "^F": draw_screen() hightlight_menu_option(menu_bar,0,2,option1) menu_bar.refresh() elif curses.ascii.unctrl(input) == "^E": draw_screen() hightlight_menu_option(menu_bar,0,2+len(option1)+2,option2) menu_bar.refresh() elif curses.ascii.unctrl(input) == "^H": draw_screen() hightlight_menu_option(menu_bar,0,2+len(option1)+2+len(option2)+2\ ,option3) menu_bar.refresh() elif curses.ascii.isctrl(input): pass elif input > 255 or input == 10: # Ignores non-ASCII keys and newline window.delch(y,x) window.delch(y,width-2) draw_borders(window,width,height) else: x += 1 # Moves to next space response += curses.ascii.unctrl(input) window.addch(input) next_line = 0 return response """ ## MAIN CODE ## ## Initializing window interface ## (Yes I will continue to use that word) try: # Initializes Main Window global stdscr stdscr = curses.initscr() # Open Curses Window curses.cbreak() # Takes input before the enter key is pressed curses.noecho() # Stops echo of input to screen curses.curs_set(1) # Sets the cursor visibility, 0-2 # Intializes Menu Options and Title global option1, option2, option3, program_title option1 = "File" option2 = "Edit" option3 = "Chat Options" program_title = "Chat Program - Press Ctrl-C to Exit" # Initializes default color combinations curses.start_color() curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK) curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_CYAN) curses.init_pair(3, curses.COLOR_CYAN, curses.COLOR_BLACK) # Creates Windows global menu_bar_width, menu_bar_height, menu_bar menu_bar_width = 80 menu_bar_height = 1 menu_bar = curses.newwin(menu_bar_height, menu_bar_width, 1, 0) draw_screen() live = 1 # Begin Interacting With User while(live): curses.curs_set(0) # Makes Cursor invisible, 0-2 stdscr.move(0,0) # Moves cursorp input = stdscr.getch(0,0) # Takes input at location # Decides what to do with the input if curses.ascii.unctrl(input) == "^F": draw_screen() hightlight_menu_option(menu_bar,0,2,option1) menu_bar.refresh() elif curses.ascii.unctrl(input) == "^E": draw_screen() hightlight_menu_option(menu_bar,0,2+len(option1)+2,option2) menu_bar.refresh() elif curses.ascii.unctrl(input) == "^H": draw_screen() hightlight_menu_option(menu_bar,0,2+len(option1)+2+len(option2)+2\ ,option3) menu_bar.refresh() stdscr.keypad(0) curses.nocbreak() curses.echo() curses.endwin() # Closes Curses Window print "Thanks!!" except: # Returns terminal to a normal state if error stdscr.keypad(0) curses.nocbreak() curses.echo() curses.endwin() # Closes Curses Window print "Thanks!!!!"