<commit_before>require "stackflame/version"
require "stackprof"

class Stackflame
  def self.profile(mode: :cpu, &block)
    stackflame = self.new
    stackflame.run(mode, &block)
    stackflame.open_flamegraph
  end

  def run(mode, &block)
    result = StackProf.run(mode: mode, raw: true, &block)

    File.open(temp_js_path, 'w') do |f|
      StackProf::Report.new(result).print_flamegraph(f)
    end
  end

  def open_flamegraph
    if system("which osascript > /dev/null")
      # NOTE: `open` can't open path with query string
      `osascript -e 'open location "#{flamegraph_path}"'`
    else
      puts "This OS is not supported. Please open: #{flamegraph_path}"
    end
  end

  private

  def flamegraph_path
    viewer_path = File.expand_path('../../vendor/viewer.html', __FILE__)
    "file://#{viewer_path}\?data=#{temp_js_path}"
  end

  def temp_js_path
    return @js_path if @js_path

    temp = `mktemp /tmp/stackflame-XXXXXXXX`.strip
    @js_path = "#{temp}.js"
    `mv #{temp} #{@js_path}`
    @js_path
  end
end<commit_msg>Fix nits<commit_after>require "stackflame/version"
require "stackprof"

class Stackflame
  def self.profile(mode: :cpu, &block)
    stackflame = self.new
    stackflame.run(mode, &block)
    stackflame.open_flamegraph
  end

  def run(mode, &block)
    result = StackProf.run(mode: mode, raw: true, &block)

    File.open(temp_js_path, 'w') do |f|
      StackProf::Report.new(result).print_flamegraph(f)
    end
  end

  def open_flamegraph
    if system("which osascript > /dev/null")
      # NOTE: `open` can't open path with query string
      `osascript -e 'open location "#{flamegraph_path}"'`
    else
      puts "This OS is not supported. Please open: #{flamegraph_path}"
    end
  end

  private

  def flamegraph_path
    viewer_path = File.expand_path('../../vendor/viewer.html', __FILE__)
    "file://#{viewer_path}?data=#{temp_js_path}"
  end

  def temp_js_path
    return @js_path if @js_path

    temp = `mktemp /tmp/stackflame-XXXXXXXX`.strip
    @js_path = "#{temp}.js"
    `mv #{temp} #{@js_path}`
    @js_path
  end
end
{'fraction_non_alphanumeric': 0.13112391930835735, 'fraction_numerical': 0.0, 'mean_word_length': 3.294845360824742, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '6672489', 'n_tokens_mistral': 769, 'n_tokens_neox': 755, 'n_words': 176}