<commit_before># FuzzyWindowController.rb
# FuzzyWindow
#
# Created by <NAME> on 3/16/10.
# Copyright 2010 Topfunky Corporation. All rights reserved.

class FuzzyWindowController < NSWindowController

  attr_accessor :tableViewController, :searchField, :statusLabel, :projectRoot

  def windowDidLoad
    setWindowFrameAutosaveName("com.topfunky.PeepOpen.FuzzyWindowController.frame")
  end

  def show(sender)
    NSApp.activateIgnoringOtherApps(true)
    showWindow self
    tableViewController.selectFirstRow
    searchField.setStringValue("")
    window.makeFirstResponder(searchField)
    updateStatusLabel
  end

  def close
    window.close
  end

  def loadFilesFromProjectRoot(theProjectRoot)
    self.projectRoot = FuzzyRecord.discoverProjectRootForDirectoryOrFile(theProjectRoot)
    tableViewController.loadFilesFromProjectRoot(self.projectRoot)
    if tableViewController.allRecords.length == 0
      NSLog "No files found"
    end
    # TODO: Catch FuzzyRecord::ProjectRootNotFoundError
  end

  ##
  # Called when text is entered into the search field.

  def didSearchForString(sender)
    tableViewController.searchForString(sender.stringValue)
    updateStatusLabel
  end

  def updateStatusLabel
    statusLabel.stringValue = "%i records" % [tableViewController.records.size]
  end

  ##
  # Handle Enter, arrows, and other events in search field.
  #
  # Returns true if this class handles it, false otherwise.

  def control(control, textView:textView, doCommandBySelector:commandSelector)
    # NSLog "cmd #{commandSelector}"
    case commandSelector
    when :"insertTab:"
      # Tab should not be used...arrow keys work automatically when in
      # search field.
      tableViewController.selectNextRow
      return true

    when :"insertNewline:"
      handleNewline
      return true

    when :"moveUp:"
      tableViewController.selectPreviousRow
      return true

    when :"moveDown:"
      tableViewController.selectNextRow
      return true

    when :"cancelOperation:"
      # Triggered when ESC is hit but search field has text in it
      if (searchField.stringValue != "")
        searchField.setStringValue("")
        didSearchForString(searchField)
      else
        handleCancel
      end
      return true

    when :"cancel:"
      # Triggered when ESC is hit with blank search field
      handleCancel
      return true

    when :"noop:"
      unless handleKeyWithModifier
        handleCancel
      end
      return true

    end
    # Other Events: :"pageDown:"
    return false
  end

  def handleKeyWithModifier
    modifierFlags = NSApp.currentEvent.modifierFlags
    if ((modifierFlags & NSCommandKeyMask) == NSCommandKeyMask)
      case NSApp.currentEvent.charactersIgnoringModifiers
      when /r/
        refreshFileList(self)
        return true
      end
    elsif ((modifierFlags & NSControlKeyMask) == NSControlKeyMask)
      # NSLog "Ctrl is down"
    end
    false
  end

  def refreshFileList(sender)
    FuzzyRecord.flushCache(projectRoot)
    tableViewController.reset
    loadFilesFromProjectRoot(projectRoot)
    didSearchForString(searchField)
  end

  def handleNewline
    if tableViewController.handleRowClick(tableViewController.tableView.selectedRow)
      window.close
    else
      runWarningAlertWithMessage("No Text Editor Found", informativeText:"Please choose a text editor in PeepOpen preferences.")
    end
  end
<commit_msg>Load files in background thread<commit_after># FuzzyWindowController.rb
# FuzzyWindow
#
# Created by <NAME> on 3/16/10.
# Copyright 2010 Topfunky Corporation. All rights reserved.

class FuzzyWindowController < NSWindowController

  attr_accessor :tableViewController, :searchField, :statusLabel, :projectRoot

  def windowDidLoad
    setWindowFrameAutosaveName("com.topfunky.PeepOpen.FuzzyWindowController.frame")
  end

  def show(sender)
    NSApp.activateIgnoringOtherApps(true)
    showWindow self
    tableViewController.selectFirstRow
    searchField.setStringValue("")
    window.makeFirstResponder(searchField)
    updateStatusLabel
  end

  def close
    window.close
  end

  def loadFilesFromProjectRoot(theProjectRoot)
    self.performSelectorInBackground("loadFilesFromProjectRootAsync:", withObject:theProjectRoot)
  end

  def loadFilesFromProjectRootAsync(theProjectRoot)
    self.projectRoot = FuzzyRecord.discoverProjectRootForDirectoryOrFile(theProjectRoot)
    tableViewController.loadFilesFromProjectRoot(self.projectRoot)
    if tableViewController.allRecords.length == 0
      NSLog "No files found"
    end
    # TODO: Catch FuzzyRecord::ProjectRootNotFoundError
    self.performSelectorOnMainThread("didFinishLoadingFilesFromProjectRoot",
                                     withObject:nil,
                                     waitUntilDone:false)
  end

  def didFinishLoadingFilesFromProjectRoot
    NSLog "LOADED"
    didSearchForString(searchField)
    updateStatusLabel
  end

  def refreshFileList(sender)
    FuzzyRecord.flushCache(projectRoot)
    tableViewController.reset
    loadFilesFromProjectRoot(projectRoot)
  end


  ##
  # Called when text is entered into the search field.

  def didSearchForString(sender)
    tableViewController.searchForString(sender.stringValue)
    updateStatusLabel
  end

  def updateStatusLabel
    statusLabel.stringValue = "%i records" % [tableViewController.records.size]
  end

  ##
  # Handle Enter, arrows, and other events in search field.
  #
  # Returns true if this class handles it, false otherwise.

  def control(control, textView:textView, doCommandBySelector:commandSelector)
    # NSLog "cmd #{commandSelector}"
    case commandSelector
    when :"insertTab:"
      # Tab should not be used...arrow keys work automatically when in
      # search field.
      tableViewController.selectNextRow
      return true

    when :"insertNewline:"
      handleNewline
      return true

    when :"moveUp:"
      tableViewController.selectPreviousRow
      return true

    when :"moveDown:"
      tableViewController.selectNextRow
      return true

    when :"cancelOperation:"
      # Triggered when ESC is hit but search field has text in it
      if (searchField.stringValue != "")
        searchField.setStringValue("")
        didSearchForString(searchField)
      else
        handleCancel
      end
      return true

    when :"cancel:"
      # Triggered when ESC is hit with blank search field
      handleCancel
      return true

    when :"noop:"
      unless handleKeyWithModifier
        handleCancel
      end
      return true

    end
    # Other Events: :"pageDown:"
    return false
  end

  def handleKeyWithModifier
    modifierFlags = NSApp.currentEvent.modifierFlags
    if ((modifierFlags & NSCommandKeyMask) == NSCommandKeyMask)
      case NSApp.currentEvent.charactersIgnoringModifiers
      when /r/
        refreshFileList(self)
        return true
      end
    elsif ((modifierFlags & NSControlKeyMask) == NSControlKeyMask)
      # NSLog "Ctrl is down"
    end
    false
  end

  def handleNewline
    if tableViewController.handleRowClick(tableViewController.tableView.selectedRow)
      window.close
    else
      runWarningAlertWithMessage("No Text Editor Found", informativeText:"Please choose a text editor in PeepOpen preferences.")
    end
  end

{'fraction_non_alphanumeric': 0.06336769759450171, 'fraction_numerical': 0.0027491408934707906, 'mean_word_length': 3.34647550776583, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7140072', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1941, 'n_words': 506}