Add first bits of color

This commit is contained in:
Sebastian Pipping 2011-10-10 04:03:57 +02:00
parent 6b726137b8
commit a3521100b6
1 changed files with 48 additions and 12 deletions

View File

@ -8,6 +8,10 @@ from __future__ import print_function
import freitagslib.network as net
from freitagslib.commands import BuyCommand, DepositCommand
import colorama
from colorama import Fore, Style
import sys
from decimal import Decimal
import os
@ -15,6 +19,17 @@ import time
import urllib2
COLOR_WARN = Fore.YELLOW
COLOR_ERROR = Fore.RED
COLOR_DEPOSIT = Fore.GREEN + Style.BRIGHT
COLOR_WITHDRAW = Fore.RED + Style.BRIGHT
COLOR_SOME = Fore.WHITE + Style.BRIGHT
COLOR_MUCH = Fore.YELLOW + Style.BRIGHT
COLOR_RESET = Style.RESET_ALL
def clear():
os.system('clear')
@ -49,7 +64,7 @@ def warn_balance():
def error_page(message):
clear()
print(message)
print(COLOR_ERROR + message + COLOR_RESET)
print()
delay('Weiter', 3)
@ -66,32 +81,52 @@ class Status:
self.transfers = None
def dump(self):
def sign(amount, plus='+'):
return '-' if amount < 0 else plus
def color(amount):
return COLOR_WITHDRAW if amount < 0 else COLOR_DEPOSIT
def show_total(balance, plus=' '):
print('%3s %-40s %s%c %6.2f Euro%s' \
% ('', '', color(balance), sign(balance, plus),
abs(balance), COLOR_RESET))
def show_item(position, command):
diff = command.difference()
label = command.label()
print('%2d) %-40s %s%c %6.2f Euro%s' \
% (position, label, color(diff), sign(diff),
abs(diff), COLOR_RESET))
def show_bar():
print('%3s %-40s %13s' % ('', '', '============='))
if self.logged_in():
print('Eingeloggt als: %s' % (self.login_name))
print('Eingeloggt als: %s%s%s' % (COLOR_SOME, self.login_name, COLOR_RESET))
print()
if self.transfers:
print('Geplante Änderungen:')
initial_command, initial_balance = self.transfers[0]
print(('%3s %-40s %c %6.2f Euro') % ('', '', ' ' if initial_balance > 0 else '-', abs(initial_balance)))
print('Geplante Änderungen:')
show_total(initial_balance)
for i, (command, balance_backup) in enumerate(self.transfers):
difference = command.difference()
label = command.label()
print(('%2d) %-40s %c %6.2f Euro') % (i + 1, label, '+' if difference > 0 else '-', abs(difference)))
print(('%3s %-40s %8s') % ('', '', '============='))
print(('%3s %-40s %c %6.2f Euro') % ('', '', ' ' if self.balance > 0 else '-', abs(self.balance)))
show_item(i + 1, command)
show_bar()
show_total(self.balance)
if self.balance < 0:
warn_balance()
print()
print('Committen nicht vergessen.')
print(COLOR_SOME + 'Committen nicht vergessen.' + COLOR_RESET)
else:
print('Kontostand beträgt: %.2f Euro' % (self.balance))
print('Kontostand beträgt: %s%.2f Euro%s' % (COLOR_MUCH, self.balance, COLOR_RESET))
if self.balance < 0:
print()
warn_balance()
else:
print('Bitte einloggen.')
print(COLOR_MUCH + 'Bitte einloggen.' + COLOR_RESET)
print()
print('Scanne dazu deine ID-Karte mit dem Barcode-Leser.')
print()
@ -195,6 +230,7 @@ def handle(line, status):
def main():
colorama.init()
status = Status()
while True: