Skip to content

Instantly share code, notes, and snippets.

@MitchRatquest
Last active December 22, 2017 21:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MitchRatquest/d2b81145d129a9c28646e5304eaa8f57 to your computer and use it in GitHub Desktop.
Save MitchRatquest/d2b81145d129a9c28646e5304eaa8f57 to your computer and use it in GitHub Desktop.
string scrolling in python with curses
#!/usr/bin/python
import curses
from time import sleep
curses.initscr()
string = "this is a really fun scrolling string "
def scroll(stdscr):
try:
curses.curs_set(0)
except:
pass
curses.start_color()
for i in range(0,curses.COLORS):
curses.init_pair(i+1,i,0)
stdscr.bkgd(" ",curses.A_BOLD | curses.color_pair(0))
display = []
for x in range(0, 400): #blank our display array
display.append(' ')
index = 0
while True:
columns=stdscr.getmaxyx()[1] #always update columns in case window resizes
for x in range(0, columns-1):
display[x] = display[x+1] #shift everything to the left one
display[columns-1] = string[index] #add the next char
index += 1
if(index >= len(string)): #if your index exceeds the string
index = 0 #reset the string index to zero
temp = '' #new clear string for printing
for x in display:
temp += x
columns=stdscr.getmaxyx()[1] #if you try to draw outside of the column, it will crash
stdscr.addstr(0,0,temp[:columns],curses.color_pair(0))
stdscr.refresh()
sleep(.09) #scroll speed
if __name__ == '__main__':
curses.wrapper(scroll)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment