<commit_before>require 'canis/core/system/ncurses'

module Canis
  module ColorMap
    # 2010-09-20 12:22 changed colors from string to symbol
    ## private
    # returns a color constant for a human color string
    def ColorMap.get_color_const colorstring
      # added check for fixnum if we go beyond these constants 2011-11-28 
      # e.g. to use full 256 colors
      return colorstring if colorstring.is_a? Fixnum 
      ret = FFI::NCurses.const_get "COLOR_#{colorstring.to_s.upcase}"
    end
    ## private
    # creates a new color pair, puts in color map and returns color_pair
    # number
    def ColorMap.install_color fgc, bgc
      #$log.debug " install_color found #{fgc} #{@bgc} "
      @color_id += 1
      fg = ColorMap.get_color_const fgc
      bg = ColorMap.get_color_const bgc
      FFI::NCurses.init_pair(@color_id, fg, bg);
      $color_map[[fgc, bgc]] = @color_id
      return @color_id
    end
    #
    # returns the colors that make up the given pair
    # you may want to find what makes up $bottomcolor and set color and bgcolor with it.
    # @param [Fixnum] color_pair
    # @return [Symbol, Symbol]  foreground and backgrounf color
    # @example 
    #     color, bgcolor = get_colors_for_pair $datacolor
    #
    def ColorMap.get_colors_for_pair pair
      $color_map.invert[pair]
    end
    ## public
    # returns a color_pair for a given foreground and background color
    def ColorMap.get_color fgc, bgc=$def_bg_color
      fgc = fgc.to_sym if fgc.is_a? String
      bgc = bgc.to_sym if bgc.is_a? String
      if $color_map.include? [fgc, bgc]
        #$log.debug " get_color found #{fgc} #{@bgc} "
        return $color_map[[fgc, bgc]]
      else
        #$log.debug " get_color NOT found #{fgc} #{@bgc} "
        return ColorMap.install_color fgc, bgc
      end
    end
    def ColorMap.colors
      @@colors
    end
    # returns true if color is a valid one, else false
    # @param [Symbol] color such as :black :cyan :yellow
    # @return [Boolean] true if valid, else false
    def ColorMap.is_color? color
      return true if color.is_a? Fixnum # so we can use 256 colors
      @@colors.include? color.to_sym
    end

    ## public
    # setup color map at start of application
    def ColorMap.setup
      @color_id = 0
      $color_map = {}
      FFI::NCurses.start_color();
      # Initialize few color pairs 
      $def_fg_color = :white   # pls set these 2 for your application
      $def_bg_color = :black
      #COLORS = [COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, COLOR_BLUE, 
      #     COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE]
      @@colors = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]

      # make foreground colors
      bg = ColorMap.get_color_const $def_bg_color
      @@colors[0...@@colors.size].each_with_index do |color, i|
        next if color == $def_bg_color # NOTE hope this doesn't do something if you change def_bg
        ColorMap.install_color color, $def_bg_color
      end
      $reversecolor = ColorMap.get_color $def_bg_color, $def_fg_color
      $popupcolor = ColorMap.get_color :cyan, $def_fg_color

      $errorcolor = ColorMap.get_color :white, :red
      #$promptcolor = $selectedcolor = ColorMap.get_color(:yellow, :red)
      $promptcolor = ColorMap.get_color(:yellow, :red)
      $normalcolor = $datacolor = ColorMap.get_color(:white, :black)
      $bottomcolor = $topcolor = ColorMap.get_color(:white, :blue)
      $selectedcolor = $datacolor # since we now use reverse attr in list

      $row_selected_attr = Ncurses::A_REVERSE
      $row_focussed_attr = Ncurses::A_BOLD
      $row_attr          = Ncurses::A_NORMAL

      #    $log.debug " colormap SETUP: #{$datacolor} #{$reversecolor} "
    end

  end # modul
  if $0 == __FILE__
    require 'logger'
    require 'ver/window'
    #include Ncurses # FFI 2011-09-8 
    include ColorMap
    # Initialize curses
    begin
      $log = Logger.new("canis.log")
      Canis::start_ncurses
      @window = Canis::Window.root_window
      $log.level = Logger::DEBUG
      ColorMap.setup

      # Create the window to be associated with the form 
      # Un post form and free the memory

      catch(:close) do
        #      $log.debug "START  ---------"
        # need to pass a form, not window.
        r = 1; c = 2; i=0
        attr = Ncurses::A_NORMAL
        @window.printstring  20, c, "press 0-9 to change BG color,  F1/q to quit. r-everse, n-ormal,b-old ", ColorMap.get_color('white')


<commit_msg>reset color_id<commit_after>require 'canis/core/system/ncurses'

# 2014-08-11 - 09:54 
# There is a limit to the number of color pairs one can make 0 .. COLOR_PAIRS - 1
# which i why i get junk colors after 255. In my program, color_pairs is 32767 ???
#
# curses.init_pair(pair_number, fg, bg)
# 
 #    Change the definition of a color-pair. It takes three arguments: the
# number of the color-pair to be changed, the foreground color number, and the
# background color number. The value of pair_number must be between 1 and
# COLOR_PAIRS - 1 (the 0 color pair is wired to white on black and cannot be
# changed). The value of fg and bg arguments must be between 0 and COLORS. If the
# color-pair was previously initialized, the screen is refreshed and all
# occurrences of that color-pair are changed to the new definition.
#   - https://docs.python.org/2/library/curses.html

