Login
window.cr at tip
Login

File src/remicharms/window.cr from the latest check-in


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
   100
   101
   102
   103
   104
   105
   106
   107
   108
   109
   110
   111
   112
   113
   114
   115
   116
   117
   118
   119
   120
   121
   122
   123
   124
   125
   126
   127
   128
   129
   130
   131
   132
   133
   134
   135
   136
   137
   138
   139
   140
   141
   142
   143
   144
   145
   146
   147
   148
   149
   150
   151
   152
   153
   154
   155
   156
   157
   158
   159
   160
   161
   162
   163
   164
   165
   166
   167
   168
   169
   170
   171
   172
   173
   174
   175
   176
   177
   178
   179
   180
   181
   182
   183
   184
   185
   186
   187
   188
   189
   190
   191
   192
   193
   194
   195
   196
   197
   198
   199
   200
   201
   202
   203
   204
   205
   206
   207
   208
   209
   210
   211
   212
   213
   214
   215
   216
   217
   218
   219
   220
   221
   222
   223
   224
   225
   226
   227
   228
   229
   230
   231
   232
   233
   234
   235
   236
   237
   238
   239
   240
   241
   242
   243
   244
   245
   246
   247
   248
   249
   250
   251
   252
   253
   254
   255
   256
   257
   258
   259
   260
   261
   262
   263
   264
   265
   266
   267
   268
   269
   270
   271
   272
   273
   274
   275
   276
   277
   278
   279
   280
   281
   282
   283
   284
   285
   286
   287
   288
   289
   290
   291
   292
   293
   294
   295
   296
   297
   298
   299
   300
   301
   302
   303
   304
   305
   306
   307
   308
   309
   310
   311
   312
   313
   314
   315
   316
   317
   318
   319
   320
   321
   322
   323
   324
   325
   326
   327
   328
   329
   330
   331
   332
   333
   334
   335
   336
   337
   338
   339
   340
   341
   342
   343
   344
   345
   346
   347
   348
   349
   350
   351
   352
   353
   354
   355
   356
   357
   358
   359
   360
   361
   362
   363
   364
   365
   366
   367
   368
   369
   370
   371
   372
   373
   374
   375
   376
   377
   378
   379
   380
   381
   382
   383
   384
   385
   386
   387
   388
   389
   390
   391
   392
   393
   394
   395
   396
   397
   398
   399
   400
   401
   402
   403
   404
   405
   406
   407
   408
   409
   410
   411
   412
   413
   414
   415
   416
   417
   418
   419
   420
   421
   422
   423
   424
   425
   426
   427
   428
   429
   430
   431
   432
   433
   434
   435
   436
   437
   438
   439
   440
   441
   442
   443
   444
   445
   446
   447
   448
   449
   450
   451
   452
   453
   454
   455
   456
   457
   458
   459
   460
   461
   462
   463
   464
   465
   466
   467
   468
   469
   470
   471
   472
   473
#### RemiCharms
#### Based on CL-Charms
#### Copyright (C) 2023 Remilia Scarlet <remilia@posteo.jp>
#### Copyright (c) 2014 Robert Smith <quad@symbo1ics.com>
####
#### This program is free software: you can redistribute it and/or
#### modify it under the terms of the GNU Affero General Public
#### License as published by the Free Software Foundation, either
#### version 3 of the License, or (at your option) any later version.
####
#### This program is distributed in the hope that it will be useful,
#### but WITHOUT ANY WARRANTY; without even the implied warranty of
#### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#### Affero General Public License for more details.
####
#### You should have received a copy of the GNU Affero General Public License
#### along with this program.  If not, see <https://www.gnu.org/licenses/>.

