#! /usr/bin/env python # -*- coding: UTF-8 -*- """ PyBrowsing, creato da Frafra (francesco.it@gmail.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. """ __module_name__ = "PyBrowsing" __module_version__ = "0.1" __module_description__ = "Per cercare in grossi db in tempo reale" # Prova ad importare le librerie per Windows, altrimenti si adatta per Unix try: from msvcrt import getch except: from termios import tcgetattr, TCSADRAIN, tcsetattr from tty import setraw from sys import stdin def getch(): fd = stdin.fileno() old_settings = tcgetattr(fd) setraw(stdin.fileno()) ch = stdin.read(1) tcsetattr(fd, TCSADRAIN, old_settings) return ch # Inizializza alcune variabili opts = [] res= "" query = "" old_query_len = 0 try: siti = open("siti.txt", "r") # File contenente i siti except: print "File-dizionario siti.txt non trovato nella directory corrente." # Legge il file while 1: line = siti.readline() if not line: break opts.append(line[0:-1]) siti.close() opts.sort() # Riordina while 1: print "\rInserisci dei caratteri:", new_query = getch() # Prende il carattere if new_query == "": # Quando si preme del... if len(query) == 0: # ...se non ci sono più caratteri... raise SystemExit(0) # ...esce else: # ...altrimenti... query = query[0:-1] # ... elimina l'ultimo carattere else: query = query + new_query # Ricompone la stringa cleaner = len(res) + old_query_len + 5 print " "*cleaner+"\b"*(cleaner+1), # Pulisce la riga dal vecchio risultato if query != "": print query + " -> ", res = ", ".join(x for x in opts if x.startswith(query)) if not res: # Se non ci sono risultati... res = "[No results]" # ... setta su [No results] print res, # Visualizza il risultato old_query_len = len(query) # Calcola la lunghezza dell'input