(define (list-of-processes? thing) (and (proper-list? thing) (every process-info? thing))) (define (make-header-line) (make-select-line (list (make-unmarked-text-element 'pid #f (right-align-string 6 "PID ")) (make-unmarked-text-element 'ppid #f (right-align-string 6 "PPID ")) (make-unmarked-text-element 'user #f (left-align-string 9 "USER ")) (make-unmarked-text-element 'time #f (right-align-string 6 "TIME ")) (make-unmarked-text-element 'command #f "COMMAND")))) (define (layout-process width p) (cut-to-size width (string-append (right-align-string 5 (number->string (process-info-pid p))) " " (right-align-string 5 (number->string (process-info-ppid p))) " " (left-align-string 8 (process-info-logname p)) " " (right-align-string 5 (number->string (process-info-time p))) " " (left-align-string 100 (string-append (process-info-executable p) " " (string-join (process-info-command-line p))))))) (define (make-process-selection-list num-cols num-lines processes) (let ((layout (lambda (p) (layout-process num-cols p)))) (make-select-list (map (lambda (p) (make-unmarked-text-element p #t (layout-process num-cols p))) processes) num-lines))) (define-option 'ps 'kill-key (char->ascii #\k)) (define-option 'ps 'refresh-key (char->ascii #\g)) (define-option 'ps 'sort-up-key (char->ascii #\s)) (define-option 'ps 'sort-down-key (char->ascii #\S)) (define (make-pps-viewer processes buffer) (let* ((processes processes) (header-line (make-header-line)) (select-list (make-process-selection-list (result-buffer-num-cols buffer) (result-buffer-num-lines buffer) processes))) (define (set-processes! new-processes) (set! processes new-processes) (set! select-list (make-process-selection-list (result-buffer-num-cols buffer) (- (result-buffer-num-lines buffer) 1) new-processes))) (define (get-selection-as-text self for-scheme-mode? focus-object-table) (let* ((marked (select-list-get-marked select-list))) (cond ((null? marked) (number->string (process-info-pid (select-list-selected-entry select-list)))) (for-scheme-mode? (string-append "'" (write-to-string (map process-info-pid marked)))) (else (string-join (map number->string (map process-info-pid marked))))))) (lambda (message) (case message ((paint) (lambda (self win buffer have-focus?) (paint-select-line-at header-line 0 0 win buffer) (paint-selection-list-at select-list 0 1 win buffer have-focus?))) ((key-press) (lambda (self key control-x-pressed?) (cond ((or (= key (config 'ps 'sort-up-key)) (= key (config 'ps 'sort-down-key))) (let ((column (select-line-selected-entry header-line))) (receive (compare-up compare-down select) (case column ((pid) (values < > process-info-pid)) ((ppid) (values < > process-info-ppid)) ((user) (values string? process-info-logname)) ((time) (values < > process-info-time)) ((command) (values string? process-info-executable)) (else (error "unknown column" column))) (let ((compare (if (= key (config 'ps 'sort-up-key)) compare-up compare-down))) (set-processes! (list-sort (lambda (p1 p2) (compare (select p1) (select p2))) processes)) self)))) ((= key (config 'ps 'kill-key)) (let ((infos (select-list-get-selection select-list))) (for-each (cut signal-process <> signal/term) (map process-info-pid infos))) self) ((= key (config 'ps 'refresh-key)) (set-processes! (pps)) self) ((select-list-key? key) (set! select-list (select-list-handle-key-press select-list key)) self) ((select-line-key? key) (select-line-handle-key-press! header-line key) self) (else self)))) ((get-selection-as-text) get-selection-as-text) ((get-selection-as-ref) (make-get-selection-as-ref-method select-list)) (else (error "pps-viewer unknown message" message)))))) (register-plugin! (make-view-plugin make-pps-viewer list-of-processes?))