module Scryglass

test

Constants

HELP_SCREEN
HELP_SCREEN_ADVANCED
VERSION

Public Class Methods

config() click to toggle source
# File lib/scryglass.rb, line 107
def self.config
  @config ||= Config.new
end
configure() { |config| ... } click to toggle source
# File lib/scryglass.rb, line 115
def self.configure
  yield(config)
end
demo_hash() click to toggle source
# File lib/example_material.rb, line 65
def self.demo_hash
  {
    :time => Time.now,
    :temperature => 'Just right',
    :data => [
      [
        ScryExampleClass.new,
        ScryExampleClass.new,
      ],
      [
        ScryExampleClass.new,
        ScryExampleClass.new,
      ],
      [
        ScryExampleClass.new,
        ScryExampleClass.new,
      ],
      [
        ScryExampleClass.new,
        ScryExampleClass.new,
      ],
      [
        ScryExampleClass.new,
        ScryExampleClass.new,
      ],
      [
        ScryExampleClass.new,
        ScryExampleClass.new,
      ],
    ]
  }
end
help() click to toggle source
# File lib/scryglass.rb, line 144
  def self.help
    console_help = <<~"CONSOLE_HELP" # Bolded with \e[1m
      \e[1m
      |  To prep Scryglass, call `Scryglass.load`
      |  (Or add it to .irbrc & .pryrc)
      |
      |  To start a Scry Session, call:
      |  >   scry my_object   OR
      |  >   my_object.scry
      |
      |  To resume the previous session:   (in same console session)
      |  >   scry
      \e[0m
    CONSOLE_HELP

    puts console_help
  end
load() click to toggle source
# File lib/scryglass.rb, line 129
def self.load
  caller_path = caller_locations.first.path

  silent_load_result = Scryglass.load_silently

  if silent_load_result[:success]
    puts "(Scryglass is loaded, from `#{caller_path}`. Use `Scryglass.help` for help getting started)"
  else
    puts "(Scryglass failed to load, from `#{caller_path}` " \
         "getting `#{silent_load_result[:error].message}`)"
  end

  silent_load_result
end
load_silently() click to toggle source
# File lib/scryglass.rb, line 119
def self.load_silently
  begin
    add_kernel_method
    create_scryglass_session_manager
    { success: true, error: nil }
  rescue => e
    { success: false, error: e }
  end
end
reset_config() click to toggle source
# File lib/scryglass.rb, line 111
def self.reset_config
  @config = Config.new
end
test_hash() click to toggle source
# File lib/example_material.rb, line 16
def self.test_hash
  {
    :move_into_this_sub_item_with_down_arrow => "I'm just a string",
    :disclaimer => 'This giant playground object is still no substitute for just pressing `?`...',
    :an_empty_array => [],
    :an_array_you_can_open_with_right_arrow => [
      1,
      1.0,
      'You can close this array, anywhere here, with left arrow',
      :an_empty_hash => {},
      :some_more_advanced_stuff => {
        'What about lens view?' => [
          "I'm a string, but you can prove it by pressing spacebar to toggle lens view",
          "There's lots to do with lens view, but the help screens ('?') are really best."
        ],
        'What is my cursor telling me about this Range object?' => (1...12),
        nil => "this line is so long you should use the w/a/s/d ('d' in particular) to see more. If you can see the whole line all at once, your text/screen ratio might need some balancing..." * 10,
        [1,2,3] => "Can't expand/collapse keys, sorry...",
        Scryglass::ViewWrapper.new('the actual string', string: 'This key appears differently in the tree view!') => Scryglass::ViewWrapper.new(24601, string: 'So do this integer!'),
        "A lambda" => ->(o) { puts o.inspect.upcase.reverse },
        "\n\n\n" => "If you want to know what that lambda does, move your cursor to it, press ENTER, and it will be returned to your console for you to play with!",
        'Allll the coolest stuff' => [
          Scryglass::ViewWrapper.new('https://github.com/annkissam/scryglass', string: '...Is really in the help screens (`?`) and the README. Press spacebar for README link in lens view'),
        ],
        :delicious_data => {
          :whoa_now => ([1]*800).map { rand(2) }.insert($stdout.winsize.first, 'Press zero to reset view position, then again to reset cursor there!'),
          999 => {
          4.1 => [4,1],
          5.1 => [5,1],
          6.1 => [6,1],
          7.1 => [7,1],
          8.1 => [8,1],
          9.1 => [9,1],
          0.1 => [0,1],
          1.1 => [1,1],
        },
          Array => [1,2, [1,2,[3]], [3, 88, [99, 100]], 0],
          'Some test classes with instance variables' => [
            ScryExampleClass.new,
            ScryExampleClass.new,
            ScryExampleClass.new,
            ScryExampleClass.new,
          ],
        },
      },
    ],
  }
end

Private Class Methods

add_kernel_method() click to toggle source
# File lib/scryglass.rb, line 168
def self.add_kernel_method
  Kernel.module_eval do
    def scry(arg = nil, _actions = nil)
      # `actions` can't be a keyword arg due to this ruby issue:
      #   https://bugs.ruby-lang.org/issues/8316

      Scryglass.config.validate!

      current_console_binding = binding.of_caller(1)

      receiver_is_just_the_console = self == current_console_binding.receiver
      receiver = self unless receiver_is_just_the_console
      # As in: `receiver.scry`,
      #   and no receiver means scry was called on 'main', (unless self is
      #   different in the because you've pry'd into something!)

      seed_object = arg || receiver

      if seed_object
        # If it's been given an arg or receiver, create new session!
        # The global variable is purposeful, and not accessible outside of
        #   the one particular console instance.
        $scry_session_manager << Scryglass::Session.new(seed_object)
      end

      unless $scry_session_manager.current_session
        raise ArgumentError,
              '`scry` requires either an argument, a receiver, or a past ' \
              'session to reopen. try `Scryglass.help`'
      end

      $scry_session_manager.track_binding!(current_console_binding)

      begin
        Hexes.stdout_rescue do
          $scry_session_manager.run_scry_ui
        end
      rescue => e # Here we ensure good visibility in case of errors
        screen_height, _screen_width = $stdout.winsize
        $stdout.write "\e[#{screen_height};1H\n" # (Moves console cursor to bottom left corner)
        raise e
      end
    end
  end
end
create_scryglass_session_manager() click to toggle source
# File lib/scryglass.rb, line 164
def self.create_scryglass_session_manager
  $scry_session_manager = Scryglass::SessionManager.new
end

Private Instance Methods

scry(arg = nil, _actions = nil) click to toggle source
# File lib/scryglass.rb, line 170
def scry(arg = nil, _actions = nil)
  # `actions` can't be a keyword arg due to this ruby issue:
  #   https://bugs.ruby-lang.org/issues/8316

  Scryglass.config.validate!

  current_console_binding = binding.of_caller(1)

  receiver_is_just_the_console = self == current_console_binding.receiver
  receiver = self unless receiver_is_just_the_console
  # As in: `receiver.scry`,
  #   and no receiver means scry was called on 'main', (unless self is
  #   different in the because you've pry'd into something!)

  seed_object = arg || receiver

  if seed_object
    # If it's been given an arg or receiver, create new session!
    # The global variable is purposeful, and not accessible outside of
    #   the one particular console instance.
    $scry_session_manager << Scryglass::Session.new(seed_object)
  end

  unless $scry_session_manager.current_session
    raise ArgumentError,
          '`scry` requires either an argument, a receiver, or a past ' \
          'session to reopen. try `Scryglass.help`'
  end

  $scry_session_manager.track_binding!(current_console_binding)

  begin
    Hexes.stdout_rescue do
      $scry_session_manager.run_scry_ui
    end
  rescue => e # Here we ensure good visibility in case of errors
    screen_height, _screen_width = $stdout.winsize
    $stdout.write "\e[#{screen_height};1H\n" # (Moves console cursor to bottom left corner)
    raise e
  end
end