<commit_before>module Byebug
  #
  # Show (and possibily stop) at every line that changes a global variable.
  #
  class TracevarCommand < Command
    self.allow_in_post_mortem = false

    def regexp
      /^\s* tr(?:acevar)? (?: \s+ (\S+))?  # (variable-name)?
                          (?: \s+ (\S+))?  # (stop | nostop)?
       \s*$/x
    end

    def execute
      var = @match[1]
      return errmsg('tracevar needs a global variable name') unless var

      unless global_variables.include?(:"#{var}")
        return errmsg("'#{var}' is not a global variable.")
      end

      if @match[2] && @match[2] !~ /(:?no)?stop/
        return errmsg("expecting 'stop' or 'nostop'; got '#{@match[2]}'")
      end

      dbg_cmd = @match[2] && @match[2] !~ /nostop/ ? 'byebug(1, false)' : ''

      eval("trace_var(:\"#{var}\") do |val|
              puts \"traced global variable '#{var}' has value '\#{val}'\"
              #{dbg_cmd}
            end")
      puts "Tracing global variable \"#{var}\"."
    end

    class << self
      def names
        %w(tracevar)<commit_msg>Cleaner way of tracing globals<commit_after>module Byebug
  #
  # Show (and possibily stop) at every line that changes a global variable.
  #
  class TracevarCommand < Command
    self.allow_in_post_mortem = false

    def regexp
      /^\s* tr(?:acevar)? (?: \s+ (\S+))?  # (variable-name)?
                          (?: \s+ (\S+))?  # (stop | nostop)?
       \s*$/x
    end

    def execute
      var = @match[1]
      return errmsg('tracevar needs a global variable name') unless var

      unless global_variables.include?(:"#{var}")
        return errmsg("'#{var}' is not a global variable.")
      end

      if @match[2] && @match[2] !~ /(:?no)?stop/
        return errmsg("expecting 'stop' or 'nostop'; got '#{@match[2]}'")
      end

      stop = @match[2] && @match[2] !~ /nostop/

      instance_eval do
        trace_var(:"#{var}") { |val| on_change(var, val, stop) }
      end

      puts "Tracing global variable \"#{var}\"."
    end

    def on_change(name, value, stop)
      puts "traced global variable '#{name}' has value '#{value}'"
      byebug(1, false) if stop
    end

    class << self
      def names
        %w(tracevar)
{'fraction_non_alphanumeric': 0.16719101123595506, 'fraction_numerical': 0.006292134831460674, 'mean_word_length': 2.2028776978417266, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '5605922', 'n_tokens_mistral': 750, 'n_tokens_neox': 741, 'n_words': 207}