<commit_before>prompt = require('sync-prompt').prompt
fs = require('fs')
pygmentize = require('pygmentize-bundled')
deasync = require("deasync")
chalk = require('chalk')

class File

  constructor: (@file, @line) ->

  type: ->
    if @file.match /coffee/
      'coffeescript'
    else
      'javascript'

  content: ->
    fs.readFileSync(@file).toString()

  formatted_content: ->
    done = false
    data = null
    pygmentize
      lang: @type()
      format: "terminal"
    , @content(), (err, res) =>
      done = true
      data = @add_line_numbers(res.toString())
    deasync.runLoopOnce() until done
    data

  add_line_numbers: (content) ->
    lines = content.split("\n")
    space = (longest, line_number) ->
      long = String(longest).length
      now = String(line_number).length
      new Array(long - now + 1).join(' ')
    space = space.bind(@, lines.length + 1)
    for line,key in lines
      pointer = "    "
      pointer = " => " if key+1 == @line
      lines[key] = "#{pointer}#{space(key+1)}#{chalk.cyan(key+1)}: #{line}"
    lines.join("\n")


class Position

  _stack: null

  constructor: ->
    @_stack = new Error().stack

  show: (before = 5, after = 5) ->
    console.log(@file()
      .formatted_content()
      .split("\n")
      .slice(@line() - (before + 1), @line() + after)
      .join("\n"))

  file: ->
    @_file ||= new File(@stack()[1], @line())

  line: ->
    parseInt(@stack()[2], 10)

  stack: ->
    @_stack.split("\n")[4].match(/\(([^:]+):(\d+)/)

class Presenter

  pos: null

  constructor: ->
    @pos = new Position()

  whereami: ->
    @pos.show()
    true

  stop: ->
    false

pry = (scope) ->
  presenter = new Presenter()
  count = 0
  presenter.whereami()
  (pr = ->
    output = prompt("[#{count}] pry> ")
    count++
    if presenter[output]
      pr() if presenter[output]()
    else
      console.log("=> #{scope(output)}")
      pr()
  )()


module.exports = pry<commit_msg>Move prompt into the presenter.<commit_after>prompt = require('sync-prompt').prompt
fs = require('fs')
pygmentize = require('pygmentize-bundled')
deasync = require("deasync")
chalk = require('chalk')

class File

  constructor: (@file, @line) ->

  type: ->
    if @file.match /coffee/
      'coffeescript'
    else
      'javascript'

  content: ->
    fs.readFileSync(@file).toString()

  formatted_content: ->
    done = false
    data = null
    pygmentize
      lang: @type()
      format: "terminal"
    , @content(), (err, res) =>
      done = true
      data = @add_line_numbers(res.toString())
    deasync.runLoopOnce() until done
    data

  add_line_numbers: (content) ->
    lines = content.split("\n")
    space = (longest, line_number) ->
      long = String(longest).length
      now = String(line_number).length
      new Array(long - now + 1).join(' ')
    space = space.bind(@, lines.length + 1)
    for line,key in lines
      pointer = "    "
      pointer = " => " if key+1 == @line
      lines[key] = "#{pointer}#{space(key+1)}#{chalk.cyan(key+1)}: #{line}"
    lines.join("\n")


class Position

  _stack: null

  constructor: ->
    @_stack = new Error().stack

  show: (before = 5, after = 5) ->
    console.log(@file()
      .formatted_content()
      .split("\n")
      .slice(@line() - (before + 1), @line() + after)
      .join("\n"))

  file: ->
    @_file ||= new File(@stack()[1], @line())

  line: ->
    parseInt(@stack()[2], 10)

  stack: ->
    @_stack.split("\n")[4].match(/\(([^:]+):(\d+)/)

class Presenter

  pos: null

  prompt_count: 0

  constructor: (@scope) ->
    @pos = new Position()

  whereami: ->
    @pos.show()
    true

  stop: ->
    false

  prompt: ->
    output = prompt("[#{@prompt_count}] pry> ")
    if @[output]
      @prompt() if @[output]()
    else
      console.log("=> #{@scope(output)}")
      @prompt()
    @prompt_count += 1

pry = (scope) ->
  presenter = new Presenter(scope)
  count = 0
  presenter.whereami()
  presenter.prompt()

module.exports = pry
{'fraction_non_alphanumeric': 0.18775201612903225, 'fraction_numerical': 0.007560483870967742, 'mean_word_length': 2.5628366247755836, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, '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': '595586', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1369, 'n_words': 300}