<commit_before>#!/usr/bin/env ruby
# ncurses-ruby unfortunatly has no built-in support for menus
# i - unfortunately - have no particular interest in porting it.
# therefore this is a simple menu class, hence its name.
# it's build to show nothing but the menu - no implementation in windows
# no nothing, although it should not be difficult to write that.
#
# This piece of code was written by <NAME> <<EMAIL>>
# and resembles free software. It is distributed under the GNU GPL v2.
# You did probablay not receive a copy of that.
# GFOAD.

require 'ncurses'

class SimpleMenu
  attr_reader :items, :index, :lines, :cols

  def initialize(list)
    set_items(list)
    @index = 0
  end

  def items=(list)
    set_items(list)
    @index = 0
    @scr.clear
    print_menu
  end

  private

  def set_items(list)
    @items = list
    @items << Item.new("Exit") { exit }
  end
 
  def calculate_size
    @lines = ( Ncurses.LINES / @items.length).floor
    @cols = Ncurses.COLS
  end

  def add(what)
    `echo "#{what}" >> /home/amens/rbtail.log`
  end
  # displays menu at current position
  #
  def print_menu
    
    @scr.refresh
    @windows = Array.new
    calculate_size

    @items.each do |i|
      additional = ""
      cindex = @items.index(i)
      
      number = @windows.length || 0
      @windows[cindex] = Ncurses::WINDOW.new(@lines, @cols, number * @lines, 0)
      w = @windows[cindex]
      w.border(*([0]*8))
      
      # highlight if necessary
      #
      if(cindex == @index)
        w.attron(Ncurses::COLOR_PAIR(2))
        w.attron(Ncurses::A_STANDOUT)
        additional = " * "
      end
      
      out = additional + i.to_s + additional

      w.move(@lines/2,(@cols - out.length)/2)
      w.addstr(out)
    end

    @windows.each { |w| w.noutrefresh } 
  end


 
  # moving the selection around
  #
  def move_upwards
    if @index > 0
      @index = @index - 1
    end
    print_menu
  end

  def move_downwards
    if @index < (@items.length - 1)
      @index = @index + 1
    end
    print_menu
  end

  # executes the command
  #
  def execute
    @items[@index].exec
  end

  public

  # does the main stuff.
  # initialize ncurses and go into the main loop
  # and clean up afterwards
  #
  def do_menu
    begin
      @scr = Ncurses.initscr()
      Ncurses.cbreak()
      Ncurses.noecho()
      Ncurses.curs_set(0) # hide cursor
      @scr.intrflush(false)
      @scr.keypad(true)

      # there's no such thing as no-color-terminals
      Ncurses.start_color()
      Ncurses.init_pair(1, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK) #std
      Ncurses.init_pair(2, Ncurses::COLOR_RED, Ncurses::COLOR_WHITE) #sel
      Ncurses.attron(Ncurses::COLOR_PAIR(1))
      Ncurses.attron(Ncurses::A_BOLD) 
      Ncurses.mousemask(Ncurses::BUTTON1_CLICKED, [])      

      print_menu
      # main loop
      #
      while(ch = Ncurses.getch)
        case ch
        when Ncurses::KEY_DOWN
          move_downwards

        when Ncurses::KEY_UP
          move_upwards
        
        when 10
          execute
        
        when Ncurses::KEY_MOUSE
          m = Ncurses::MEVENT.new
          Ncurses.getmouse(m)
          @index = (m.y / @lines)
          print_menu
          execute
        end

      end

    ensure
      stop
    end  
  end

  def stop
    Ncurses.echo()
    Ncurses.nocbreak()
    Ncurses.nl()
    Ncurses.endwin()
  end

end

class Item
  attr_accessor :caption, :command
  def initialize(caption, &cmd)
        @caption = caption
        @command = cmd
  end
  def to_s
    @caption
  end
  def exec
    @command.call
  end
end

<commit_msg>some fixes on da SimpleMenu<commit_after>#!/usr/bin/env ruby

