import bottombar, os, time

print('\033[{};1H'.format(os.get_terminal_size().lines))
print('====================')
print('BOTTOMBAR UNIT TESTS')
print('--------------------')
print('1. basic terminal tests')
input('   a. check that this question shows at the very last line')
input('   b. check that the the last word shows in \033[0;33myellow\033[0m')
input('   c. check that this box [ ] contains the blinking cursor\033[31D')
print('2. basic bottombar tests')
with bottombar.BottomBar('foo...') as bb:
  input('   a. check that the bottom line shows the text "foo..."')
  bb('bar!')
  input('   b. check that the bottom line shows "bar!" (and nothing more)')
  input('   c. check that this {}long sentence wraps without overwriting "bar!"'.format('very ' * 40))
  bb('abc', 123)
  input('   d. check that the bottom line shows "abc 123"')
input('   e. check that the bar disappeared')
print('3. line wrapping tests')
with bottombar.BottomBar('this is a very long bar; ' * 99) as bb:
  input('   a. check that the line below is truncated without wrapping')
  print('   b. check that \033[0;33m[this entire ', end='', flush=True)
  bb('white bar')
  input('box]\033[0m is yellow but the bar is white')
  print('   c. check that \033[0;33m[this entire ', end='', flush=True)
  bb('\033[0;34mblue bar')
  input('box]\033[0m is yellow but the bar is blue')
  print('   d. check that \033[0;33m[this entire ', end='', flush=True)
input('box]\033[0m is yellow and the bar disappeared')
print('4. resize tests')
with bottombar.BottomBar('left', 'right', format=lambda a, b, width: a.ljust(width-len(b),'-') + b) as bb:
  input('   a. check that the bottom line shows "left--(...)--right" over the entire length')
  input('   b. change the window width; check that "left" and "right" move along')
  input('   c. change the window height; check that the bar remains at the bottom')
  input('   e. check that the blinking cursor stays in this box [ ] upon resize\033[14D')
print('5. update tests')
def format(text: str, width: int) -> str:
  t = time.time()
  time.sleep(.123)
  return '{0.tm_hour:02d}:{0.tm_min:02d}:{1:06.3f} {2}'.format(time.localtime(t), t%60, text)
with bottombar.BottomBar('updating approximately every second', format=format, interval=-1) as bb:
  input('   a. check that the time in the bar updates approximately every second')
  input('   b. check that the milliseconds change with every update')
with bottombar.BottomBar('updating precisely every second', format=format, interval=+1) as bb:
  input('   c. check that the milliseconds remains fixed with every update')
  bb('the end.')
  input('   d. check that the text after the time changed to "the end."')
print('--------------------')
