Python ncurses

From Openmoko

(Difference between revisions)
Jump to: navigation, search
(Writing python apps for the neo that use ncurses)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
== Writing python apps for the neo that use ncurses ==
 
== Writing python apps for the neo that use ncurses ==
  
To get started, visit [[Repositories]] and add ScaredyCat's repository to your list.  Then:
+
To get started, visit [[Users Repositories]] and add ScaredyCat's repository to your list.  Then:
  
 
   ipkg update
 
   ipkg update
   ipkg install python-base python-curses
+
   ipkg install python-core python-curses
  
 
Then I've put together a simple ncurses based app that'll sit in a loop waiting for input, and use ssh to execute xmms remote commands on the host computer.  The full source is below:
 
Then I've put together a simple ncurses based app that'll sit in a loop waiting for input, and use ssh to execute xmms remote commands on the host computer.  The full source is below:
  
<nowiki>
+
  # very helpful: http://www.amk.ca/python/howto/curses/curses.html#SECTION000300000000000000000
<pre><code>
+
 
# very helpful: http://www.amk.ca/python/howto/curses/curses.html#SECTION000300000000000000000
+
  import os
 +
  import curses
 +
  stdscr = curses.initscr()
 +
 
 +
  curses.noecho()
 +
  curses.cbreak()
 +
  stdscr.keypad(1)
 +
 
 +
  host = "192.168.1.200"
 +
  user = "jadams"
 +
  MESSAGE_X = 0
 +
  MESSAGE_Y = 5
 +
 
 +
  def send_xmms_arg(char):
 +
      os.system('ssh ' + user + '@' + host + ' "xmms -' + char + '"')
 +
 
 +
  def write_instructions():
 +
      stdscr.addstr(0, 0, "c: play/pause; b: next; z: prev")
 +
      stdscr.refresh()
 +
 
 +
  def write_message(message):
 +
      stdscr.addstr(MESSAGE_Y, MESSAGE_X, message) # clear out
 +
      stdscr.refresh()
 +
 
 +
  def do_play_pause():
 +
      send_xmms_arg('u')
 +
      write_message("XMMS was paused / unpaused")
 +
 
 +
  def do_next():
 +
      send_xmms_arg('f')
 +
      write_message("Skipped to the next song.")
 +
 
 +
  def do_prev():
 +
      send_xmms_arg('r')
 +
      write_message("Skipped to the previous song.")
 +
 
 +
  write_instructions()
 +
 
 +
  while 1:
 +
      c = stdscr.getch()
 +
      if c == ord('q'): break
 +
      elif c == ord('c'): do_play_pause()
 +
      elif c == ord('b'): do_next()
 +
      elif c == ord('z'): do_prev()
 +
 
 +
  curses.nocbreak(); stdscr.keypad(0); curses.echo()
 +
  curses.endwin()
  
import os
+
[[Category:Application Developer]]
import curses
+
stdscr = curses.initscr()
+
 
+
curses.noecho()
+
curses.cbreak()
+
stdscr.keypad(1)
+
 
+
host = "192.168.1.200"
+
user = "jadams"
+
MESSAGE_X = 0
+
MESSAGE_Y = 5
+
 
+
def send_xmms_arg(char):
+
    os.system('ssh ' + user + '@' + host + ' "xmms -' + char + '"')
+
 
+
def write_instructions():
+
    stdscr.addstr(0, 0, "c: play/pause; b: next; z: prev")
+
    stdscr.refresh()
+
 
+
def write_message(message):
+
    stdscr.addstr(MESSAGE_Y, MESSAGE_X, message) # clear out
+
    stdscr.refresh()
+
 
+
def do_play_pause():
+
    send_xmms_arg('u')
+
    write_message("XMMS was paused / unpaused")
+
 
+
def do_next():
+
    send_xmms_arg('f')
+
    write_message("Skipped to the next song.")
+
 
+
def do_prev():
+
    send_xmms_arg('r')
