2020-09-06 11:32:08 +02:00
|
|
|
;; tinmop: an humble gemini and pleroma client
|
2021-08-22 12:56:35 +02:00
|
|
|
;; Copyright (C) 2020,2021 cage
|
2020-05-08 15:45:43 +02:00
|
|
|
|
|
|
|
;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
(in-package :line-oriented-window)
|
|
|
|
|
|
|
|
(defclass line ()
|
|
|
|
((selected-fg
|
|
|
|
:initform :black
|
|
|
|
:initarg :selected-fg
|
|
|
|
:accessor selected-fg
|
|
|
|
:documentation "The foreground color for a selected line")
|
|
|
|
(selected-bg
|
|
|
|
:initform :cyan
|
|
|
|
:initarg :selected-bg
|
|
|
|
:accessor selected-bg
|
|
|
|
:documentation "The background color for a selected line")
|
|
|
|
(normal-fg
|
|
|
|
:initform :cyan
|
|
|
|
:initarg :normal-fg
|
|
|
|
:accessor normal-fg
|
|
|
|
:documentation "The foreground color for a line")
|
|
|
|
(normal-bg
|
|
|
|
:initform :black
|
|
|
|
:initarg :normal-bg
|
|
|
|
:accessor normal-bg
|
|
|
|
:documentation "The background color for a line")
|
|
|
|
(normal-text
|
2023-07-15 14:33:31 +02:00
|
|
|
:initform (make-tui-string "…")
|
2020-05-08 15:45:43 +02:00
|
|
|
:initarg :normal-text
|
|
|
|
:accessor normal-text
|
|
|
|
:documentation "The actual not selected text")
|
|
|
|
(selected-text
|
2023-07-15 14:33:31 +02:00
|
|
|
:initform (make-tui-string "…")
|
2020-05-08 15:45:43 +02:00
|
|
|
:initarg :selected-text
|
|
|
|
:accessor selected-text
|
|
|
|
:documentation "The actual selected text")
|
|
|
|
(deleted-text
|
2023-07-15 14:33:31 +02:00
|
|
|
:initform (make-tui-string "…")
|
2020-05-08 15:45:43 +02:00
|
|
|
:initarg :deleted-text
|
|
|
|
:accessor deleted-text
|
|
|
|
:documentation "The actual deleted text ")
|
|
|
|
(fields
|
|
|
|
:initform ()
|
|
|
|
:initarg :fields
|
|
|
|
:accessor fields
|
|
|
|
:documentation "A generic plist of useful informations for the window")
|
|
|
|
(index
|
|
|
|
:initform 0
|
|
|
|
:initarg :index
|
|
|
|
:accessor index
|
|
|
|
:documentation "The index of this line in the window")
|
|
|
|
(selected
|
|
|
|
:initform nil
|
|
|
|
:initarg :selected
|
|
|
|
:reader selectedp
|
|
|
|
:writer (setf selected)
|
|
|
|
:documentation "Non nil if this line is selected state"))
|
|
|
|
(:documentation "This class represents a single line in a row-oriented-widget"))
|
|
|
|
|
2021-04-05 12:01:30 +02:00
|
|
|
(defmethod print-object ((object line) stream)
|
2021-04-10 13:52:56 +02:00
|
|
|
(format stream "line: ~s" (tui-string->chars-string (normal-text object))))
|
2021-04-05 12:01:30 +02:00
|
|
|
|
2020-05-08 15:45:43 +02:00
|
|
|
(defclass row-oriented-widget ()
|
|
|
|
((rows
|
|
|
|
:initform ()
|
|
|
|
:initarg :rows
|
|
|
|
:accessor rows
|
|
|
|
:documentation "The rows of data for this widget")
|
|
|
|
(row-selected-index
|
2020-07-26 12:04:46 +02:00
|
|
|
:initform 0
|
2020-05-08 15:45:43 +02:00
|
|
|
:initarg :row-selected-index
|
|
|
|
:accessor row-selected-index
|
|
|
|
:documentation "The index of the selected row")
|
|
|
|
(single-row-height
|
|
|
|
:initform 1
|
|
|
|
:initarg :single-row-height
|
|
|
|
:accessor single-row-height
|
|
|
|
:documentation "The height of a row (in character)")
|
|
|
|
(top-row-padding
|
|
|
|
:initform 0
|
|
|
|
:initarg :top-row-padding
|
|
|
|
:accessor top-row-padding
|
|
|
|
:documentation "the padding from the top of the window and the
|
|
|
|
position where to draw the first line")
|
|
|
|
(current-row-index
|
|
|
|
:initform 0
|
|
|
|
:initarg :current-row-index
|
|
|
|
:accessor current-row-index
|
|
|
|
:documentation "The active line index")
|
|
|
|
(y-current-row
|
|
|
|
:initform 0
|
|
|
|
:initarg :y-current-row
|
|
|
|
:accessor y-current-row
|
2020-06-22 13:58:04 +02:00
|
|
|
:documentation "The active line position")
|
|
|
|
(top-rows-slice
|
|
|
|
:initform 0
|
|
|
|
:initarg :top-rows-slice
|
|
|
|
:accessor top-rows-slice
|
|
|
|
:documentation "The start index of the visible rows")
|
|
|
|
(bottom-rows-slice
|
|
|
|
:initform 0
|
|
|
|
:initarg :bottom-rows-slice
|
|
|
|
:accessor bottom-rows-slice
|
|
|
|
:documentation "The start index of the visible rows"))
|
2020-05-08 15:45:43 +02:00
|
|
|
(:documentation "A widget that holds a selectable list of lines"))
|
|
|
|
|
|
|
|
(defmethod initialize-instance :after ((object row-oriented-widget) &key &allow-other-keys)
|
|
|
|
(with-accessors ((top-row-padding top-row-padding)
|
|
|
|
(y-current-row y-current-row)) object
|
|
|
|
(setf y-current-row top-row-padding)))
|
|
|
|
|
|
|
|
(defmethod (setf top-row-padding) ((object row-oriented-widget) new-padding)
|
|
|
|
(setf (slot-value object 'top-row-padding) new-padding)
|
|
|
|
(setf (slot-value object 'y-current-row) new-padding)
|
|
|
|
object)
|
|
|
|
|
|
|
|
(defgeneric renderizable-rows-data (object))
|
|
|
|
|
|
|
|
(defgeneric unselect-all (object))
|
|
|
|
|
|
|
|
(defgeneric select-row (object index))
|
|
|
|
|
2021-05-02 16:28:50 +02:00
|
|
|
(defgeneric select-first-row (object))
|
|
|
|
|
2021-10-08 11:55:16 +02:00
|
|
|
(defgeneric adjust-selected-rows (object strategy))
|
|
|
|
|
2020-05-08 15:45:43 +02:00
|
|
|
(defgeneric selected-row (object))
|
|
|
|
|
|
|
|
(defgeneric selected-row-fields (object))
|
|
|
|
|
|
|
|
(defgeneric selected-row-delete (object))
|
|
|
|
|
2020-10-03 16:58:02 +02:00
|
|
|
(defgeneric search-row (object regex &key redraw))
|
|
|
|
|
2020-05-08 15:45:43 +02:00
|
|
|
(defgeneric row-move (object amount)
|
|
|
|
(:documentation "Move selected line of 'amount'. 'Amount' can be
|
|
|
|
an integer number, if positive increase the position of the selected
|
|
|
|
line otherwise decrease, relative to current index position.
|
|
|
|
The value is clamped at range [ 0, '(length (rows object)))' ).
|
|
|
|
This function return the number of positions acually moved"))
|
|
|
|
|
2021-04-11 10:26:48 +02:00
|
|
|
(defgeneric update-all-rows (object new-rows))
|
|
|
|
|
|
|
|
(defgeneric append-new-rows (object new-rows))
|
|
|
|
|
|
|
|
(defgeneric map-rows (object function &key &allow-other-keys))
|
|
|
|
|
2021-04-11 15:19:45 +02:00
|
|
|
(defgeneric rows-map-raw (object function &key &allow-other-keys))
|
|
|
|
|
2021-04-11 10:26:48 +02:00
|
|
|
(defgeneric rows-length (object &key &allow-other-keys))
|
|
|
|
|
|
|
|
(defgeneric rows-empty-p (object &key &allow-other-keys))
|
|
|
|
|
|
|
|
(defgeneric rows-remove-if (object function &key &allow-other-keys))
|
|
|
|
|
2022-07-01 16:31:38 +02:00
|
|
|
(defgeneric rows-find-if (object function &key from-end start end key &allow-other-keys))
|
2021-09-10 17:34:03 +02:00
|
|
|
|
2021-04-11 10:26:48 +02:00
|
|
|
(defgeneric rows-safe-subseq (object start &key end &allow-other-keys))
|
|
|
|
|
|
|
|
(defgeneric rows-elt (object index &key &allow-other-keys))
|
|
|
|
|
|
|
|
(defgeneric rows-last-elt (object &key &allow-other-keys))
|
|
|
|
|
|
|
|
(defgeneric rows-first-elt (object &key &allow-other-keys))
|
|
|
|
|
|
|
|
(defgeneric rows-position-if (object predicate &key from-end start end key &allow-other-keys))
|
|
|
|
|
2020-05-08 15:45:43 +02:00
|
|
|
(defmethod renderizable-rows-data ((object row-oriented-widget))
|
|
|
|
"Cut from all the lines a slice that that fits into the widget"
|
|
|
|
(with-accessors ((top-row-padding top-row-padding)
|
|
|
|
(current-row-index current-row-index)
|
|
|
|
(row-selected-index row-selected-index)
|
2020-06-22 13:58:04 +02:00
|
|
|
(single-row-height single-row-height)
|
|
|
|
(top-rows-slice top-rows-slice)
|
|
|
|
(bottom-rows-slice bottom-rows-slice)
|
2020-05-08 15:45:43 +02:00
|
|
|
(rows rows)) object
|
|
|
|
(let* ((window-height (if (uses-border-p object)
|
|
|
|
(win-height-no-border object)
|
|
|
|
(win-height object)))
|
2020-06-22 13:58:04 +02:00
|
|
|
(available-rows (truncate (/ (- window-height top-row-padding)
|
|
|
|
single-row-height)))
|
2020-05-08 15:45:43 +02:00
|
|
|
(selected-top-offset (rem row-selected-index available-rows))
|
|
|
|
(top (- row-selected-index selected-top-offset))
|
|
|
|
(bottom (+ row-selected-index
|
|
|
|
(- available-rows selected-top-offset))))
|
2020-06-22 13:58:04 +02:00
|
|
|
(setf top-rows-slice top
|
|
|
|
bottom-rows-slice bottom)
|
2020-05-08 15:45:43 +02:00
|
|
|
(values (safe-subseq rows top bottom)
|
|
|
|
selected-top-offset))))
|
|
|
|
|
|
|
|
(defmethod unselect-all ((object row-oriented-widget))
|
|
|
|
(loop for row in (rows object) do
|
|
|
|
(setf (selected row) nil))
|
|
|
|
object)
|
|
|
|
|
|
|
|
(defmethod select-row ((object row-oriented-widget) (index number))
|
|
|
|
(with-accessors ((rows rows)
|
|
|
|
(row-selected-index row-selected-index)) object
|
|
|
|
(restart-case
|
|
|
|
(if (or (< index 0)
|
|
|
|
(>= index (length rows)))
|
|
|
|
(error 'conditions:out-of-bounds :idx index :seq rows)
|
|
|
|
(progn
|
|
|
|
(setf row-selected-index index)
|
|
|
|
(setf (selected (elt rows index)) t)))
|
|
|
|
(ignore-selecting-action (e)
|
|
|
|
(declare (ignore e))
|
2021-01-10 11:35:28 +01:00
|
|
|
nil)
|
|
|
|
(set-default-index (e)
|
|
|
|
(declare (ignore e))
|
|
|
|
(setf row-selected-index 0)))
|
|
|
|
object))
|
2020-05-08 15:45:43 +02:00
|
|
|
|
2021-05-02 16:28:50 +02:00
|
|
|
(defmethod select-first-row ((object row-oriented-widget))
|
|
|
|
(select-row object 0))
|
|
|
|
|
|
|
|
(defun adjust-rows-select-first (window)
|
2021-07-31 11:42:12 +02:00
|
|
|
(when (rows window)
|
|
|
|
(select-first-row window)))
|
2021-05-02 16:28:50 +02:00
|
|
|
|
2021-10-08 11:37:53 +02:00
|
|
|
(defun adjust-rows-noop (window)
|
|
|
|
window)
|
|
|
|
|
2021-07-26 19:03:10 +02:00
|
|
|
(defun adjust-rows-select-last (window)
|
|
|
|
(with-accessors ((rows rows)) window
|
|
|
|
(when rows
|
|
|
|
(let ((height (win-height-no-border window)))
|
2021-10-08 12:11:00 +02:00
|
|
|
(if (>= (rows-length window) height)
|
|
|
|
(select-row window (- (rows-length window)
|
|
|
|
height))
|
|
|
|
(select-first-row window)))))
|
2021-07-26 19:03:10 +02:00
|
|
|
window)
|
|
|
|
|
2021-10-08 11:55:16 +02:00
|
|
|
(defmethod adjust-selected-rows ((object row-oriented-widget) (strategy function))
|
|
|
|
(funcall strategy object)
|
|
|
|
object)
|
|
|
|
|
2020-05-08 15:45:43 +02:00
|
|
|
(defmethod selected-row ((object row-oriented-widget))
|
|
|
|
"Return the current selected row"
|
|
|
|
(with-accessors ((rows rows)
|
|
|
|
(row-selected-index row-selected-index)) object
|
|
|
|
(when rows
|
|
|
|
(if (<= 0
|
|
|
|
row-selected-index
|
|
|
|
(1- (length rows)))
|
|
|
|
(elt rows row-selected-index)
|
|
|
|
nil))))
|
|
|
|
|
|
|
|
(defmethod selected-row-fields ((object row-oriented-widget))
|
|
|
|
"Return the fields current selected row"
|
|
|
|
(when-let ((selected-row (selected-row object)))
|
|
|
|
(fields selected-row)))
|
|
|
|
|
|
|
|
(defmethod selected-row-delete ((object row-oriented-widget))
|
|
|
|
"delete the selected row"
|
|
|
|
(with-accessors ((rows rows)
|
|
|
|
(row-selected-index row-selected-index)) object
|
|
|
|
(let ((last-was-removed-p (= row-selected-index
|
|
|
|
(1- (length rows)))))
|
|
|
|
(setf rows (remove-if #'selectedp rows))
|
2021-06-12 21:15:50 +02:00
|
|
|
(if rows
|
|
|
|
(if last-was-removed-p
|
|
|
|
(select-row object (1- row-selected-index))
|
|
|
|
(select-row object row-selected-index))
|
|
|
|
(setf row-selected-index 0)))
|
|
|
|
object))
|
2020-05-08 15:45:43 +02:00
|
|
|
|
|
|
|
(defmethod row-move ((object row-oriented-widget) amount)
|
|
|
|
"Navigate the lines, move the selected row by `amount', returns the
|
|
|
|
actual of rows moved. This can be different from `amount' if moving
|
2021-04-08 15:13:31 +02:00
|
|
|
this exact quantity would go beyond the length or rows or zero."
|
2020-05-08 15:45:43 +02:00
|
|
|
(with-accessors ((rows rows)
|
|
|
|
(row-selected-index row-selected-index)) object
|
2020-09-10 17:50:22 +02:00
|
|
|
(if (and rows
|
|
|
|
row-selected-index
|
|
|
|
(/= 0 amount))
|
2020-09-06 11:18:49 +02:00
|
|
|
(let* ((desired-amount (+ amount row-selected-index))
|
|
|
|
(actual-amount (if (< amount 0)
|
|
|
|
(max (- desired-amount
|
|
|
|
row-selected-index)
|
|
|
|
(- row-selected-index))
|
|
|
|
(- (min desired-amount
|
|
|
|
(1- (length rows)))
|
|
|
|
row-selected-index))))
|
|
|
|
(select-row object (+ row-selected-index
|
|
|
|
actual-amount))
|
|
|
|
actual-amount)
|
|
|
|
0)))
|
2020-05-08 15:45:43 +02:00
|
|
|
|
2020-10-03 16:58:02 +02:00
|
|
|
(defmethod search-row ((object row-oriented-widget) regex &key (redraw t))
|
|
|
|
(handler-case
|
|
|
|
(with-accessors ((row-selected-index row-selected-index)) object
|
2021-04-06 19:59:58 +02:00
|
|
|
(let* ((scanner (create-scanner regex :case-insensitive-mode t))
|
2021-06-16 19:08:41 +02:00
|
|
|
(selected-row (selected-row object))
|
|
|
|
(selected-text (normal-text selected-row))
|
|
|
|
(actual-row-starting (if (scan scanner selected-text)
|
|
|
|
(1+ row-selected-index)
|
|
|
|
row-selected-index))
|
2021-04-06 19:59:58 +02:00
|
|
|
(position-found (position-if (lambda (a)
|
|
|
|
(if (selectedp a)
|
|
|
|
(scan scanner (selected-text a))
|
|
|
|
(scan scanner (normal-text a))))
|
|
|
|
(safe-subseq (rows object)
|
2021-06-16 19:08:41 +02:00
|
|
|
actual-row-starting))))
|
2021-05-13 15:32:31 +02:00
|
|
|
(when position-found
|
|
|
|
(unselect-all object)
|
2021-06-16 19:08:41 +02:00
|
|
|
(select-row object (+ actual-row-starting position-found))
|
2021-05-13 15:32:31 +02:00
|
|
|
(when redraw
|
|
|
|
(draw object))
|
|
|
|
position-found)))
|
2020-10-03 16:58:02 +02:00
|
|
|
(error ()
|
|
|
|
(ui:error-message (_ "Invalid regular expression")))))
|
|
|
|
|
2021-04-08 15:13:31 +02:00
|
|
|
(defmethod update-all-rows ((object row-oriented-widget) (new-rows sequence))
|
|
|
|
(setf (rows object) new-rows))
|
|
|
|
|
|
|
|
(defmethod append-new-rows ((object row-oriented-widget) (new-rows sequence))
|
2021-05-16 15:20:17 +02:00
|
|
|
(with-slots (rows) object
|
2021-04-08 15:13:31 +02:00
|
|
|
(let ((reversed-old-rows (reverse rows)))
|
|
|
|
(loop for new-row in new-rows do
|
|
|
|
(push new-row reversed-old-rows))
|
|
|
|
(setf rows (reverse reversed-old-rows)))))
|
|
|
|
|
2021-06-05 21:47:37 +02:00
|
|
|
(defmethod append-new-rows ((object row-oriented-widget) (new-rows line))
|
|
|
|
(append-new-rows object (list new-rows)))
|
|
|
|
|
2021-04-08 15:13:31 +02:00
|
|
|
(defmethod map-rows ((object row-oriented-widget) (function function)
|
|
|
|
&key &allow-other-keys)
|
|
|
|
(mapcar function (rows object)))
|
|
|
|
|
2021-04-11 15:19:45 +02:00
|
|
|
(defmethod rows-map-raw ((object row-oriented-widget) (function function)
|
|
|
|
&key &allow-other-keys)
|
|
|
|
(mapcar function (slot-value object 'rows)))
|
|
|
|
|
2021-12-10 11:50:37 +01:00
|
|
|
(defmacro do-rows ((object row) &body body)
|
|
|
|
`(map nil (lambda (,row) (progn ,@body)) (rows ,object)))
|
|
|
|
|
|
|
|
(defmacro do-rows-raw ((object row) &body body)
|
|
|
|
`(map nil (lambda (,row) (progn ,@body)) (slot-value ,object 'rows)))
|
|
|
|
|
|
|
|
(defmacro loop-rows ((object row &rest loop-clauses) &body body)
|
|
|
|
`(loop for ,row in (rows ,object)
|
|
|
|
,@loop-clauses
|
|
|
|
,@body))
|
|
|
|
|
2021-04-08 15:13:31 +02:00
|
|
|
(defmethod rows-length ((object row-oriented-widget) &key &allow-other-keys)
|
|
|
|
(length (rows object)))
|
|
|
|
|
|
|
|
(defmethod rows-empty-p ((object row-oriented-widget) &key &allow-other-keys)
|
|
|
|
(not (rows object)))
|
|
|
|
|
|
|
|
(defmethod rows-remove-if ((object row-oriented-widget) (function function) &key &allow-other-keys)
|
|
|
|
(remove-if function (rows object)))
|
|
|
|
|
2022-07-01 16:31:38 +02:00
|
|
|
(defmethod rows-find-if ((object row-oriented-widget) (predicate function)
|
|
|
|
&key from-end (start 0) end key &allow-other-keys)
|
|
|
|
(find-if predicate (rows object)
|
|
|
|
:from-end from-end
|
|
|
|
:start start
|
|
|
|
:end end
|
|
|
|
:key key))
|
2021-09-10 17:34:03 +02:00
|
|
|
|
2021-04-08 15:13:31 +02:00
|
|
|
(defmethod rows-safe-subseq ((object row-oriented-widget) start
|
|
|
|
&key (end nil) &allow-other-keys)
|
|
|
|
(safe-subseq (rows object) start end))
|
|
|
|
|
|
|
|
(defmethod rows-elt ((object row-oriented-widget) index &key &allow-other-keys)
|
|
|
|
(elt (rows object) index))
|
|
|
|
|
|
|
|
(defmethod rows-last-elt ((object row-oriented-widget) &key &allow-other-keys)
|
|
|
|
(last-elt (rows object)))
|
|
|
|
|
|
|
|
(defmethod rows-first-elt ((object row-oriented-widget) &key &allow-other-keys)
|
|
|
|
(first-elt (rows object)))
|
|
|
|
|
|
|
|
(defmethod rows-position-if ((object row-oriented-widget) (predicate function)
|
2021-11-26 14:10:11 +01:00
|
|
|
&key from-end (start 0) end key &allow-other-keys)
|
2021-04-08 15:13:31 +02:00
|
|
|
(position-if predicate
|
|
|
|
(rows object)
|
|
|
|
:from-end from-end
|
|
|
|
:start start
|
|
|
|
:end end
|
|
|
|
:key key))
|
|
|
|
|
2021-04-10 13:52:56 +02:00
|
|
|
(defun rows->text-rows (window &key (accessor-fn #'normal-text))
|
|
|
|
(let ((*blanks* '(#\Newline)))
|
|
|
|
(map-rows window
|
|
|
|
(lambda (a)
|
|
|
|
(trim-blanks (tui:tui-string->chars-string (funcall accessor-fn a)))))))
|
|
|
|
|
|
|
|
(defun rows->text (window &key (accessor-fn #'normal-text))
|
|
|
|
(join-with-strings (rows->text-rows window :accessor-fn accessor-fn)
|
|
|
|
(format nil "~%")))
|
|
|
|
|
2020-05-08 15:45:43 +02:00
|
|
|
(defclass simple-line-navigation-window (wrapper-window row-oriented-widget border-window)
|
|
|
|
((selected-line-bg
|
2020-05-17 17:47:33 +02:00
|
|
|
:initform :blue
|
2020-05-08 15:45:43 +02:00
|
|
|
:initarg :selected-line-bg
|
|
|
|
:accessor selected-line-bg
|
|
|
|
:documentation "The background color for a selected line")
|
|
|
|
(selected-line-fg
|
2020-05-17 17:47:33 +02:00
|
|
|
:initform :red
|
2020-05-08 15:45:43 +02:00
|
|
|
:initarg :selected-line-fg
|
|
|
|
:accessor selected-line-fg
|
2020-08-28 17:52:00 +02:00
|
|
|
:documentation "The foreground color for a selected line")
|
2021-08-28 14:02:03 +02:00
|
|
|
(unselected-line-bg
|
|
|
|
:initform :blue
|
|
|
|
:initarg :unselected-line-bg
|
|
|
|
:accessor unselected-line-bg
|
|
|
|
:documentation "The background color for a unselected line")
|
|
|
|
(unselected-line-fg
|
|
|
|
:initform :red
|
|
|
|
:initarg :unselected-line-fg
|
|
|
|
:accessor unselected-line-fg
|
|
|
|
:documentation "The foreground color for a unselected line")
|
2020-08-28 17:52:00 +02:00
|
|
|
(top-horizontal-padding
|
|
|
|
:initform 0
|
|
|
|
:initarg :top-horizontal-padding
|
|
|
|
:accessor top-horizontal-padding
|
|
|
|
:documentation "The vertical padding (from top) of each single row"))
|
2020-05-08 15:45:43 +02:00
|
|
|
(:documentation "A window that displays a navigable list of objects"))
|
|
|
|
|
|
|
|
(defmethod draw :after ((object simple-line-navigation-window))
|
2020-08-28 17:52:00 +02:00
|
|
|
(with-accessors ((uses-border-p uses-border-p)
|
|
|
|
(single-row-height single-row-height)
|
|
|
|
(top-row-padding top-row-padding)
|
|
|
|
(top-horizontal-padding top-horizontal-padding)) object
|
2021-08-13 12:14:58 +02:00
|
|
|
(when-window-shown (object)
|
|
|
|
(let ((max-line-size (if uses-border-p
|
|
|
|
(win-width-no-border object)
|
|
|
|
(win-width object))))
|
|
|
|
(let ((rows (renderizable-rows-data object))
|
|
|
|
(x (if (uses-border-p object)
|
|
|
|
1
|
|
|
|
0))
|
|
|
|
(y-start (if (uses-border-p object)
|
|
|
|
1
|
|
|
|
0)))
|
|
|
|
(loop
|
|
|
|
for y from (+ y-start
|
|
|
|
top-horizontal-padding
|
|
|
|
top-row-padding)
|
|
|
|
by single-row-height
|
|
|
|
for ct from 0
|
|
|
|
for row in rows do
|
|
|
|
(if (selectedp row)
|
2021-08-29 15:39:48 +02:00
|
|
|
(let ((tui-text (to-tui-string (selected-text row))))
|
|
|
|
(print-text object
|
|
|
|
(tui-string-apply-colors (right-pad-text (text-ellipsis tui-text
|
|
|
|
max-line-size)
|
|
|
|
max-line-size)
|
|
|
|
(selected-fg row)
|
|
|
|
(selected-bg row))
|
|
|
|
|
|
|
|
x y))
|
2021-08-13 12:14:58 +02:00
|
|
|
(print-text object
|
|
|
|
(right-pad-text (text-ellipsis (normal-text row)
|
|
|
|
max-line-size)
|
|
|
|
max-line-size)
|
|
|
|
x y
|
|
|
|
:bgcolor (normal-bg row)
|
2022-04-16 11:09:16 +02:00
|
|
|
:fgcolor (normal-fg row))))
|
2022-04-16 17:54:57 +02:00
|
|
|
(when (> (rows-length object) 0)
|
|
|
|
(let* ((current-selected (1+ (row-selected-index object)))
|
|
|
|
(pages-count-line (format nil
|
|
|
|
(_ "line ~a of ~a")
|
|
|
|
current-selected
|
|
|
|
(rows-length object)))
|
|
|
|
(x-count-line (- (win-width object)
|
|
|
|
(length pages-count-line)
|
|
|
|
1))
|
|
|
|
(y-count-line (1- (win-height object))))
|
|
|
|
(print-text object
|
|
|
|
(text-ellipsis pages-count-line (win-width-no-border object))
|
|
|
|
x-count-line
|
|
|
|
y-count-line))))))))
|
2020-05-08 15:45:43 +02:00
|
|
|
|
|
|
|
(defgeneric resync-rows-db (object &key redraw)
|
|
|
|
(:documentation "Synchronize information table slot of `object` with
|
|
|
|
table in the database, if `redraw` is not nil redraw the object, if
|
|
|
|
possible"))
|
|
|
|
|
2022-11-24 17:28:31 +01:00
|
|
|
(defmethod resync-rows-db ((object null) &key redraw)
|
|
|
|
(declare (ignore redraw))
|
|
|
|
t)
|
|
|
|
|
2020-05-08 15:45:43 +02:00
|
|
|
(defun make-blocking-list-dialog-window (screen all-fields text-lines callback
|
|
|
|
&optional (title (_ "Information")))
|
|
|
|
"Draw a window with a scrollable list of entries, pressing enter
|
|
|
|
will fire the `callback' function (with the selected field from `all-fields'
|
|
|
|
and text from `text-line'. This window is fitten into `screen' sizes."
|
2020-10-03 21:08:55 +02:00
|
|
|
(assert (>= (length all-fields)
|
|
|
|
(length text-lines)))
|
2020-05-08 15:45:43 +02:00
|
|
|
(let* ((low-level-window (make-blocking-croatoan-window :enable-function-keys t))
|
|
|
|
(window-width (max (+ 4
|
|
|
|
(length title))
|
|
|
|
(+ (find-max-line-width text-lines)
|
|
|
|
2)))
|
|
|
|
(window-height (min (truncate (* 0.9 (win-height screen)))
|
2022-04-15 13:40:07 +02:00
|
|
|
(+ (max (length text-lines)
|
|
|
|
+min-shown-win-height+)
|
|
|
|
2)))
|
2020-05-08 15:45:43 +02:00
|
|
|
(window-x (truncate (- (* 0.5 (win-width screen))
|
|
|
|
(* 0.5 window-width))))
|
|
|
|
(window-y (truncate (- (* 0.5 (win-height screen))
|
|
|
|
(* 0.5 window-height))))
|
|
|
|
(bg (swconf:win-bg swconf:+key-info-dialog+))
|
|
|
|
(fg (swconf:win-fg swconf:+key-info-dialog+))
|
|
|
|
(high-level-window (make-instance 'simple-line-navigation-window
|
|
|
|
:single-row-height 1
|
|
|
|
:uses-border-p t
|
|
|
|
:croatoan-window low-level-window)))
|
|
|
|
(flet ((draw ()
|
2022-04-16 11:09:16 +02:00
|
|
|
(win-clear high-level-window :redraw nil)
|
|
|
|
(draw high-level-window)
|
|
|
|
(print-text high-level-window title 2 0)))
|
2022-03-21 21:42:50 +01:00
|
|
|
(setf (c:background low-level-window) (tui:make-win-background bg))
|
|
|
|
(setf (c:fgcolor low-level-window) fg)
|
2020-05-08 15:45:43 +02:00
|
|
|
(win-resize high-level-window window-width window-height)
|
|
|
|
(win-move high-level-window window-x window-y)
|
|
|
|
(setf (rows high-level-window)
|
|
|
|
(loop
|
|
|
|
for text in text-lines
|
|
|
|
for fields in all-fields
|
|
|
|
collect
|
|
|
|
(make-instance 'line
|
|
|
|
:fields fields
|
|
|
|
:normal-text text
|
|
|
|
:selected-text text
|
|
|
|
:normal-bg bg
|
|
|
|
:normal-fg fg
|
|
|
|
:selected-bg fg
|
|
|
|
:selected-fg bg)))
|
|
|
|
(select-row high-level-window 0)
|
|
|
|
(draw)
|
|
|
|
(loop named inner
|
2022-03-21 21:42:50 +01:00
|
|
|
for c = (tui:decode-key-event (c:get-wide-event low-level-window))
|
2020-05-08 15:45:43 +02:00
|
|
|
while (string/= c "q")
|
|
|
|
do
|
|
|
|
(cond
|
|
|
|
((string= c :up)
|
|
|
|
(unselect-all high-level-window)
|
|
|
|
(row-move high-level-window -1))
|
|
|
|
((string= c :down)
|
|
|
|
(unselect-all high-level-window)
|
|
|
|
(row-move high-level-window 1))
|
2022-01-02 17:47:30 +01:00
|
|
|
((string= c "^J")
|
2020-05-08 15:45:43 +02:00
|
|
|
(let ((selected-fields (selected-row-fields high-level-window))
|
|
|
|
(selected-text (selected-text (selected-row high-level-window))))
|
2022-02-18 21:28:48 +01:00
|
|
|
(when callback
|
|
|
|
(funcall callback selected-text selected-fields)))))
|
2020-05-08 15:45:43 +02:00
|
|
|
(draw))
|
|
|
|
(win-close high-level-window))))
|