#!/usr/bin/python

import logging, gc
import urwid.curses_display
from sys import stdin
from urwid import *
from time import sleep

ITER = 1000000
LOG_FILE="memory.log"

gc.set_threshold(1, 1, 1)

ui = None

logging.basicConfig(
  level=logging.DEBUG, filename=LOG_FILE,
  format="%(asctime)s %(levelname)s %(message)s"
)
    
Palette = [
  ("body", "light gray", "dark blue"),
  ("header", "dark blue", "light gray"),
  ("footer", "yellow", "dark blue")
]

def Test(idx):
  logging.debug("GC TRACKING %d OBJECTS" % len(gc.get_objects()))
  caption = AttrWrap(Text(("header", "Test Window %d" % idx), align="center"), "header")
  hint = AttrWrap(Text(("footer", "Test Window Footer"), align="center"), "footer")
  content = AttrWrap(Filler(Text("This is a test", align="center")), "body")
  content = Frame(content, header=caption, footer=hint)
  content = Padding(content, "center", 60)
  content = Filler(content, "middle", 25)
  area = ui.get_cols_rows()
  ui.draw_screen(area, content.render(area, True))
  sleep(0.25)

def Test2():
  logging.debug("GC TRACKING %d OBJECTS" % len(gc.get_objects()))
  l = ["THIS IS A TEST STRING %10d" % c for c in range(1, ITER)]
  for c in range(1, ITER): l.pop()

def Runner():
  for c in range(1, ITER): Test(c)

ui = urwid.curses_display.Screen()
ui.register_palette(Palette)
ui.run_wrapper(Runner)
