<commit_before>module Canis
  module Utils
    private
    def _suspend clear=true
      return unless block_given?
      Ncurses.def_prog_mode
      if clear
        Ncurses.endwin 
        # NOTE: avoid false since screen remains half off
        # too many issues
      else
        system "/bin/stty sane"
      end
      yield if block_given?
      Ncurses.reset_prog_mode
      if !clear
        # Hope we don't screw your terminal up with this constantly.
        Canis::stop_ncurses
        Canis::start_ncurses  
        #<EMAIL>.reset_all # not required
      end
      @form.repaint if @form
      @window.wrefresh if @window
      Ncurses::Panel.update_panels
    end
    #
    # Suspends to shell so user can execute commands.
    # Maybe not be able to get your prompt correctly.
    # 
    public
    def suspend
      _suspend(false) do
        system("tput cup 26 0")
        system("tput ed")
        system("echo Enter C-d to return to application")
        system (ENV['PS1']='\s-\v\$ ') if ENV['SHELL']== '/bin/bash'
        system(ENV['SHELL']);
      end
    end

    #
    # prompts user for unix command and displays output in viewer
    # 
    def shell_output
      $shell_history ||= []
      cmd = get_string("Enter shell command:", :maxlen => 50) do |f|
        require 'canis/core/include/rhistory'
        f.extend(FieldHistory)
        f.history($shell_history)
      end
      if cmd && !cmd.empty?
        run_command cmd
        $shell_history.push(cmd) unless $shell_history.include? cmd
      end
    end

    #
    # executes given command and displays in viewer
    # @param [String] unix command, e.g., git -st
    def run_command cmd
      # http://whynotwiki.com/Ruby_/_Process_management#What_happens_to_standard_error_.28stderr.29.3F
      require 'canis/core/util/viewer'
      begin
        res = `#{cmd} 2>&1`
      rescue => ex
        res = ex.to_s
        res << ex.backtrace.join("\n") 
      end
      res.gsub!("\t","   ")
      Canis::Viewer.view(res.split("\n"), :close_key => KEY_ENTER, :title => "<Enter> to close, M-l M-h to scroll")
    end
    def shell_out command
      w = @window || @form.window
      w.hide
      Ncurses.endwin
      ret = system command
      Ncurses.refresh
      #Ncurses.curs_set 0  # why ?
      w.show
      return ret
    end
  end # utils
  class PrefixCommand
    attr_accessor :object
    def initialize _symbol, calling, config={}, &block
      @object = calling
      @symbol = _symbol
      @descriptions = {}
      define_prefix_command _symbol
      yield self if block_given?
    end
    def define_prefix_command _name, config={}
      $rb_prefix_map ||= {}<commit_msg>change close key to q since ENTER used to invoke stuff<commit_after>module Canis
  module Utils
    private
    def _suspend clear=true
      return unless block_given?
      Ncurses.def_prog_mode
      if clear
        Ncurses.endwin 
        # NOTE: avoid false since screen remains half off
        # too many issues
      else
        system "/bin/stty sane"
      end
      yield if block_given?
      Ncurses.reset_prog_mode
      if !clear
        # Hope we don't screw your terminal up with this constantly.
        Canis::stop_ncurses
        Canis::start_ncurses  
        #@form.reset_all # not required
      end
      @form.repaint if @form
      @window.wrefresh if @window
      Ncurses::Panel.update_panels
    end
    #
    # Suspends to shell so user can execute commands.
    # Maybe not be able to get your prompt correctly.
    # 
    public
    def suspend
      _suspend(false) do
        system("tput cup 26 0")
        system("tput ed")
        system("echo Enter C-d to return to application")
        system (ENV['PS1']='\s-\v\$ ') if ENV['SHELL']== '/bin/bash'
        system(ENV['SHELL']);
      end
    end

    #
    # prompts user for unix command and displays output in viewer
    # 
    def shell_output
      $shell_history ||= []
      cmd = get_string("Enter shell command:", :maxlen => 50) do |f|
        require 'canis/core/include/rhistory'
        f.extend(FieldHistory)
        f.history($shell_history)
      end
      if cmd && !cmd.empty?
        run_command cmd
        $shell_history.push(cmd) unless $shell_history.include? cmd
      end
    end

    #
    # executes given command and displays in viewer
    # @param [String] unix command, e.g., git -st
    def run_command cmd
      # http://whynotwiki.com/Ruby_/_Process_management#What_happens_to_standard_error_.28stderr.29.3F
      require 'canis/core/util/viewer'
      begin
        res = `#{cmd} 2>&1`
      rescue => ex
        res = ex.to_s
        res << ex.backtrace.join("\n") 
      end
      res.gsub!("\t","   ")
      # Earlier close key was ENTER but we need that to execute or fire
      Canis::Viewer.view(res.split("\n"), :close_key => 'q', :title => "<q> to close, M-l M-h to scroll")
    end
    def shell_out command
      w = @window || @form.window
      w.hide
      Ncurses.endwin
      ret = system command
      Ncurses.refresh
      #Ncurses.curs_set 0  # why ?
      w.show
      return ret
    end
  end # utils
  class PrefixCommand
    attr_accessor :object
    def initialize _symbol, calling, config={}, &block
      @object = calling
      @symbol = _symbol
      @descriptions = {}
      define_prefix_command _symbol
      yield self if block_given?
    end
    def define_prefix_command _name, config={}
      $rb_prefix_map ||= {}
{'fraction_non_alphanumeric': 0.09667340562396619, 'fraction_numerical': 0.005146112846903143, 'mean_word_length': 2.111492281303602, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 16, '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': '804383', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1690, 'n_words': 489}