<commit_before>require './../../../libs/mousetrap.js'
require './../../../libs/mousetrap-global-bind.js'

module.exports = class KDKeyboardListener

  constructor: ->
    @maps = {}
    @isListening = no

  makeUpdater = (fn) -> ->
    { isListening } = this
    @reset()  if isListening
    retVal = fn.apply this, arguments
    @listen()  if isListening
    return retVal

  addComboMap: makeUpdater (comboMap, priority) ->
    m = @maps[priority ? comboMap.priority ? 0] ?= []
    m.push comboMap
    return this

  removeComboMap: makeUpdater (comboMap) ->
    for own priority, ms of @maps
      @maps[priority] = ms.filter (m) -> m isnt comboMap
    return this

  listen: ->
    return this  if @isActive()

    KDKeyboardListener.currentListener?.reset()

    seen = {}
    @combos (combo, options = { global: yes }, listener) ->
      return  if seen[combo]
      seen[combo] = yes
      method = if options.global then 'bindGlobal' else 'bind'
      Mousetrap[method] combo, listener

    KDKeyboardListener.currentListener = this
    @isListening = yes
    return this

  reset: ->
    return this  unless @isActive()

    Mousetrap.reset()
    @isListening = no
    KDKeyboardListener.currentListener = null
    return this

  getCombos: ->
    Object.keys @maps
      # prioritize by key:
      .sort (a, b) -> b - a # descending priority
      # map back to value:
      .map (k) => @maps[k]
      # flatten:
      .reduce (a, b) ->
        a.concat b
      , []

  combos: (fn) ->
    @getCombos().forEach (m) -> m.eachCombo fn
    return this

  isActive: ->
    @isListening and this is KDKeyboardListener.currentListener

  @current = ->
    return @currentListener  if @currentListener?

    @currentListener = new this
    @currentListener.listen()
    return @currentListener<commit_msg>KDKeyboardListener: add static method 'record(callback)' that lets recording keyboard sequences<commit_after>require './../../../libs/mousetrap.js'
require './../../../libs/mousetrap-global-bind.js'
require './../../../libs/mousetrap-record.js'

module.exports = class KDKeyboardListener

  constructor: ->
    @maps = {}
    @isListening = no

  makeUpdater = (fn) -> ->
    { isListening } = this
    @reset()  if isListening
    retVal = fn.apply this, arguments
    @listen()  if isListening
    return retVal

  addComboMap: makeUpdater (comboMap, priority) ->
    m = @maps[priority ? comboMap.priority ? 0] ?= []
    m.push comboMap
    return this

  removeComboMap: makeUpdater (comboMap) ->
    for own priority, ms of @maps
      @maps[priority] = ms.filter (m) -> m isnt comboMap
    return this

  listen: ->
    return this  if @isActive()

    KDKeyboardListener.currentListener?.reset()

    seen = {}
    @combos (combo, options = { global: yes }, listener) ->
      return  if seen[combo]
      seen[combo] = yes
      method = if options.global then 'bindGlobal' else 'bind'
      Mousetrap[method] combo, listener

    KDKeyboardListener.currentListener = this
    @isListening = yes
    return this

  reset: ->
    return this  unless @isActive()

    Mousetrap.reset()
    @isListening = no
    KDKeyboardListener.currentListener = null
    return this

  getCombos: ->
    Object.keys @maps
      # prioritize by key:
      .sort (a, b) -> b - a # descending priority
      # map back to value:
      .map (k) => @maps[k]
      # flatten:
      .reduce (a, b) ->
        a.concat b
      , []

  combos: (fn) ->
    @getCombos().forEach (m) -> m.eachCombo fn
    return this

  isActive: ->
    @isListening and this is KDKeyboardListener.currentListener

  @current = ->
    return @currentListener  if @currentListener?

    @currentListener = new this
    @currentListener.listen()
    return @currentListener

  @record = (fn) ->
    Mousetrap.record fn.bind fn
    return

{'fraction_non_alphanumeric': 0.12575479128380151, 'fraction_numerical': 0.0005250721974271463, 'mean_word_length': 2.5050597976080957, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 38, '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': '7370892', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1204, 'n_words': 322}