module RemiCharms
  # A high-level representation of a curses window.
  #
  # You can still call the lower-level bindings by using the `#pointer` field
  # where necessary.
  class Window
    @@standardWindow : Window?
    @@cachedWindow : Window = Window.new

    # Pointer to the underlying representation of a window pointer.  You can use
    # this in calls to the lower-level bindings.
    getter pointer : NCurses::WindowPtr

    protected def initialize
      @pointer = NCurses::WindowPtr.null
    end

    protected def initialize(@pointer : NCurses::WindowPtr)
    end

    # Creates a new `Window` with the given *width* and *height*, starting at
    # the coordinate (*startX*, *startY*).
    #
    # Note that windows may not overlap.
    def initialize(width : Int, height : Int, startX : Int, startY : Int)
      # FIXME: The behavior of this function is special if WIDTH or HEIGHT are
      # 0. Either document this behavior or disallow it.
      #
      # We should check if this window overlaps with another, and appropriately
      # warn.
      @pointer = NCurses.newwin(height, width, startY, startX)
      if @pointer.null?
        raise Error.new("Failed to allocate a new window")
      end
    end

    # Cleans up the internal pointer if `#destroy` hasn't yet been called when
    # this instance is garbage collected.  If `#destroy` has been called, this
    # does nothing.
    #
    # See
    # [https://crystal-lang.org/reference/1.9/syntax_and_semantics/finalize.html]()
    # for more details
    def finalize
      unless @pointer.null?
        RemiCharms.checkStatus(NCurses.delwin(@pointer))
        @pointer = NCurses::WindowPtr.null
      end
    end

    protected def pointer=(@pointer : NCurses::WindowPtr) : Nil
    end

    # Destroys this window.
    def destroy : Nil
      RemiCharms.checkStatus(NCurses.delwin(@pointer))
      @pointer = NCurses::WindowPtr.null
    end

    # Creates a copy of this window.
    def dup : Window
      newPtr = NCurses.dupwin(@pointer)
      if newPtr.null?
        raise Error.new("Failed to copy the window")
      else
        Window.new(newPtr)
      end
    end

    # Returns the width and height of this window, in that order.
    @[AlwaysInline]
    def size : Tuple(Int32, Int32)
      y, x = NCurses.getmaxyx(@pointer)
      {x, y}
    end

    # Returns the `stdscr` in curses.
    def self.standardWindow : Window
      if @@standardWindow.nil?
        stdscr = LibNCurses.stdscr

        # Update the cached window if necessary.
        unless stdscr.address == @@cachedWindow.pointer.address
          if stdscr.null?
            raise Error.new("No standard window exists.  Did you initialize correctly?")
          else
            @@cachedWindow.pointer = stdscr
          end
        end

        @@standardWindow = @@cachedWindow
      end

      @@standardWindow.not_nil!
    end

    # Refresh the display of this window.
    def refresh : Nil
      RemiCharms.checkStatus(NCurses.wrefresh(@pointer))
    end

    # Forces the entire window to be cleared and repainted on the next call to
    # `#refresh`.
    def forceRepaint : Nil
      RemiCharms.checkStatus(NCurses.clearok(@pointer, NCurses::TRUE))
    end

    # Blank out the contents of this window. If *repaint* is `true`, then the
    # window will be repainted entirely in the next refresh.  Using this option
    # can be more optimally performant than calling `#forceRepaint` manually.
    def clear(repaint : Bool = false) : Nil
      if repaint
        RemiCharms.checkStatus(NCurses.wclear(@pointer))
      else
        RemiCharms.checkStatus(NCurses.werase(@pointer))
      end
    end

    # Clear the rest of the window after the cursor.
    def clearAfterCursor : Nil
      # XXX: Man page says "returns an error if the cursor position is about to
      # wrap
      RemiCharms.checkStatus(NCurses.wclrtobot(@pointer))
    end

    # Clear the rest of the line after the cursor.
    def clearLineAfterCursor : Nil
      RemiCharms.checkStatus(NCurses.wclrtoeol(@pointer))
    end

    # Returns the character at the cursor in this window.
    def charAt : Char
      NCurses.winch(@pointer).chr
    end

    # Returns the character at the point (*x*, *y*) in this window.
    def charAt(x : Int, y : Int) : Char
      NCurses.mvwinch(@pointer, y, x).chr
    end

    # Get a character from this window.  In the event a character is not ready
    # or could not be returned, this will raise an `Error` if *ignoreError* is
    # `false`, or return `nil` otherwise.
    @[AlwaysInline]
    def getChar(ignoreError : Bool = false) : Char?
      ch = NCurses.wgetch(@pointer)
      case
      when ch != NCurses::ERR then ch.chr
      when ignoreError then nil
      else raise Error.new("Error getting character")
      end
    end

    # Same as `#getChar`, but the *ignoreError* parameter for it is always `true`.
    def getChar! : Char?
      getChar(true)
    end

    # Insert the character *ch* at the cursor within this window, advancing the
    # rest of the line, without moving the cursor.  This is akin to pressing the
    # 'insert' key and typing a character.
    def insert(ch : Char) : Nil
      RemiCharms.checkStatus(NCurses.winsch(@pointer, ch.ord))
    end

    # Insert the character *ch* at the coordinates (*x*, *y*) within this
    # window, advancing the rest of the line, without moving the cursor.  This
    # is akin to pressing the 'insert' key and typing a character.
    def insert(ch : Char, x : Int, y : Int) : Nil
      RemiCharms.checkStatus(NCurses.winsch(@pointer, ch.ord))
    end

    # Returns `true` if the coordinate (*x*, *y*) is at the last position within
    # this window, or `false` otherwise.
    def lastPos?(x : Int, y : Int) : Bool
      width, height = size
      x == width - 1 && y == height - 1
    end

    # Writes *ch* at the last position of this window.  This assumes the width
    # of the window is at least 2.
    def writeAtLastPos(ch : Char) : Nil
      width, height = size
      insert(ch, width - 1, height - 1)
    end

    # Writes the character *ch* in this window at the cursor.
    @[AlwaysInline]
    def write(ch : Char) : Nil
      x, y = cursorPos
      if lastPos?(x, y)
        writeAtLastPos(ch)
      else
        RemiCharms.checkStatus(NCurses.waddch(@pointer, ch.ord))
      end
    end

    # Writes the string *str* in this window at the cursor.
    def write(str : String) : Nil
      RemiCharms.checkStatus(NCurses.waddstr(@pointer, str.to_unsafe))
    end

    # Writes the character *ch* in this window at the coordinates (*x*, *y*).
    @[AlwaysInline]
    def write(ch : Char, x : Int, y : Int) : Nil
      if lastPos?(x, y)
        writeAtLastPos(ch)
      else
        RemiCharms.checkStatus(NCurses.mvwaddch(@pointer, y, x, ch.ord))
      end
    end

    # Writes the string *str* in this window at the coordinates (*x*, *y*).
    def write(str : String, x : Int, y : Int) : Nil
      RemiCharms.checkStatus(NCurses.mvwaddstr(@pointer, y, x, str.to_unsafe))
    end

    def write(ch : Acs) : Nil
      RemiCharms.checkStatus(NCurses.waddch(@pointer, ch.fromMap))
    end

    def write(ch : Acs, x : Int, y : Int) : Nil
      RemiCharms.checkStatus(NCurses.mvwaddch(@pointer, y, x, ch.fromMap))
    end

    # Returns the X and Y coordinates of the cursor in this window, in that
    # order.
    @[AlwaysInline]
    def cursorPos : Tuple(Int32, Int32)
      y, x = NCurses.getyx(@pointer)
      {x, y}
    end

    # Moves the cursor in this window to the coordinates (*x*, *y*).
    def moveCursor(x : Int, y : Int) : Nil
      RemiCharms.checkStatus(NCurses.wmove(@pointer, y, x))
    end

    # Records the current cursor position in this window, then yields.  This
    # will then ensure that the cursor is restored to its original position
    # before calling this function.
    def withRestoredCursor(&) : Nil
      oldX, oldY = cursorPos
      yield
      moveCursor(oldX, oldY)
    end

    # Moves the cursor in this window up by *delta* characters.  A negative
    # *delta* moves the cursor down instead.
    @[AlwaysInline]
    def cursorUp(delta : Int32 = 1) : Nil
      x, y = cursorPos
      moveCursor(x, Math.max(0, y - delta))
    end

    # Moves the cursor in this window down by *delta* characters.  A negative
    # *delta* moves the cursor up instead.
    @[AlwaysInline]
    def cursorDown(delta : Int32 = 1) : Nil
      x, y = cursorPos
      moveCursor(x, Math.max(0, y + delta)) # Remi: 0?
    end

    # Moves the cursor in this window left by *delta* characters.  A negative
    # *delta* moves the cursor right instead.
    @[AlwaysInline]
    def cursorLeft(delta : Int32 = 1) : Nil
      x, y = cursorPos
      moveCursor(Math.max(0, x - delta), y)
    end

    # Moves the cursor in this window right by *delta* characters.  A negative
    # *delta* moves the cursor left instead.
    @[AlwaysInline]
    def cursorRight(delta : Int32 = 1) : Nil
      x, y = cursorPos
      moveCursor(Math.max(0, x + delta), y) # Remi: 0?
    end

    # Enable or disable extra keys, such as arrow and function keys, in this
    # window.
    def extraKeys=(value : Bool) : Nil
      if value
        RemiCharms.checkStatus(NCurses.keypad(@pointer, NCurses::TRUE))
      else
        RemiCharms.checkStatus(NCurses.keypad(@pointer, NCurses::FALSE))
      end
    end

    # Enables or disables non-blocking mode for this window. When enabled, this
    # will cause character input functions to not block and error (or return
    # `nil`)."
    def nonBlocking=(value : Bool) : Nil
      if value
        RemiCharms.checkStatus(NCurses.nodelay(@pointer, NCurses::TRUE))
      else
        RemiCharms.checkStatus(NCurses.nodelay(@pointer, NCurses::FALSE))
      end
    end

    # Turn on the attributes for this window without affecting any others.
    def attributeOn(*attrs : Attribute|Color|Int16|UInt32) : Nil
      attributeOn(RemiCharms.combineAttrs(*attrs))
    end

    # :ditto:
    def attributeOn(attrs : Attribute|Color|Int16|UInt32) : Nil
      RemiCharms.checkStatus(NCurses.wattron(@pointer, RemiCharms.combineAttrs(attrs)))
    end

    # Turn off the attributes for this window without affecting any others.
    def attributeOff(*attrs : Attribute|Color|Int16|UInt32) : Nil
      attributeOff(RemiCharms.combineAttrs(*attrs))
    end

    # :ditto:
    def attributeOff(attrs : Attribute|Color|Int16|UInt32) : Nil
      RemiCharms.checkStatus(NCurses.wattroff(@pointer, RemiCharms.combineAttrs(attrs)))
    end

    # Temporarily turns on the given attributes for this window in the block,
    # then turns them off before returning.
    def withAttributes(attrs : Attribute|UInt32, &) : Nil
      attributeOn(attrs)
      yield
      attributeOff(attrs)
    end

    # Draws a border around this window using the default ACS characters.
    def border : Nil
      RemiCharms.checkStatus(NCurses.wborder(@pointer,
                                             Acs::Vline, Acs::Vline, # Left, right
                                             Acs::Hline, Acs::Hline, # Top, bottom
                                             Acs::Ulcorner, Acs::Urcorner,
                                             Acs::Llcorner, Acs::Lrcorner))
    end

    # Draws a border around this window using the given ACS characters.  The
    # same character is used for the top as the bottom, and the same for the
    # left as the right.
    @[AlwaysInline]
    def border(topAndBottom : Acs, leftAndRight, upperLeft : Acs, upperRight : Acs,
               lowerLeft : Acs, lowerRight : Acs) : Nil
      border(topAndBottom, topAndBottom, leftAndRight, leftAndRight,
             upperLeft, upperRight, lowerLeft, lowerRight)
    end

    # Draws a border around this window using the given ACS characters.
    def border(topSide : Acs, bottomSide : Acs, leftSide : Acs, rightSide : Acs,
               upperLeft : Acs, upperRight : Acs, lowerLeft : Acs, lowerRight : Acs) : Nil
      RemiCharms.checkStatus(NCurses.wborder(@pointer,
                                             leftSide, rightSide, topSide, bottomSide,
                                             upperLeft, upperRight, lowerLeft, lowerRight))
    end

    # Drawa a border around this window using the given character.  The same
    # character is used for the top as the bottom, and the same for the left as
    # the right.  The characters must be ASCII-based.
    @[AlwaysInline]
    def border(topAndBottom : Char, leftAndRight, upperLeft : Char, upperRight : Char,
               lowerLeft : Char, lowerRight : Char) : Nil
      border(topAndBottom, topAndBottom, leftAndRight, leftAndRight,
             upperLeft, upperRight, lowerLeft, lowerRight)
    end

    # Drawa a border around this window using the given characters.  The
    # characters must be ASCII-based.
    def border(topSide : Char, bottomSide : Char, leftSide : Char, rightSide : Char,
               upperLeft : Char, upperRight : Char, lowerLeft : Char, lowerRight : Char) : Nil
      unless topSide.ascii? && bottomSide.ascii? && leftSide.ascii? && rightSide.ascii? &&
             upperLeft.ascii? && upperRight.ascii? && lowerLeft.ascii? && lowerRight.ascii?
        raise Error.new("Only ASCII characters are accepted")
      end
      RemiCharms.checkStatus(NCurses.wborder(@pointer,
                                             leftSide.ord, rightSide.ord,
                                             topSide.ord, bottomSide.ord,
                                             upperLeft.ord, upperRight.ord,
                                             lowerLeft.ord, lowerRight.ord))
    end

    # Draws a horizontal line at the cursor that is *count* characters wide
    # using `Acs::Hline` for the character.
    def hline(count : Int) : Nil
      RemiCharms.checkStatus(NCurses.whline(@pointer, Acs::Hline, count))
    end

    # Draws a horizontal line at the coordinates that is *count* characters wide
    # using `Acs::Hline` for the character.
    def hline(x : Int, y : Int, count : Int) : Nil
      RemiCharms.checkStatus(NCurses.mvwhline(@pointer, y, x, Acs::Hline, count))
    end

    # Draws a vertical line at the cursor that is *count* characters wide
    # using `Acs::Vline` for the character.
    def vline(count : Int) : Nil
      RemiCharms.checkStatus(NCurses.wvline(@pointer, Acs::Vline, count))
    end

    # Draws a vertical line at the coordinates that is *count* characters wide
    # using `Acs::Vline` for the character.
    def vline(x : Int, y : Int, count : Int) : Nil
      RemiCharms.checkStatus(NCurses.mvwvline(@pointer, y, x, Acs::Vline, count))
    end

    # Draws a horizontal line at the cursor that is *count* characters wide
    # using the given ACS character.
    def hline(ch : Acs, count : Int) : Nil
      RemiCharms.checkStatus(NCurses.whline(@pointer, ch, count))
    end

    # Draws a horizontal line at the coordinates that is *count* characters wide
    # using the given ACS character.
    def hline(ch : Acs, x : Int, y : Int, count : Int) : Nil
      RemiCharms.checkStatus(NCurses.mvwhline(@pointer, y, x, ch, count))
    end

    # Draws a vertical line at the cursor that is *count* characters wide
    # using the given ACS character.
    def vline(ch : Acs, count : Int) : Nil
      RemiCharms.checkStatus(NCurses.wvline(@pointer, ch, count))
    end

    # Draws a vertical line at the coordinates that is *count* characters wide
    # using the given ACS character.
    def vline(ch : Acs, x : Int, y : Int, count : Int) : Nil
      RemiCharms.checkStatus(NCurses.mvwvline(@pointer, y, x, ch, count))
    end

    # Draws a horizontal line at the cursor that is *count* characters wide
    # using the given character.  The character must be ASCII-based.
    def hline(ch : Char, count : Int) : Nil
      RemiCharms.checkStatus(NCurses.whline(@pointer, ch.ord, count))
    end

    # Draws a horizontal line at the coordinates that is *count* characters wide
    # using the given character.  The character must be ASCII-based.
    def hline(ch : Char, x : Int, y : Int, count : Int) : Nil
      RemiCharms.checkStatus(NCurses.mvwhline(@pointer, y, x, ch.ord, count))
    end

    # Draws a vertical line at the cursor that is *count* characters wide
    # using the given character.  The character must be ASCII-based.
    def vline(ch : Char, count : Int) : Nil
      RemiCharms.checkStatus(NCurses.wvline(@pointer, ch.ord, count))
    end

    # Draws a vertical line at the coordinates that is *count* characters wide
    # using the given character.  The character must be ASCII-based.
    def vline(ch : Char, x : Int, y : Int, count : Int) : Nil
      RemiCharms.checkStatus(NCurses.mvwvline(@pointer, y, x, ch.ord, count))
    end

    # TODO: scrollok, idlok, idcok
  end
end