commander-s/scheme/select-line.scm

58 lines
2.0 KiB
Scheme
Raw Normal View History

(define-record-type select-line :select-line
(really-make-select-line elements cursor-index num-cols)
select-line?
(elements select-line-elements)
(cursor-index select-line-cursor-index set-select-line-cursor-index!)
(num-cols select-line-num-cols))
(define (make-select-line elements)
(really-make-select-line elements 0 (length elements)))
(define (select-line-key? key)
(or (= key key-right)
(= key key-left)))
(define (select-line-handle-key-press! select-line key)
(cond
((= key key-right)
(move-cursor-right! select-line))
((= key key-left)
(move-cursor-left! select-line))
(else #f)))
(define (move-cursor-left! select-line)
(let ((old-col (select-line-cursor-index select-line)))
(if (and (> old-col 0)
(> (select-line-num-cols select-line) 1))
(set-select-line-cursor-index! select-line (- old-col 1)))))
(define (move-cursor-right! select-line)
(let ((old-col (select-line-cursor-index select-line)))
(if (< old-col (- (select-line-num-cols select-line) 1))
(set-select-line-cursor-index! select-line (+ old-col 1)))))
(define (paint-select-line select-line win result-buffer)
(paint-select-line-at select-line 0 0 win result-buffer))
(define (paint-select-line-at select-line x y win result-buffer)
(let ((cursor-col (select-line-cursor-index select-line))
(width (result-buffer-num-cols result-buffer)))
(let lp ((elts (select-line-elements select-line))
(i 0)
(x x))
(cond ((null? elts)
(values))
((= i cursor-col)
(let ((new-x ((element-painter (car elts)) win x y width #t #f))) ; no marking for now
(lp (cdr elts) (+ i 1) new-x)))
(else
(let ((new-x ((element-painter (car elts)) win x y width #f #f)))
(lp (cdr elts) (+ i 1) new-x)))))))
(define (select-line-selected-entry select-line)
(element-value
(list-ref (select-line-elements select-line)
(select-line-cursor-index select-line))))