+
    write_message("Skipped to the previous song.")
+
 
+
write_instructions()
+
 
+
while 1:
+
    c = stdscr.getch()
+
    if c == ord('q'): break
+
    elif c == ord('c'): do_play_pause()
+
    elif c == ord('b'): do_next()
+
    elif c == ord('z'): do_prev()
+
 
+
curses.nocbreak(); stdscr.keypad(0); curses.echo()
+
curses.endwin()
+
</code></pre>
+
</nowiki>
+

Latest revision as of 13:19, 16 August 2008

[edit] Writing python apps for the neo that use ncurses

To get started, visit Users Repositories and add ScaredyCat's repository to your list. Then:

 ipkg update
 ipkg install python-core python-curses

Then I've put together a simple ncurses based app that'll sit in a loop waiting for input, and use ssh to execute xmms remote commands on the host computer. The full source is below:

 # very helpful: http://www.amk.ca/python/howto/curses/curses.html#SECTION000300000000000000000
 
 import os
 import curses
 stdscr = curses.initscr()
 
 curses.noecho()
 curses.cbreak()
 stdscr.keypad(1)
 
 host = "192.168.1.200"
 user = "jadams"
 MESSAGE_X = 0
 MESSAGE_Y = 5
 
 def send_xmms_arg(char):
     os.system('ssh ' + user + '@' + host + ' "xmms -' + char + '"')
 
 def write_instructions():
     stdscr.addstr(0, 0, "c: play/pause; b: next; z: prev")
     stdscr.refresh()
 
 def write_message(message):
     stdscr.addstr(MESSAGE_Y, MESSAGE_X, message) # clear out 
     stdscr.refresh()
 
 def do_play_pause():
     send_xmms_arg('u')
     write_message("XMMS was paused / unpaused")
 
 def do_next():
     send_xmms_arg('f')
     write_message("Skipped to the next song.")
 
 def do_prev():
     send_xmms_arg('r')
     write_message("Skipped to the previous song.")
 
 write_instructions()
 
 while 1:
     c = stdscr.getch()
     if c == ord('q'): break
     elif c == ord('c'): do_play_pause()
     elif c == ord('b'): do_next()
     elif c == ord('z'): do_prev()
 
 curses.nocbreak(); stdscr.keypad(0); curses.echo()
 curses.endwin()
Personal tools

Writing python apps for the neo that use ncurses

To get started, visit Repositories and add ScaredyCat's repository to your list. Then:

 ipkg update
 ipkg install python-base python-curses

Then I've put together a simple ncurses based app that'll sit in a loop waiting for input, and use ssh to execute xmms remote commands on the host computer. The full source is below:

<pre><code> # very helpful: http://www.amk.ca/python/howto/curses/curses.html#SECTION000300000000000000000 import os import curses stdscr = curses.initscr() curses.noecho() curses.cbreak() stdscr.keypad(1) host = "192.168.1.200" user = "jadams" MESSAGE_X = 0 MESSAGE_Y = 5 def send_xmms_arg(char): os.system('ssh ' + user + '@' + host + ' "xmms -' + char + '"') def write_instructions(): stdscr.addstr(0, 0, "c: play/pause; b: next; z: prev") stdscr.refresh() def write_message(message): stdscr.addstr(MESSAGE_Y, MESSAGE_X, message) # clear out stdscr.refresh() def do_play_pause(): send_xmms_arg('u') write_message("XMMS was paused / unpaused") def do_next(): send_xmms_arg('f') write_message("Skipped to the next song.") def do_prev(): send_xmms_arg('r') write_message("Skipped to the previous song.") write_instructions() while 1: c = stdscr.getch() if c == ord('q'): break elif c == ord('c'): do_play_pause() elif c == ord('b'): do_next() elif c == ord('z'): do_prev() curses.nocbreak(); stdscr.keypad(0); curses.echo() curses.endwin() </code></pre>