module Canis
  module ColorMap
    # 2010-09-20 12:22 changed colors from string to symbol
    ## private
    # returns a color constant for a human color string
    def ColorMap.get_color_const colorstring
      # added check for fixnum if we go beyond these constants 2011-11-28 
      # e.g. to use full 256 colors
      return colorstring if colorstring.is_a? Fixnum 
      ret = FFI::NCurses.const_get "COLOR_#{colorstring.to_s.upcase}"
    end
    ## private
    # creates a new color pair, puts in color map and returns color_pair
    # number
    def ColorMap.install_color fgc, bgc
      #$log.debug " install_color found #{fgc} #{@bgc} "
      @color_id += 1
      # testing to see, since i get junk after 255 or so
      #@color_id = 255 if @color_id > 255
      fg = ColorMap.get_color_const fgc
      bg = ColorMap.get_color_const bgc
      FFI::NCurses.init_pair(@color_id, fg, bg);
      $color_map[[fgc, bgc]] = @color_id
      return @color_id
    end
    #
    # returns the colors that make up the given pair
    # you may want to find what makes up $bottomcolor and set color and bgcolor with it.
    # @param [Fixnum] color_pair
    # @return [Symbol, Symbol]  foreground and backgrounf color
    # @example 
    #     color, bgcolor = get_colors_for_pair $datacolor
    #
    def ColorMap.get_colors_for_pair pair
      $color_map.invert[pair]
    end
    ## public
    # returns a color_pair for a given foreground and background color
    def ColorMap.get_color fgc, bgc=$def_bg_color
      fgc = fgc.to_sym if fgc.is_a? String
      bgc = bgc.to_sym if bgc.is_a? String
      if $color_map.include? [fgc, bgc]
        #$log.debug " get_color found #{fgc} #{@bgc} "
        return $color_map[[fgc, bgc]]
      else
        #$log.debug " get_color NOT found #{fgc} #{@bgc} "
        return ColorMap.install_color fgc, bgc
      end
    end
    def ColorMap.colors
      @@colors
    end
    # returns true if color is a valid one, else false
    # @param [Symbol] color such as :black :cyan :yellow
    # @return [Boolean] true if valid, else false
    def ColorMap.is_color? color
      return true if color.is_a? Fixnum # so we can use 256 colors
      @@colors.include? color.to_sym
    end

    ## public
    # setup color map at start of application
    def ColorMap.setup
      @color_id = 0
      $color_map = {}
      FFI::NCurses.start_color();
      # Initialize few color pairs 
      $def_fg_color = :white   # pls set these 2 for your application
      $def_bg_color = :black
      #COLORS = [COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, COLOR_BLUE, 
      #     COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE]
      @@colors = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]

      # make foreground colors
      bg = ColorMap.get_color_const $def_bg_color
      @@colors[0...@@colors.size].each_with_index do |color, i|
        next if color == $def_bg_color # NOTE hope this doesn't do something if you change def_bg
        ColorMap.install_color color, $def_bg_color
      end
      $reversecolor = ColorMap.get_color $def_bg_color, $def_fg_color
      $popupcolor = ColorMap.get_color :cyan, $def_fg_color

      $errorcolor = ColorMap.get_color :white, :red
      #$promptcolor = $selectedcolor = ColorMap.get_color(:yellow, :red)
      $promptcolor = ColorMap.get_color(:yellow, :red)
      $normalcolor = $datacolor = ColorMap.get_color(:white, :black)
      $bottomcolor = $topcolor = ColorMap.get_color(:white, :blue)
      $selectedcolor = $datacolor # since we now use reverse attr in list

      $row_selected_attr = Ncurses::A_REVERSE
      $row_focussed_attr = Ncurses::A_BOLD
      $row_attr          = Ncurses::A_NORMAL

      #    $log.debug " colormap SETUP: #{$datacolor} #{$reversecolor} "
    end
    # reset the color_id to zero so one can create colors from scratch.
    # This is only to be used in the case of a color demo when you don't want the colors
    #  we originally made, since there seems to be a shortage of slots.
    def ColorMap.reset_color_id
      @color_id = 0
      $color_map = {}
    end

  end # modul
  if $0 == __FILE__
    require 'logger'
    require 'ver/window'
    #include Ncurses # FFI 2011-09-8 
    include ColorMap
    # Initialize curses
    begin
      $log = Logger.new("canis.log")
      Canis::start_ncurses
      @window = Canis::Window.root_window
      $log.level = Logger::DEBUG
      ColorMap.setup

      # Create the window to be associated with the form 
      # Un post form and free the memory

      catch(:close) do
        #      $log.debug "START  ---------"
        # need to pass a form, not window.
        r = 1; c = 2; i=0
        attr = Ncurses::A_NORMAL
        @window.printstring  20, c, "press 0-9 to change BG color,  F1/q to quit. r-everse, n-ormal,b-old ", ColorMap.get_color('white')



{'fraction_non_alphanumeric': 0.09405065776556057, 'fraction_numerical': 0.012664441390143335, 'mean_word_length': 2.701671511627907, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 1, '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': '2210777', 'n_tokens_mistral': 3493, 'n_tokens_neox': 3316, 'n_words': 1057}