def main(): session = getsession() session.activity = u'hanging out in voting script' term = getterminal() echo(term.clear()) db = DBProxy(databasename) if not 'questions' in db: generate_database() while True: echo(term.clear()) # clears the screen and displays the vote art header for line in showart(os.path.join(os.path.dirname(__file__),'art','vote.ans'),'topaz'): echo(term.cyan+term.move_x((term.width/2)-40)+line) if 'sysop' in session.user.groups: spacing = 1 else: spacing = 7 echo(' ') echo(term.magenta+'\n ('+term.cyan+'r'+term.magenta+')'+term.white+'esults'+' '*spacing) echo(term.magenta+'('+term.cyan+'v'+term.magenta+')'+term.white+'ote on a question'+' '*spacing) echo(term.magenta+'('+term.cyan+'a'+term.magenta+')'+term.white+'dd a new question'+' '*spacing) if 'sysop' in session.user.groups: echo(term.magenta+'('+term.cyan+'d'+term.magenta+')'+term.white+'elete a question'+' '*spacing) echo(term.magenta+'('+term.cyan+'q'+term.magenta+')'+term.white+'uit') echo(term.magenta+'\r\n\r\n\r\nx/84 voting booth command: ') le = LineEditor(30) le.colors['highlight'] = term.cyan inp = le.read() inp = inp.lower() # makes the input indifferent to wheter you used lower case when typing in a command or not.. if 'sysop' in session.user.groups and inp == 'd': while 1: questionnumber = query_question() if questionnumber == 999: break delete_question(questionnumber) elif inp == 'r': while 1: questionnumber = query_question() if questionnumber == 999: break list_results(questionnumber) elif inp == 'v': while 1: questionnumber = query_question() if questionnumber == 999: break vote(questionnumber) elif inp == 'a': add_question() elif inp == 'q': return else: echo(term.red+'\r\nNo such command. Try again.\r\n') # if no valid key is pressed then do some ami/x esthetics. waitprompt()
def add_question(): term = getterminal() db = DBProxy(databasename) questions = [] amount_of_alternatives = [] index = {} alternatives = {} results = {} index = db['index'] questions = db['questions'] alternatives = db['alternatives'] results = db['results'] amount_of_alternatives = db['amount_of_alternatives'] amount_of_questions = len(questions) echo(term.clear + term.white + u'\r\nQuestion: ') le = LineEditor(65) new_question = le.read() if new_question == '' or new_question == None: return echo( term.bold_black(u'\r\n\r\nLeave a blank line when you are finished..')) new_amount = 0 while True: echo(term.normal + term.white + u'\r\nchoice ' + term.red + str(new_amount) + term.white + u': ') le = LineEditor(48) alternatives[(amount_of_questions, new_amount)] = le.read() if alternatives[(amount_of_questions, new_amount)] == '' or alternatives[( amount_of_questions, new_amount)] == None: break else: results[(amount_of_questions, new_amount)] = 0 new_amount = new_amount + 1 if new_amount > 0: echo(term.white(u'\r\n\r\nSave this voting question?')) answer = ynprompt() if answer == 1: questions.append(new_question) amount_of_alternatives.append(new_amount) indexcounter = db['indexcounter'] indexcounter = indexcounter + 1 index.append(str(indexcounter)) db['indexcounter'] = indexcounter db['index'] = index db['questions'] = questions db['amount_of_alternatives'] = amount_of_alternatives db['results'] = results db['amount_of_questions'] = amount_of_questions db['alternatives'] = alternatives waitprompt(term)
def add_question(): term = getterminal() db = DBProxy(databasename) questions = [] amount_of_alternatives = [] index = {} alternatives = {} results = {} index = db['index'] questions = db['questions'] alternatives = db['alternatives'] results = db['results'] amount_of_alternatives = db['amount_of_alternatives'] amount_of_questions = len(questions) echo(term.clear + term.white + '\r\nQuestion: ') le = LineEditor(65) new_question = le.read() if new_question == '': return echo( term.bold_black + '\r\n\r\nLeave a blank line when you are finished..') new_amount = 0 while True: echo(term.normal + term.white + '\r\nchoice ' + term.red + str(new_amount) + term.white + ': ') le = LineEditor(48) alternatives[(amount_of_questions, new_amount)] = le.read() if alternatives[(amount_of_questions, new_amount)] == '': break else: results[(amount_of_questions, new_amount)] = 0 new_amount = new_amount + 1 if new_amount > 0: echo(term.normal + term.white + '\r\n\r\nSave this voting question?') answer = ynprompt() if answer == 1: questions.append(new_question) amount_of_alternatives.append(new_amount) indexcounter = db['indexcounter'] indexcounter = indexcounter + 1 index.append(str(indexcounter)) db['indexcounter'] = indexcounter db['index'] = index db['questions'] = questions db['amount_of_alternatives'] = amount_of_alternatives db['results'] = results db['amount_of_questions'] = amount_of_questions db['alternatives'] = alternatives waitprompt()
def list_results(questionnumber): term = getterminal() db = DBProxy(databasename) alternatives = {} questions = [] results = [] amount_of_alternatives = db['amount_of_alternatives'] alternatives = db['alternatives'] questions = db['questions'] results = db['results'] echo(term.clear()) text = (term.white + questions[questionnumber] + u'\r\n' + term.blue + u'-' * len(questions[questionnumber]) + u'\r\n\r\n') # only display full statistics if the screen width is above 79 columns. if term.width > 79: text = text + (term.magenta + u'(alternatives)' + term.move_x(49) + u'(votes)' + term.move_x(57) + u'(percentage)\r\n') totalvotes = 0.00 for i in range(0, amount_of_alternatives[questionnumber]): totalvotes = totalvotes + results[(questionnumber, i)] for i in range(0, amount_of_alternatives[questionnumber]): if results[(questionnumber, i)] > 0: percentage = (results[(questionnumber, i)] / totalvotes) * 100 else: percentage = 0 staple = int(round(percentage / 5)) text = text + u''.join( term.move_x(0) + term.white(alternatives[ (questionnumber, i)]) + term.move_x(49) + term.cyan(str(results[(questionnumber, i)])) + u' ' + term.cyan + str(int(percentage)) + term.cyan(u'%') + term.move_x(57) + term.cyan(u'[') + term.green('#' * staple) + term.move_x(78) + term.cyan(']')) if i != amount_of_alternatives[questionnumber]: text = text + u'\r\n' else: for i in range(0, amount_of_alternatives[questionnumber]): text = text + (term.white(str(alternatives[(questionnumber, i)])) + term.cyan(u' votes: ') + term.magenta( str(results[(questionnumber, i)])) + u'\r\n') text = text.splitlines() prompt_pager(content=text, line_no=0, colors={ 'highlight': term.cyan, 'lowlight': term.green, }, width=term.width, breaker=None, end_prompt=False) echo(term.move_x(0) + term.bold_black(u'* = already voted\r\n')) waitprompt(term)
def toplist(parameter): session, term = getsession(), getterminal() handle = session.user.handle counter = 0 user_handles = list_users() username = {} feature = {} location = {} database = {} echo(term.red + u' crunching data..') for handle in user_handles: user_record = get_user(handle) if u'sysop' in user_record.groups: continue if parameter == 'calls': database[user_record.handle.encode('utf8')] = user_record.calls if parameter == 'msgs': database[user_record.handle.encode('utf8')] = user_record.get( 'msgs_sent', 0) for name in sorted(database, key=database.get, reverse=True): username[counter] = name user_record = get_user(name) location[counter] = user_record.location feature[counter] = str(database[name]) counter = counter + 1 if counter > 10: counter = 10 # we only want to display the top ten users echo(term.clear()) showansi('topten.ans') if parameter == 'calls': echo(term.yellow + term.move(7, 1) + u'[ % ] - tOP tEN cALLERS [ % ]') if parameter == 'msgs': echo(term.yellow + term.move(7, 1) + u'[ % ] - tOP tEN wRITERS [ % ]') echo(term.cyan + term.move(9, 3) + u'username' + term.move_x(27) + u'group/location' + term.move_x(67) + parameter + u'\n\n') for i in range(0, counter): echo(term.white + term.move_x(3) + username[i] + term.move_x(27) + location[i] + term.move_x(67) + feature[i] + u'\r\n') waitprompt(term)
def list_results(questionnumber): term = getterminal() db = DBProxy(databasename) alternatives = {} questions = [] results = [] amount_of_alternatives = db['amount_of_alternatives'] alternatives = db['alternatives'] questions = db['questions'] results = db['results'] echo(term.clear()) text = (term.white + questions[questionnumber] + u'\r\n' + term.blue + u'-' * len(questions[questionnumber]) + u'\r\n\r\n') # only display full statistics if the screen width is above 79 columns. if term.width > 79: text = text + (term.magenta + u'(alternatives)' + term.move_x(49) + u'(votes)' + term.move_x(57) + u'(percentage)\r\n') totalvotes = 0.00 for i in range(0, amount_of_alternatives[questionnumber]): totalvotes = totalvotes + results[(questionnumber, i)] for i in range(0, amount_of_alternatives[questionnumber]): if results[(questionnumber, i)] > 0: percentage = (results[(questionnumber, i)] / totalvotes) * 100 else: percentage = 0 staple = int(round(percentage / 5)) text = text + u''.join(term.move_x(0) + term.white(alternatives[(questionnumber, i)]) + term.move_x(49) + term.cyan(str(results[(questionnumber, i)])) + u' ' + term.cyan + str(int(percentage)) + term.cyan(u'%') + term.move_x(57) + term.cyan(u'[') + term.green('#' * staple) + term.move_x(78) + term.cyan(']')) if i != amount_of_alternatives[questionnumber]: text = text + u'\r\n' else: for i in range(0, amount_of_alternatives[questionnumber]): text = text + (term.white(str(alternatives[(questionnumber, i)])) + term.cyan(u' votes: ') + term.magenta(str(results[(questionnumber, i)])) + u'\r\n') text = text.splitlines() prompt_pager(content=text, line_no=0, colors={'highlight': term.cyan, 'lowlight': term.green, }, width=term.width, breaker=None, end_prompt=False) echo(term.move_x(0) + term.bold_black(u'* = already voted\r\n')) waitprompt(term)
def query_question(): term = getterminal() session = getsession() db = DBProxy(databasename) questions = [] index = [] uservotingdata = [] questions = db['questions'] index = db['index'] counter = 0 # create database for user if the user hasn't made any votes if not session.user.handle in db: db[session.user.handle] = {} uservotingdata = db[session.user.handle] echo(term.clear() + term.blue + '>>' + term.white + 'questions availible\r\n' + term.blue + '---------------------\r\n\r\n' + term.white) for i in range(0, len(questions)): if (index[i], 0) in uservotingdata: # prints out a star to show that the user already voted on this # question echo(term.green + '*') echo(term.magenta + '(' + term.cyan + str(i) + term.magenta + ') ' + term.white + questions[i] + '\r\n') # if the list of questions is longer than the screen height, display a # press enter prompt counter = counter + 1 if counter > term.height - 7: counter = 0 waitprompt() echo(term.move_x(0) + term.clear_eol + term.move_up) echo(term.bold_black + '* = already voted\r\n\r\n' + term.normal) while True: echo( term.magenta + '\rselect one of the questions above or press enter to return: ') le = LineEditor(30) le.colors['highlight'] = term.cyan inp = le.read() if inp.isnumeric() and int(inp) < len(questions): return int(inp) else: # 999 in this case means that no valid option was chosen.. break # loop. return 999
def list_results(questionnumber): term = getterminal() db = DBProxy(databasename) alternatives = {} questions = [] results = [] amount_of_alternatives = [] amount_of_alternatives = db['amount_of_alternatives'] alternatives = db['alternatives'] alternatives = db['alternatives'] questions = db['questions'] results = db['results'] index = db['index'] counter = 0 echo(term.clear()) echo(term.white + questions[questionnumber] + term.move_x(term.width - 10) + ' index: ' + str(index[questionnumber]) + '\r\n' + term.blue + '-' * len(questions[questionnumber]) + '\r\n\r\n') echo(term.magenta + '(alternatives)' + term.move_x(49) + '(votes)' + term.move_x(57) + '(percentage)\r\n') totalvotes = 0.00 for i in range(0, amount_of_alternatives[questionnumber]): totalvotes = totalvotes + results[(questionnumber, i)] for i in range(0, amount_of_alternatives[questionnumber]): if results[(questionnumber, i)] > 0: percentage = (results[(questionnumber, i)] / totalvotes) * 100 else: percentage = 0 staple = int(round(percentage / 5)) echo(term.cyan + term.move_x(49) + str(results[(questionnumber, i)]) + ' ' + str(int(percentage)) + '%' + term.move_x(57) + '[ ]' + term.move_x(58) + term.green + '#' * staple + '\r') echo(term.white + alternatives[(questionnumber, i)] + '\r\n') counter = counter + 1 # if the list of questions is longer than the screen height, display a press enter prompt if counter > term.height - 7: counter = 0 waitprompt() echo(term.move_x(0) + term.clear_eol + term.move_up) waitprompt()
def list_results(questionnumber): term = getterminal() db = DBProxy(databasename) alternatives = {} questions = [] results = [] amount_of_alternatives = [] amount_of_alternatives = db['amount_of_alternatives'] alternatives = db['alternatives'] alternatives = db['alternatives'] questions = db['questions'] results = db['results'] index = db['index'] counter = 0 echo(term.clear()) echo(term.white + questions[questionnumber] + term.move_x(max(0, term.width - 10)) + ' index: ' + str(index[questionnumber]) + '\r\n' + term.blue + '-' * len(questions[questionnumber]) + '\r\n\r\n') echo(term.magenta + '(alternatives)' + term.move_x(49) + '(votes)' + term.move_x(57) + '(percentage)\r\n') totalvotes = 0.00 for i in range(0, amount_of_alternatives[questionnumber]): totalvotes = totalvotes + results[(questionnumber, i)] for i in range(0, amount_of_alternatives[questionnumber]): if results[(questionnumber, i)] > 0: percentage = (results[(questionnumber, i)] / totalvotes) * 100 else: percentage = 0 staple = int(round(percentage / 5)) echo(term.cyan + term.move_x(49) + str(results[(questionnumber, i)]) + ' ' + str(int(percentage)) + '%' + term.move_x(57) + '[ ]' + term.move_x(58) + term.green + '#' * staple + '\r') echo(term.white + alternatives[(questionnumber, i)] + '\r\n') # if the list of questions is longer than the screen height, display a # press enter prompt counter = counter + 1 if counter > term.height - 7: counter = 0 waitprompt() echo(term.move_x(0) + term.clear_eol + term.move_up) waitprompt()
def query_question(): term = getterminal() session = getsession() db = DBProxy(databasename) questions = [] index = [] uservotingdata = [] questions = db['questions'] index = db['index'] counter = 0 if not session.user.handle in db: # create database for user if the user hasn't made any votes db[session.user.handle] = {} uservotingdata = db[session.user.handle] echo(term.clear() + term.blue + '>>' + term.white + 'questions availible\r\n' + term.blue + '---------------------\r\n\r\n' + term.white) for i in range(0, len(questions)): if (index[i], 0) in uservotingdata: echo( term.green + '*' ) # prints out a star to show that the user already voted on this question echo(term.magenta + '(' + term.cyan + str(i) + term.magenta + ') ' + term.white + questions[i] + '\r\n') counter = counter + 1 # if the list of questions is longer than the screen height, display a press enter prompt if counter > term.height - 7: counter = 0 waitprompt() echo(term.move_x(0) + term.clear_eol + term.move_up) echo(term.bold_black + '* = already voted\r\n\r\n' + term.normal) while 1: echo(term.magenta + '\rselect one of the questions above or press enter to return: ') le = LineEditor(30) le.colors['highlight'] = term.cyan inp = le.read() if inp.isnumeric() and int(inp) < len(questions): return int(inp) else: return 999 # 999 in this case means that no valid option was chosen.. break loop.
def main(quick=False): """ Main procedure. """ session, term = getsession(), getterminal() session.activity = 'checking for new messages' # set syncterm font, if any if term.kind.startswith('ansi'): echo(syncterm_setfont(syncterm_font)) colors = dict(highlight=lambda txt: txt, lowlight=lambda txt: txt, backlight=lambda txt: txt, text=lambda txt: txt) if not colored_menu_items else dict( highlight=getattr(term, color_highlight), lowlight=getattr(term, color_lowlight), backlight=getattr(term, color_backlight), text=getattr(term, color_text)) yloc = top_margin = 0 subscription = session.user.get('msg_subscription', []) dirty = 2 while True: if dirty == 2: # display header art, yloc = display_banner(art_file, encoding=art_encoding, center=True) xloc = max(0, (term.width // 2) - 40) echo(u'\r\n') top_margin = yloc = (yloc + 1) elif dirty: echo(term.move(top_margin, 0) + term.normal + term.clear_eos) echo(term.move(top_margin, xloc)) if dirty: if not subscription: # prompt the user for a tag subscription, and loop # back again when completed to re-draw and show new messages. subscription = session.user['msg_subscription'] = ( prompt_subscription(session=session, term=term, yloc=top_margin, subscription=subscription, colors=colors)) continue messages, messages_bytags = get_messages_by_subscription( session, subscription) # When quick login ('y') selected in top.py, return immediately # when no new messages are matched any longer. if quick and not messages['new']: echo(term.move_x(xloc) + u'\r\nNo new messages.\r\n') return waitprompt(term) txt = describe_message_area(term=term, subscription=subscription, messages_bytags=messages_bytags, colors=colors) yloc = top_margin + show_description( term=term, description=txt, color=None, subsequent_indent=' ' * len('message area: ')) echo( render_menu_entries(term=term, top_margin=yloc, menu_items=get_menu(messages), colors=colors, max_cols=2)) echo(display_prompt(term=term, colors=colors)) echo(colors['backlight'](u' \b')) dirty = False event, data = session.read_events(('refresh', 'newmsg', 'input')) if event == 'refresh': # screen resized, redraw. dirty = 2 continue elif event == 'newmsg': # When a new message is sent, 'newmsg' event is broadcasted. session.flush_event('newmsg') nxt_msgs, nxt_bytags = get_messages_by_subscription( session, subscription) if nxt_msgs['new'] - messages['new']: # beep and re-display when a new message has arrived. echo(u'\b') messages, messages_bytags = nxt_msgs, nxt_bytags dirty = True continue elif event == 'input': # on input, block until carriage return session.buffer_input(data, pushback=True) given_inp = LineEditor(1, colors={ 'highlight': colors['backlight'] }).read() if given_inp is None: # escape/cancel continue inp = given_inp.strip() if inp.lower() in (u'n', 'a', 'v'): # read new/all/private messages message_indices = sorted( list({ 'n': messages['new'], 'a': messages['all'], 'v': messages['private'], }[inp.lower()])) if message_indices: dirty = 2 read_messages(session=session, term=term, message_indices=message_indices, colors=colors) elif inp.lower() == u'm' and messages['new']: # mark all messages as read dirty = 1 do_mark_as_read(session, messages['new']) elif inp.lower() in (u'p', u'w'): # write new public/private message dirty = 2 public = bool(inp.lower() == u'p') msg = Msg() if (not prompt_recipient( term=term, msg=msg, colors=colors, public=public) or not prompt_subject(term=term, msg=msg, colors=colors) or not prompt_body(term=term, msg=msg, colors=colors) or not prompt_tags(session=session, term=term, msg=msg, colors=colors, public=public)): continue do_send_message(session=session, term=term, msg=msg, colors=colors) elif inp.lower() == u'c': # prompt for new tag subscription (at next loop) subscription = [] dirty = 1 elif inp.lower() == u'?': # help echo(term.move(top_margin, 0) + term.clear_eos) do_describe_message_system(term, colors) waitprompt(term) dirty = 2 elif inp.lower() == u'q': return if given_inp: # clear out line editor prompt echo(colors['backlight'](u'\b \b'))
def main(): session = getsession() session.activity = u'hanging out in voting script' term = getterminal() echo(syncterm_setfont('topaz')) db = DBProxy(databasename) if 'questions' not in db: generate_database() while True: # clears the screen and displays the vote art header echo(term.clear()) for line in showart( os.path.join(os.path.dirname(__file__), 'art', 'vote.ans'), 'cp437'): echo(term.cyan + term.move_x(max(0, (term.width / 2) - 40)) + line) if 'sysop' in session.user.groups: spacing = 1 else: spacing = 7 echo(' ') echo(term.magenta(u'\n (') + term.cyan(u'r') + term.magenta(u')') + term.white(u'esults') + ' ' * spacing + term.magenta(u'(') + term.cyan(u'v') + term.magenta(u')') + term.white(u'ote on a question') + u' ' * spacing + term.magenta(u'(') + term.cyan(u'a') + term.magenta(u')') + term.white(u'dd a new question') + u' ' * spacing) if 'sysop' in session.user.groups: echo(term.magenta(u'(') + term.cyan(u'd') + term.magenta(u')') + term.white(u'elete a question') + u' ' * spacing) echo(term.magenta(u'(') + term.cyan(u'q') + term.magenta(u')') + term.white(u'uit') + term.magenta(u'\r\n\r\nx/84 voting booth command: ')) le = LineEditor(10) le.colors['highlight'] = term.cyan inp = le.read() # makes the input indifferent to wheter you used lower case when typing # in a command or not.. inp = (inp or u'').lower() if 'sysop' in session.user.groups and inp == u'd': while True: questionnumber = query_question() if questionnumber == -1: break delete_question(questionnumber) elif inp == u'r': while True: questionnumber = query_question() if questionnumber == -1: break list_results(questionnumber) elif inp == u'v': while True: questionnumber = query_question() if questionnumber == -1: break vote(questionnumber) elif inp == u'a': add_question() elif inp == u'q': return else: # if no valid key is pressed then do some ami/x esthetics. echo(term.red(u'\r\nNo such command. Try again.\r\n')) waitprompt(term)
def main(): session = getsession() session.activity = u'hanging out in voting script' term = getterminal() echo(term.clear()) db = DBProxy(databasename) if not 'questions' in db: generate_database() while True: echo( term.clear()) # clears the screen and displays the vote art header for line in showart( os.path.join(os.path.dirname(__file__), 'art', 'vote.ans'), 'topaz'): echo(term.cyan + term.move_x((term.width / 2) - 40) + line) if 'sysop' in session.user.groups: spacing = 1 else: spacing = 7 echo(' ') echo(term.magenta + '\n (' + term.cyan + 'r' + term.magenta + ')' + term.white + 'esults' + ' ' * spacing) echo(term.magenta + '(' + term.cyan + 'v' + term.magenta + ')' + term.white + 'ote on a question' + ' ' * spacing) echo(term.magenta + '(' + term.cyan + 'a' + term.magenta + ')' + term.white + 'dd a new question' + ' ' * spacing) if 'sysop' in session.user.groups: echo(term.magenta + '(' + term.cyan + 'd' + term.magenta + ')' + term.white + 'elete a question' + ' ' * spacing) echo(term.magenta + '(' + term.cyan + 'q' + term.magenta + ')' + term.white + 'uit') echo(term.magenta + '\r\n\r\n\r\nx/84 voting booth command: ') le = LineEditor(30) le.colors['highlight'] = term.cyan inp = le.read() inp = inp.lower( ) # makes the input indifferent to wheter you used lower case when typing in a command or not.. if 'sysop' in session.user.groups and inp == 'd': while 1: questionnumber = query_question() if questionnumber == 999: break delete_question(questionnumber) elif inp == 'r': while 1: questionnumber = query_question() if questionnumber == 999: break list_results(questionnumber) elif inp == 'v': while 1: questionnumber = query_question() if questionnumber == 999: break vote(questionnumber) elif inp == 'a': add_question() elif inp == 'q': return else: echo(term.red + '\r\nNo such command. Try again.\r\n' ) # if no valid key is pressed then do some ami/x esthetics. waitprompt()
def main(): session, term = getsession(), getterminal() session.activity = 'reading news from ycombinator.com' echo(term.clear + term.yellow + 'firing up hACKER nEWS! *') feed = feedparser.parse('https://news.ycombinator.com/rss') article = [] link = [] dirty = True offset = 0 wrappedarticle = [] # if the article header needs wrapping.. # the amount of rows that a description is estimated to use. amount = term.height / 5 echo(u'*') # buffers the articles titels, summarys and links. for post in feed.entries: article.append(post.title) link.append(post.link) while True: if dirty == True: echo(term.normal) for i in range(1, term.height - 1): echo(term.move(i, 0) + term.clear_eol) echo(term.move(2, 0)) for i in range(0, amount): if len(article) > i + offset: echo(term.magenta + str(i + offset) + '. ') if len(article[i + offset]) > 79 - 4: wrappedarticle = wrap( article[ i + offset], 79 - 4, break_long_words=True) for i2 in range(0, len(wrappedarticle)): echo(term.cyan + wrappedarticle[i2] + '\r\n') else: echo(term.cyan + article[i + offset] + '\r\n') echo( term.white + link[ i + offset] + '\r\n\r\n' + term.normal) echo(term.normal + term.move(term.height, 0) + term.on_blue + term.clear_eol + '(' + term.bold('up/down') + term.on_blue + ') next/previous (' + term.bold('q/escape') + term.on_blue + ') quits (' + term.bold('enter') + term.on_blue + ') select' + term.move(term.height, term.width - 20) + term.cyan + term.move(0, 0) + term.clear_eol + term.cyan + '** hacker news on ycombinator.com **' + term.normal) keypressed = getch() dirty = True if keypressed == 'q' or keypressed == 'Q' or keypressed == term.KEY_ESCAPE: break elif keypressed == term.KEY_DOWN: if (offset + amount) < len(article): offset = offset + amount else: # checks wheter the article has fewer lines than the screen or # not.. if len(article) < amount: offset = 0 elif keypressed == term.KEY_UP: if offset > amount: offset = offset - amount else: offset = 0 elif keypressed == term.KEY_ENTER: echo( term.move( term.height, 0) + term.clear_eol + term.white + 'choose your article no#: ') le = LineEditor(10) le.colors['highlight'] = term.cyan inp = le.read() if inp.isnumeric() and int(inp) < len(article): echo( term.clear + term.yellow + 'fetching the latest news just for you..' + term.normal) choosenurl = link[int(inp)] h = html2text.HTML2Text() h.ignore_links = True h.ignore_images = True h.escape_all = True h.body_width = 79 req = urllib2.Request( choosenurl, headers={ 'User-Agent': 'Mozilla'}) # identify as mozilla try: text = urllib2.urlopen(req, timeout=10).read() except socket.timeout as e: echo( term.clear() + term.yellow + 'request timed out.. try again.') waitprompt() except urllib2.URLError as e: echo( term.clear() + term.yellow + 'faulty http link.. try again.') waitprompt() else: text = unicode(h.handle(text.decode(errors='ignore'))) articlereader(text.split('\n')) else: dirty = False
def main(quick=False): """ Main procedure. """ session, term = getsession(), getterminal() session.activity = 'checking for new messages' # set syncterm font, if any if term.kind.startswith('ansi'): echo(syncterm_setfont(syncterm_font)) colors = dict( highlight=lambda txt: txt, lowlight=lambda txt: txt, backlight=lambda txt: txt, text=lambda txt: txt ) if not colored_menu_items else dict( highlight=getattr(term, color_highlight), lowlight=getattr(term, color_lowlight), backlight=getattr(term, color_backlight), text=getattr(term, color_text)) yloc = top_margin = 0 subscription = session.user.get('msg_subscription', []) dirty = 2 while True: if dirty == 2: # display header art, yloc = display_banner(art_file, encoding=art_encoding, center=True) xloc = max(0, (term.width // 2) - 40) echo(u'\r\n') top_margin = yloc = (yloc + 1) elif dirty: echo(term.move(top_margin, 0) + term.normal + term.clear_eos) echo(term.move(top_margin, xloc)) if dirty: if not subscription: # prompt the user for a tag subscription, and loop # back again when completed to re-draw and show new messages. subscription = session.user['msg_subscription'] = ( prompt_subscription( session=session, term=term, yloc=top_margin, subscription=subscription, colors=colors)) continue messages, messages_bytags = get_messages_by_subscription( session, subscription) # When quick login ('y') selected in top.py, return immediately # when no new messages are matched any longer. if quick and not messages['new']: echo(term.move_x(xloc) + u'\r\nNo new messages.\r\n') return waitprompt(term) txt = describe_message_area( term=term, subscription=subscription, messages_bytags=messages_bytags, colors=colors) yloc = top_margin + show_description( term=term, description=txt, color=None, subsequent_indent=' ' * len('message area: ')) echo(render_menu_entries( term=term, top_margin=yloc, menu_items=get_menu(messages), colors=colors, max_cols=2)) echo(display_prompt(term=term, colors=colors)) echo(colors['backlight'](u' \b')) dirty = False event, data = session.read_events(('refresh', 'newmsg', 'input')) if event == 'refresh': # screen resized, redraw. dirty = 2 continue elif event == 'newmsg': # When a new message is sent, 'newmsg' event is broadcasted. session.flush_event('newmsg') nxt_msgs, nxt_bytags = get_messages_by_subscription( session, subscription) if nxt_msgs['new'] - messages['new']: # beep and re-display when a new message has arrived. echo(u'\b') messages, messages_bytags = nxt_msgs, nxt_bytags dirty = True continue elif event == 'input': # on input, block until carriage return session.buffer_input(data, pushback=True) given_inp = LineEditor( 1, colors={'highlight': colors['backlight']} ).read() if given_inp is None: # escape/cancel continue inp = given_inp.strip() if inp.lower() in (u'n', 'a', 'v'): # read new/all/private messages message_indices = sorted(list( {'n': messages['new'], 'a': messages['all'], 'v': messages['private'], }[inp.lower()])) if message_indices: dirty = 2 read_messages(session=session, term=term, message_indices=message_indices, colors=colors) elif inp.lower() == u'm' and messages['new']: # mark all messages as read dirty = 1 do_mark_as_read(session, messages['new']) elif inp.lower() in (u'p', u'w'): # write new public/private message dirty = 2 public = bool(inp.lower() == u'p') msg = Msg() if ( not prompt_recipient( term=term, msg=msg, colors=colors, public=public ) or not prompt_subject( term=term, msg=msg, colors=colors ) or not prompt_body( term=term, msg=msg, colors=colors ) or not prompt_tags( session=session, term=term, msg=msg, colors=colors, public=public )): continue do_send_message(session=session, term=term, msg=msg, colors=colors) elif inp.lower() == u'c': # prompt for new tag subscription (at next loop) subscription = [] dirty = 1 elif inp.lower() == u'?': # help echo(term.move(top_margin, 0) + term.clear_eos) do_describe_message_system(term, colors) waitprompt(term) dirty = 1 elif inp.lower() == u'q': return if given_inp: # clear out line editor prompt echo(colors['backlight'](u'\b \b'))