191 likes | 474 Views
CURSES!!!!!. FOILED AGAIN!! Liam O’Leary 12/3/07 Seminar: Ruby V. Python Pr. John Santore. Curses and its Purposes :. The curses library is used for displays on various terminals (like the Linux console) and controls simple things like text input, cursor movement, etc.
E N D
CURSES!!!!! FOILED AGAIN!! Liam O’Leary 12/3/07 Seminar: Ruby V. Python Pr. John Santore
Curses and its Purposes: • The curses library is used for displays on various terminals (like the Linux console) and controls simple things like text input, cursor movement, etc. • A common use for curses is on things like OS installers and kernel configurators which run before more complicated graphics and text editors are available
Starting Curses… import curses stdscr = curses.initscr() • initscr() initializes curses and sets up any necessary code to connect to the terminal. • initscr(), creates a window, usually taking up the entire screen, to display curses graphics
Curses functions Pt. I • newwin(height, width, begin y, begin x) – Creates a new curses window (Note: window coordinates in curses are the same as in pygame) • noecho() -Prevents curses from displaying keystrokes on the screen • echo() – Allows curses to display user input from the keyboard • cbreak() – Used to allow curses programs to automatically react to keys, without needing Enter to be pressed
Curses Functions Pt. II (Featuring pad) • refresh() – Refreshes the window, like blit() in pygame • newpad() – Creates a “pad”, which can be bigger than the actual window or just a small section of a window which curses will use • curs_set(0) – Allows you to hide the cursor, should your application not need it
Displaying Text in Curses • Curses in python uses addch/str(y, x, str or ch, attr) to draw characters or strings onto the screen. • The y and x in addstr() is optional, allowing you to specfically place the text where you want it. If omitted, the text is displayed in the upper left corner (0, 0) • addstr() (and initscr) replaces mvaddstr() and waddstr() from the C version of curses, waddstr() creates a new window for the new string and mvaddstr() moves the string to a specified location
Attributes of Curses… • A_BLINK – Causes any string or character written with this attribute to flash on screen • A_BOLD – Makes strings or characters bold or extra bright • A_DIM – Decreases brightness of text by half • A_REVERSE – Puts text in reverse video
More Curses Attributes… • A_STANDOUT – Sets the text to the brightest possible highlight • A_UNDERLINE – Underlines all text with this attribute
Curses inColor • start_color() – Initializes the default color set, needs to be called shortly after initscr() • has_colors() – Returns true if the terminal has the ability to display colors • Init_pair (x, foreground_color, background_color) – Initializes a color pair, with x being the number of the pair
Curses in Color Pt. II • color_pair() – A designated foreground and background color fair curses output, default, color_pair(0), is white on black and can’t be changed • curses eight normal color possibilities are 0) black, 1) red, 2) green, 3) yellow, 4) blue, 5) magenta, 6) cyan (blue-green), and 7) white. Colors are called by the function curses.COLOR_(RED)
Curses and You… • getch() – Allows the user to input text into a curses window, it pauses, waits for a keystroke (assuming echo() was already called) and displays it to the screen. A (y, x) position can be given with getch() if desired • getstr() – Just like getch(), but instead, accepts a string of a user-given length • getmouse() – Accepts mouse clicks as events, represented by a 5-tuple (id, x, y, z, bit-state) id is a value used to distinguish between multiple devices, x and y are the events’ coordinates (z is unused) bit-state is a value used to determine what type of event it is; a button press, a button release, a double click, etc.
Curses and You Pt. II • nodelay() – Turns off the default delay for getch() and gives an error if no input is received • halfdelay(x) – Essentially sets a timer for getch(), if no input is received within the number of milliseconds equal to x, and exception is thrown
Curses and You Pt. III • During getch(), curses only outputs numbers with ASCII values between 0 and 255, the following are functions for keys beyond that scope • curses.KEY_HOME – A function to determine if a keystroke from getch() is, in fact, the HOME key, can used in a function to set the cursor back to the origin • curses.KEY_LEFT (etc) – Same as KEY_HOME, only instead to be used to move the cursor by individual spaces
Fancy Curses… • stdscr.hline(y, x, ch, n) – Draws a horizontal line of n length, starting at (y, x) made of the character ch • stdscr.vline(y, x, ch, n) – Draws a vertical line of n length, starting at (y, x), made of the character ch • beep() – Let’s out a short beeping sound
Curses on the Menu… • Curses can give the appearance of menus, using addstr() and the proper amount of spacing • Creating real menus with curses requires using either Tkinter or another library called dialog to create real menus and buttons
Cursed Curses!!! • Upon finishing an application, curses has a tendency to mess up the terminal, either leaving it the last color used by the application, or not displaying any further input • This problem is solved (at least on csdev01) by typing in reset, while you won’t see it appear on the screen, it will still work, setting the terminal back to normal
More Cursed Curses… • Curses can set colors through RGB with a function called init_color(color, r, g, b). Color is a number representing one of the normal curses colors, (r, g, b) are numerical representations of the amount of red, green or blue desired, ranging from between 0 and 1000 • However, init_color() will only work if the terminal is capable of doing so, which can be determined using can_change_color() (1 indicates true, meaning the terminal can use RGB to determine colors)