Created
August 1, 2020 16:22
-
-
Save bennn/52713a3c002eba9928f3b7ec256f1dc7 to your computer and use it in GitHub Desktop.
quickscript: count lines in a file, even if unsaved or with unmatched parentheses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
;; Count lines | |
;; Use the selection, if any, otherwise count lines across all definitions. | |
;; | |
;; Other ideas: | |
;; - if the file is read-able, use `syntax-sloc` | |
;; - find way to count types, contracts, etc. | |
(require quickscript | |
(only-in racket/class send)) | |
(define count-linebreaks | |
(let ((newline-char (integer->char 10))) | |
(lambda (str) | |
(+ 1 ;; for the final line, because it's hard to select the newline at the end of it | |
(for/sum ((c (in-string str)) | |
#:when (eq? c newline-char)) | |
1))))) | |
(define-script line-count | |
#:label "line-count" | |
#:help-string "Count lines in the current selection, or for all definitions." | |
#:output-to message-box | |
(λ (selection #:definitions def) | |
(define-values [kind num-lines] | |
(if (< 0 (string-length selection)) | |
(values "selection" (count-linebreaks selection)) | |
(values "definitions" (count-linebreaks (send def get-flattened-text))))) | |
(format "Lines in ~a : ~a" kind num-lines))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment