<commit_before>require 'ncursesw'

module Dephine

  # Public: Terminal User Interface for making a simple UI.
  # All methods are instance methods and should be called on an instance.
  #
  # Examples
  #
  #   t = TUI.new
  #   t.puts 'it uses ncurses'
  #   t.close
  #   # => 'it uses ncurses'
  class TUI
    # Public: Initialize a terminal user interface.
    def initialize
      Ncurses.initscr
      Ncurses.nonl
      Ncurses.cbreak
      Ncurses.noecho

      @screen = Ncurses.stdscr
      @screen.scrollok(true)
      @screen.keypad(true)

      @top = 0
      @contexts = []
      @callbacks = {}
    end

    def maxy
      @screen.getmaxy
    end

    # Public: Add a String to an Array named 'contexts'.
    # This method basically doesn't act like the built-in method 'puts',
    # but values of 'contexts' will be printed.
    #
    # text - The String that will be pushed to an Array.
    #
    # Returns the contexts Array.
    def puts(text = '')
      @contexts << text
    end

    # Public: Move up the scroll.
    #
    # Returns true or fase.
    def scroll_up
      if @top > 0
        @screen.scrl(-1)
        @top -= 1
        str = @contexts[@top]

        if str
          @screen.move(0, 0)
          @screen.addstr(str)
        end

        true

      else
        false
      end
    end

    # Public: Move down the scroll.
    #
    # Returns true or false.
    def scroll_down
      if @top + maxy < @contexts.length
        @screen.scrl(1)
        @top += 1
        str = @contexts[@top + maxy - 1]

        if str
          @screen.move(maxy - 1, 0)
          @screen.addstr(str)
        end

        true

      else
        false
      end
    end

    # Internal: Set some hot-keys for performing basic things.
    # Basic things are scroll up, scroll down and quit.
    #
    # Returns nothing.
    def hotkeys
      loop do
        c = @screen.getch

        case c
        when Ncurses::KEY_DOWN, 14
          scroll_down

        when Ncurses::KEY_UP, 16
          scroll_up

        when Ncurses::KEY_NPAGE<commit_msg>added support for j and k keys<commit_after>require 'ncursesw'

module Dephine

  # Public: Terminal User Interface for making a simple UI.
  # All methods are instance methods and should be called on an instance.
  #
  # Examples
  #
  #   t = TUI.new
  #   t.puts 'it uses ncurses'
  #   t.close
  #   # => 'it uses ncurses'
  class TUI
    # Public: Initialize a terminal user interface.
    def initialize
      Ncurses.initscr
      Ncurses.nonl
      Ncurses.cbreak
      Ncurses.noecho

      @screen = Ncurses.stdscr
      @screen.scrollok(true)
      @screen.keypad(true)

      @top = 0
      @contexts = []
      @callbacks = {}
    end

    def maxy
      @screen.getmaxy
    end

    # Public: Add a String to an Array named 'contexts'.
    # This method basically doesn't act like the built-in method 'puts',
    # but values of 'contexts' will be printed.
    #
    # text - The String that will be pushed to an Array.
    #
    # Returns the contexts Array.
    def puts(text = '')
      @contexts << text
    end

    # Public: Move up the scroll.
    #
    # Returns true or fase.
    def scroll_up
      if @top > 0
        @screen.scrl(-1)
        @top -= 1
        str = @contexts[@top]

        if str
          @screen.move(0, 0)
          @screen.addstr(str)
        end

        true

      else
        false
      end
    end

    # Public: Move down the scroll.
    #
    # Returns true or false.
    def scroll_down
      if @top + maxy < @contexts.length
        @screen.scrl(1)
        @top += 1
        str = @contexts[@top + maxy - 1]

        if str
          @screen.move(maxy - 1, 0)
          @screen.addstr(str)
        end

        true

      else
        false
      end
    end

    # Internal: Set some hot-keys for performing basic things.
    # Basic things are scroll up, scroll down and quit.
    #
    # Returns nothing.
    def hotkeys
      loop do
        c = @screen.getch

        case c
        when Ncurses::KEY_DOWN, 14, 'j'.ord
          scroll_down

        when Ncurses::KEY_UP, 16, 'k'.ord
          scroll_up

        when Ncurses::KEY_NPAGE
{'fraction_non_alphanumeric': 0.08539218037898777, 'fraction_numerical': 0.007195970256656272, 'mean_word_length': 1.6309148264984228, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '5479497', 'n_tokens_mistral': 1387, 'n_tokens_neox': 1320, 'n_words': 425}