<commit_before>import pygame
from itertools import chain
import pygame.locals as pl

BUTTON_SCROLL_WHEEL_UP = 4
BUTTON_SCROLL_WHEEL_DOWN = 5

SCROLLBAR_THICKNESS = 20
#TEXT_SCROLLBAR_SPACE = 10
SCROLL_SPEED = 20


class ScrolledTextPanel(pygame.Surface):
  def __init__(self, display, x, y, width, height, background_color=(255, 255, 255)):
    pygame.Surface.__init__(self,(width, height))

    self.focus = False
    self.label = ''

    self.display = display
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.font = self.display.getFont()
    self.background_color = background_color
    self.text_lines = []
    self.text_surfaces = []
    self.text_width = self.width
    self.text_height = self.height
    self.text_width = self.getTextWidth()
    self.text_height = self.getTextHeight()
    self.text_surface = pygame.Surface((self.text_width, self.text_height))

    self.line_cursor = 0
    self.speak_lines = True

    self.rect = self.get_rect()
    self.ratio = 1.0
    self.track = pygame.Rect(self.rect.right - SCROLLBAR_THICKNESS, self.rect.top, SCROLLBAR_THICKNESS, self.rect.height)
    self.knob = pygame.Rect(self.track)
    self.knob.height = self.track.height * self.ratio
    self.scrolling = False
    self.mouse_in_me = False


  @staticmethod
  def truncline(text, font, maxwidth):
    real = len(text)
    stext = text
    l = font.size(text)[0]
    cut = 0
    a = 0
    done = True
    # old = None
    while l > maxwidth:
      a = a + 1
      n = text.rsplit(None, a)[0]
      if stext == n:
        cut += 1
        stext = n[:-cut]
      else:
        stext = n
      l = font.size(stext)[0]
      real = len(stext)
      done = False
    return real, done, stext


  def wrapline(self, text, font, maxwidth):
    done = False
    wrapped = []
  
    while not done:
      nl, done, stext = self.truncline(text, font, maxwidth)
      wrapped.append(stext.strip())
      text = text[nl:]
    return wrapped


  def wrap_multi_line(self, text, font, maxwidth):
    """ returns text taking new lines into account.
    """
    lines = chain(
      *(self.wrapline(line, font, maxwidth) for line in text.splitlines()))
    return list(lines)


  def getTextWidth(self, text_lines=None):
    if text_lines is None:
      text_lines = self.text_lines
      
    self.text_height = self.getTextHeight(text_lines)
    if self.text_height > self.height:
      text_width = self.width - SCROLLBAR_THICKNESS
    else:
      text_width = self.width
    return text_width
    
    
  # def getTextHeight(self):
  #
  #   text_height = 0
  #   for text_line_surface in self.text_surfaces:
  #     text_height += text_line_surface.get_height()
  #   return text_height


  def getTextHeight(self, text_lines=None):
    if text_lines is None:
      text_lines = self.text_lines
    
    font_height = self.font.get_height()
    if text_lines is self.text_lines:
      text_height = 0
    else:
      text_height = self.text_height
      
    text_height += len(text_lines) * font_height
    return text_height


  def getHeight(self):
    height = 0
    for text_line in self.text_surfaces:
      height += text_line.get_height()
  
    return height
  
    
  def buildScrollbar(self):
    self.rect = self.get_rect()
    if self.rect.height < self.text_surface.get_height():
      self.ratio = (1.0 * self.rect.height) / self.text_surface.get_height()
    self.track = pygame.Rect(self.rect.right - SCROLLBAR_THICKNESS,
                             self.rect.top, SCROLLBAR_THICKNESS,
                             self.rect.height)
    self.knob = pygame.Rect(self.track)
    self.knob.height = self.track.height * self.ratio


  def addText(self, text, color=(0, 0, 0)):
    self.text_width = self.getTextWidth()
    temp_text_lines = self.text_lines + self.wrap_multi_line(text, self.font, self.text_width)
    self.text_width = self.getTextWidth(temp_text_lines)
    
    length = len(self.text_lines)
    self.text_lines += self.wrap_multi_line(text, self.font, self.text_width)
    for i in range(length, len(self.text_lines)):
      self.text_surfaces.append(self.font.render(self.text_lines[i], 1, color))
    self.line_cursor = len(self.text_lines) - 1
    
    self.text_width = self.getTextWidth()
    self.text_height = self.getTextHeight()
    self.height = self.getHeight()
    self.text_surface = pygame.Surface((self.text_width, self.text_height))

    self.buildScrollbar()


  def clearText(self):
    self.text_surfaces = []<commit_msg>reverted dynamic text width, so we have a static text width = panel width - SCROLLBAR_THICKNESS<commit_after>import pygame