# ncurses-ruby unfortunatly has no built-in support for menus i -
# unfortunately - have no particular interest in porting it.
# therefore this is a simple menu class, hence its name.  it's build
# to show nothing but the menu - no implementation in windows no
# nothing, although it should not be difficult to write that.
#

require 'ncurses'

# Presents a simple ncurses menu
# Needs a list of MenuItems and nothing else
class SimpleMenu
  attr_reader :items, :index, :lines, :cols

  def initialize(list)
    set_items(list)
    @index = 0
  end

  def items=(list)
    set_items(list)
    @index = 0
    @scr.clear
    print_menu
  end

  private

  def set_items(list)
    @items = list
    @items << MenuItem.new("Exit") { exit }
  end
 
  def calculate_size
    @lines = ( Ncurses.LINES / @items.length).floor
    @cols = Ncurses.COLS
  end

  # displays menu at current position
  #
  def print_menu
    
    @scr.refresh
    @windows = Array.new
    calculate_size

    @items.each do |i|
      additional = ""
      cindex = @items.index(i)
      
      number = @windows.length || 0
      @windows[cindex] = Ncurses::WINDOW.new(@lines, @cols, number * @lines, 0)
      w = @windows[cindex]
      w.border(*([0]*8))
      
      # highlight if necessary
      #
      if(cindex == @index)
        w.attron(Ncurses::COLOR_PAIR(2))
        w.attron(Ncurses::A_STANDOUT)
        additional = " * "
      end
      
      out = additional + i.to_s + additional

      w.move(@lines/2,(@cols - out.length)/2)
      w.addstr(out)
    end

    @windows.each { |w| w.noutrefresh } 
  end


 
  # moving the selection around
  #
  def move_upwards
    if @index > 0
      @index = @index - 1
    end
    print_menu
  end

  def move_downwards
    if @index < (@items.length - 1)
      @index = @index + 1
    end
    print_menu
  end

  # executes the command
  #
  def execute
    @items[@index].exec
  end

  public

  # does the main stuff.
  # initialize ncurses and go into the main loop
  # and clean up afterwards
  #
  def do_menu
    begin
      @scr = Ncurses.initscr()
      Ncurses.cbreak()
      Ncurses.noecho()
      Ncurses.curs_set(0) # hide cursor
      @scr.intrflush(false)
      @scr.keypad(true)

      # there's no such thing as no-color-terminals
      Ncurses.start_color()
      Ncurses.init_pair(1, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK) #std
      Ncurses.init_pair(2, Ncurses::COLOR_RED, Ncurses::COLOR_WHITE) #sel
      Ncurses.attron(Ncurses::COLOR_PAIR(1))
      Ncurses.attron(Ncurses::A_BOLD) 
      Ncurses.mousemask(Ncurses::BUTTON1_CLICKED, [])      

      print_menu
      # main loop
      #
      while(ch = Ncurses.getch)
        case ch
        when Ncurses::KEY_DOWN
          move_downwards

        when Ncurses::KEY_UP
          move_upwards
        
        when 10
          execute
        
        when Ncurses::KEY_MOUSE
          m = Ncurses::MEVENT.new
          Ncurses.getmouse(m)
          @index = (m.y / @lines)
          print_menu
          execute
        end

      end

    ensure
      stop
    end  
  end

  def stop
    Ncurses.echo()
    Ncurses.nocbreak()
    Ncurses.nl()
    Ncurses.endwin()
  end

end

# consists of a caption and a ruby-proc. pretty easy, eh?
class MenuItem
  attr_accessor :caption, :command
  def initialize(caption, &cmd)
        @caption = caption
        @command = cmd
  end
  def to_s
    @caption
  end
  def exec
    @command.call
  end
end


{'fraction_non_alphanumeric': 0.09789029535864979, 'fraction_numerical': 0.0057665260196905765, 'mean_word_length': 2.147853032315184, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 10, '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': '5800081', 'n_tokens_mistral': 2522, 'n_tokens_neox': 2431, 'n_words': 634}