<commit_before>autosize = require 'autosize'

{later} = require './vutil'

isModifierKey = (ev) -> ev.ctrlKey || ev.metaKey || ev.shiftKey

cursorToEnd = (el) -> el.selectionStart = el.selectionEnd = el.value.length

history = []
historyIndex = 0
historyLength = 100
historyBackup = ""

historyPush = (data) ->
  history.push data
  if history.length == historyLength then history.shift()
  historyIndex = history.length

historyWalk = (el, offset) ->
  # if we are starting to dive into history be backup current message
  if offset is -1 and historyIndex is history.length then historyBackup = el.value
  historyIndex = historyIndex + offset
  # constrain index
  if historyIndex < 0 then historyIndex = 0
  if historyIndex > history.length then historyIndex = history.length
  # if don't have history value restore 'current message'
  val = history[historyIndex] or historyBackup
  el.value = val
  setTimeout (-> cursorToEnd el), 1

module.exports = view (models) ->
    div class:'input', -> div ->
        textarea autofocus:true, placeholder:'Message', rows: 1, ''
        , onDOMNodeInserted: (e) ->
            # at this point the node is still not inserted
            ta = e.target
            later -> autosize ta
        , onkeydown: (e) ->
            return if isModifierKey e
            if e.keyCode == 13
                e.preventDefault()
                action 'sendmessage', e.target.value
                historyPush e.target.value
                e.target.value = ''
            if e.keyIdentifier is "Up" then historyWalk e.target, -1
            if e.keyIdentifier is "Down" then historyWalk e.target, +1
<commit_msg>code formatting<commit_after>autosize = require 'autosize'

{later} = require './vutil'

isModifierKey = (ev) -> ev.ctrlKey || ev.metaKey || ev.shiftKey

cursorToEnd = (el) -> el.selectionStart = el.selectionEnd = el.value.length

history = []
historyIndex = 0
historyLength = 100
historyBackup = ""

historyPush = (data) ->
    history.push data
    if history.length == historyLength then history.shift()
    historyIndex = history.length

historyWalk = (el, offset) ->
    # if we are starting to dive into history be backup current message
    if offset is -1 and historyIndex is history.length then historyBackup = el.value
    historyIndex = historyIndex + offset
    # constrain index
    if historyIndex < 0 then historyIndex = 0
    if historyIndex > history.length then historyIndex = history.length
    # if don't have history value restore 'current message'
    val = history[historyIndex] or historyBackup
    el.value = val
    setTimeout (-> cursorToEnd el), 1

module.exports = view (models) ->
    div class:'input', -> div ->
        textarea autofocus:true, placeholder:'Message', rows: 1, ''
        , onDOMNodeInserted: (e) ->
            # at this point the node is still not inserted
            ta = e.target
            later -> autosize ta
        , onkeydown: (e) ->
            return if isModifierKey e
            if e.keyCode == 13
                e.preventDefault()
                action 'sendmessage', e.target.value
                historyPush e.target.value
                e.target.value = ''
            if e.keyIdentifier is "Up" then historyWalk e.target, -1
            if e.keyIdentifier is "Down" then historyWalk e.target, +1

{'fraction_non_alphanumeric': 0.09842995169082126, 'fraction_numerical': 0.00785024154589372, 'mean_word_length': 2.5169851380042463, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '2234957', 'n_tokens_mistral': 943, 'n_tokens_neox': 919, 'n_words': 307}