Class: Vedeu::Editor::Cropper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Common
Defined in:
lib/vedeu/editor/cropper.rb

Overview

Crop the lines to the visible area of the document, as defined by the geometry provided.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines:, ox:, oy:, name:) ⇒ Vedeu::Editor::Cropper

Returns a new instance of Vedeu::Editor::Cropper.

Parameters:



28
29
30
31
32
33
# File 'lib/vedeu/editor/cropper.rb', line 28

def initialize(lines:, ox:, oy:, name:)
  @lines = lines
  @name  = present?(name) ? name : Vedeu.focus
  @ox    = ox
  @oy    = oy
end

Instance Attribute Details

#nameString|Symbol (readonly, protected)

Returns:

  • (String|Symbol)


62
63
64
# File 'lib/vedeu/editor/cropper.rb', line 62

def name
  @name
end

#oxFixnum (readonly, protected)

Returns:

  • (Fixnum)


66
67
68
# File 'lib/vedeu/editor/cropper.rb', line 66

def ox
  @ox
end

#oyFixnum (readonly, protected)

Returns:

  • (Fixnum)


70
71
72
# File 'lib/vedeu/editor/cropper.rb', line 70

def oy
  @oy
end

Instance Method Details

#absent?(variable) ⇒ Boolean Originally defined in module Common

Returns a boolean indicating whether a variable is nil or empty.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#borderVedeu::Borders::Border (private)



104
105
106
# File 'lib/vedeu/editor/cropper.rb', line 104

def border
  @border ||= Vedeu.borders.by_name(name)
end

#columns(line) ⇒ String (private)

Return a range of visible characters from each line.

Returns:

  • (String)


99
100
101
# File 'lib/vedeu/editor/cropper.rb', line 99

def columns(line)
  line[ox...(ox + width)] || ''
end

#demodulize(klass) ⇒ String Originally defined in module Common

Removes the module part from the expression in the string.

Examples:

demodulize('Vedeu::SomeModule::SomeClass') # => "SomeClass"

Parameters:

  • klass (Class|String)

Returns:

  • (String)

#interfaceVedeu::Interfaces::Interface (private)



92
93
94
# File 'lib/vedeu/editor/cropper.rb', line 92

def interface
  @interface ||= Vedeu.interfaces.by_name(name)
end

#linesVedeu::Editor::Lines (private)

Return a range of visible lines.



87
88
89
# File 'lib/vedeu/editor/cropper.rb', line 87

def lines
  @lines[oy...(oy + height)] || []
end

#present?(variable) ⇒ Boolean Originally defined in module Common

Returns a boolean indicating whether a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#snake_case(name) ⇒ String Originally defined in module Common

Converts a class name to a lowercase snake case string.

Examples:

snake_case(MyClassName) # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

Parameters:

  • name (String)

Returns:

  • (String)

#to_aArray<void>

Returns:

  • (Array<void>)


54
55
56
# File 'lib/vedeu/editor/cropper.rb', line 54

def to_a
  visible
end

#viewportArray<void>

Returns the visible lines as a sequence of View::Char objects.

Returns:

  • (Array<void>)


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vedeu/editor/cropper.rb', line 39

def viewport
  out = []

  visible.each_with_index do |line, iy|
    line.chars.each_with_index do |char, ix|
      out << Vedeu::Views::Char.new(parent:   interface,
                                    position: [(by + iy), (bx + ix)],
                                    value:    char.freeze)
    end
  end

  out
end

#visibleArray<void> (private)

Note:

If there are no lines of content, we return an empty

Returns the visible lines.

array. If there are any empty lines, then they are discarded.

Returns:

  • (Array<void>)


80
81
82
# File 'lib/vedeu/editor/cropper.rb', line 80

def visible
  lines.map { |line| columns(line) }
end