from itertools import chain
import pygame.locals as pl

BUTTON_SCROLL_WHEEL_UP = 4
BUTTON_SCROLL_WHEEL_DOWN = 5

SCROLLBAR_THICKNESS = 20
#TEXT_SCROLLBAR_SPACE = 10
SCROLL_SPEED = 20


class ScrolledTextPanel(pygame.Surface):
  def __init__(self, display, x, y, width, height, background_color=(255, 255, 255)):
    pygame.Surface.__init__(self,(width, height))

    self.focus = False
    self.label = ''

    self.display = display
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.font = self.display.getFont()
    self.background_color = background_color
    self.text_lines = []
    self.text_surfaces = []
    self.text_width = self.width
    self.text_height = self.height
    self.text_width = self.getTextWidth()
    self.text_height = self.getTextHeight()
    self.text_surface = pygame.Surface((self.text_width, self.text_height))

    self.line_cursor = 0
    self.speak_lines = True

    self.rect = self.get_rect()
    self.ratio = 1.0
    self.track = pygame.Rect(self.rect.right - SCROLLBAR_THICKNESS, self.rect.top, SCROLLBAR_THICKNESS, self.rect.height)
    self.knob = pygame.Rect(self.track)
    self.knob.height = self.track.height * self.ratio
    self.scrolling = False
    self.mouse_in_me = False


  @staticmethod
  def truncline(text, font, maxwidth):
    real = len(text)
    stext = text
    l = font.size(text)[0]
    cut = 0
    a = 0
    done = True
    # old = None
    while l > maxwidth:
      a = a + 1
      n = text.rsplit(None, a)[0]
      if stext == n:
        cut += 1
        stext = n[:-cut]
      else:
        stext = n
      l = font.size(stext)[0]
      real = len(stext)
      done = False
    return real, done, stext


  def wrapline(self, text, font, maxwidth):
    done = False
    wrapped = []
  
    while not done:
      nl, done, stext = self.truncline(text, font, maxwidth)
      wrapped.append(stext.strip())
      text = text[nl:]
    return wrapped


  def wrap_multi_line(self, text, font, maxwidth):
    """ returns text taking new lines into account.
    """
    lines = chain(
      *(self.wrapline(line, font, maxwidth) for line in text.splitlines()))
    return list(lines)


  def getTextWidth(self):
    text_width = self.width - SCROLLBAR_THICKNESS
    return text_width
    
    
  # def getTextHeight(self):
  #
  #   text_height = 0
  #   for text_line_surface in self.text_surfaces:
  #     text_height += text_line_surface.get_height()
  #   return text_height


  def getTextHeight(self):
    font_height = self.font.get_height()
    return len(self.text_lines) * font_height


  def getHeight(self):
    height = 0
    for text_line in self.text_surfaces:
      height += text_line.get_height()
  
    return height
  
    
  def buildScrollbar(self):
    self.rect = self.get_rect()
    if self.rect.height < self.text_surface.get_height():
      self.ratio = (1.0 * self.rect.height) / self.text_surface.get_height()
    self.track = pygame.Rect(self.rect.right - SCROLLBAR_THICKNESS,
                             self.rect.top, SCROLLBAR_THICKNESS,
                             self.rect.height)
    self.knob = pygame.Rect(self.track)
    self.knob.height = self.track.height * self.ratio


  def addText(self, text, color=(0, 0, 0)):
    self.text_width = self.getTextWidth()
    
    length = len(self.text_lines)
    self.text_lines += self.wrap_multi_line(text, self.font, self.text_width)
    for i in range(length, len(self.text_lines)):
      self.text_surfaces.append(self.font.render(self.text_lines[i], 1, color))
    self.line_cursor = len(self.text_lines) - 1
    
    self.text_width = self.getTextWidth()
    self.text_height = self.getTextHeight()
    self.height = self.getHeight()
    self.text_surface = pygame.Surface((self.text_width, self.text_height))

    self.buildScrollbar()


  def clearText(self):
    self.text_surfaces = []
{'fraction_non_alphanumeric': 0.11019640127013995, 'fraction_numerical': 0.008585205221686463, 'mean_word_length': 2.7812361049355268, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '6938931', 'n_tokens_mistral': 3007, 'n_tokens_neox': 2902, 'n_words': 609}