<commit_before>import calendar
import curses
import curses.ascii
from curses import textpad
import datetime
import logging
import os
import itertools

logger = logging.getLogger('sailor')

CTRL_A = 1
CTRL_E = ord('e') - ord('a') + 1
CTRL_J = ord('j') - ord('a') + 1
CTRL_K = ord('k') - ord('a') + 1
CTRL_N = ord('n') - ord('a') + 1
CTRL_P = ord('p') - ord('a') + 1

MAC_BACKSPACE = 127   # Don't feel like finding out why
SHIFT_TAB = 353
CR = 13  # Or Ctrl-M, so don't use that

black = curses.COLOR_BLACK
red = curses.COLOR_RED
green = curses.COLOR_GREEN
white = curses.COLOR_WHITE
blue = curses.COLOR_BLUE
cyan = curses.COLOR_CYAN
magenta = curses.COLOR_MAGENTA
yellow = curses.COLOR_YELLOW

# FIXME: Crash when running off the edges

def reduce_esc_delay():
  try:
    os.environ['ESCDELAY']
  except KeyError:
    os.environ['ESCDELAY'] = '25'


def is_enter(ev):
  return ev.key in [curses.KEY_ENTER, CR]


def ident(x):
  return x


def get_value(x):
  return x.value if isinstance(x, Option) else x

#----------------------------------------------------------------------
#  VIEW classes

class View(object):
  def size(self, rect):
    return (0, 0)

  def display(self, rect):
    self.rect = rect
    if rect.w > 0 and rect.h > 0:
      self.disp(rect)

  def disp(self, rect):
    raise RuntimeError('Not implemented: disp()')


class Display(View):
  def __init__(self, text, min_width=0, fg=white, bg=black, attr=0):
    self.lines = str(text).split('\n')
    self.fg = fg
    self.bg = bg
    self.min_width = min_width
    self.attr = attr

  def size(self, rect):
    return max(self.min_width, max(len(l) for l in self.lines)), len(self.lines)

  def disp(self, rect):
    col = rect.get_color(self.fg, self.bg)
    print_width = max(0, rect.w)
    lines = self.lines[:rect.h]
    if print_width > 0 and lines:
      for i, line in enumerate(lines):
        padding = ' ' * min(print_width, self.min_width - len(line))
        rect.screen.addstr(rect.y + i, rect.x, line[:print_width] + padding, curses.color_pair(col) | self.attr)


class Positioned(View):
  def __init__(self, inner, x=-1, y=-1):
    self.inner = inner
    self.x = x
    self.y = y

  def size(self, rect):<commit_msg>Don't crash if we can't draw some thing<commit_after>import calendar
import curses
import curses.ascii
from curses import textpad
import datetime
import logging
import os
import itertools

logger = logging.getLogger('sailor')

CTRL_A = 1
CTRL_E = ord('e') - ord('a') + 1
CTRL_J = ord('j') - ord('a') + 1
CTRL_K = ord('k') - ord('a') + 1
CTRL_N = ord('n') - ord('a') + 1
CTRL_P = ord('p') - ord('a') + 1

MAC_BACKSPACE = 127   # Don't feel like finding out why
SHIFT_TAB = 353
CR = 13  # Or Ctrl-M, so don't use that

black = curses.COLOR_BLACK
red = curses.COLOR_RED
green = curses.COLOR_GREEN
white = curses.COLOR_WHITE
blue = curses.COLOR_BLUE
cyan = curses.COLOR_CYAN
magenta = curses.COLOR_MAGENTA
yellow = curses.COLOR_YELLOW

# FIXME: Crash when running off the edges

def reduce_esc_delay():
  try:
    os.environ['ESCDELAY']
  except KeyError:
    os.environ['ESCDELAY'] = '25'


def is_enter(ev):
  return ev.key in [curses.KEY_ENTER, CR]


def ident(x):
  return x


def get_value(x):
  return x.value if isinstance(x, Option) else x

#----------------------------------------------------------------------
#  VIEW classes

class View(object):
  def size(self, rect):
    return (0, 0)

  def display(self, rect):
    self.rect = rect
    if rect.w > 0 and rect.h > 0:
      self.disp(rect)

  def disp(self, rect):
    raise RuntimeError('Not implemented: disp()')


class Display(View):
  def __init__(self, text, min_width=0, fg=white, bg=black, attr=0):
    self.lines = str(text).split('\n')
    self.fg = fg
    self.bg = bg
    self.min_width = min_width
    self.attr = attr

  def size(self, rect):
    return max(self.min_width, max(len(l) for l in self.lines)), len(self.lines)

  def disp(self, rect):
    col = rect.get_color(self.fg, self.bg)
    print_width = max(0, rect.w)
    lines = self.lines[:rect.h]
    if print_width > 0 and lines:
      for i, line in enumerate(lines):
        padding = ' ' * min(print_width, self.min_width - len(line))
        try:
          rect.screen.addstr(rect.y + i, rect.x, line[:print_width] + padding, curses.color_pair(col) | self.attr)
        except curses.error, e:
          logger.warn(str(e))


class Positioned(View):
  def __init__(self, inner, x=-1, y=-1):
    self.inner = inner
    self.x = x
    self.y = y

  def size(self, rect):
{'fraction_non_alphanumeric': 0.16507866164413915, 'fraction_numerical': 0.011522269000664746, 'mean_word_length': 2.915004336513443, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5337990', 'n_tokens_mistral': 1694, 'n_tokens_neox': 1629, 'n_words': 447}