Split out utils so that we don't need SRFI.

This commit is contained in:
Dominik Pantůček 2023-03-14 23:13:26 +01:00
parent 98a772cee9
commit 5b32f3a30a
3 changed files with 66 additions and 18 deletions

View file

@ -25,7 +25,7 @@
brmsaptool: brmsaptool.o testing.o listing.o month.o period.o ansi.o \ brmsaptool: brmsaptool.o testing.o listing.o month.o period.o ansi.o \
member-file.o dictionary.o command-line.o \ member-file.o dictionary.o command-line.o \
members-base.o members-base.o utils.o
csc -o brmsaptool $^ csc -o brmsaptool $^
.PHONY: clean .PHONY: clean
@ -47,7 +47,8 @@ clean:
brmsaptool.o: brmsaptool.scm testing.import.scm listing.import.scm \ brmsaptool.o: brmsaptool.scm testing.import.scm listing.import.scm \
dictionary.import.scm month.import.scm period.import.scm \ dictionary.import.scm month.import.scm period.import.scm \
ansi.import.scm member-file.import.scm \ ansi.import.scm member-file.import.scm \
command-line.import.scm members-base.import.scm command-line.import.scm members-base.import.scm \
utils.import.scm
TESTING-SOURCES=testing.scm TESTING-SOURCES=testing.scm
@ -74,7 +75,7 @@ PERIOD-SOURCES=period.scm testing.import.scm month.import.scm
period.o: $(PERIOD-SOURCES) period.o: $(PERIOD-SOURCES)
period.import.scm: $(PERIOD-SOURCES) period.import.scm: $(PERIOD-SOURCES)
ANSI-SOURCES=ansi.scm testing.import.scm ANSI-SOURCES=ansi.scm testing.import.scm utils.import.scm
ansi.o: $(ANSI-SOURCES) ansi.o: $(ANSI-SOURCES)
ansi.import.scm: $(ANSI-SOURCES) ansi.import.scm: $(ANSI-SOURCES)
@ -95,3 +96,8 @@ MEMBERS-BASE-SOURCES=members-base.scm testing.import.scm
members-base.o: $(MEMBERS-BASE-SOURCES) members-base.o: $(MEMBERS-BASE-SOURCES)
members-base.import.scm: $(MEMBERS-BASE-SOURCES) members-base.import.scm: $(MEMBERS-BASE-SOURCES)
UTILS-SOURCES=utils.scm testing.import.scm
utils.o: $(UTILS-SOURCES)
utils.import.scm: $(UTILS-SOURCES)

View file

@ -35,7 +35,8 @@
(import scheme (import scheme
(chicken base) (chicken base)
(chicken string) (chicken string)
testing) testing
utils)
;; Only basic ANSI colors and bold attribute support. ;; Only basic ANSI colors and bold attribute support.
(define colors (define colors
@ -50,18 +51,6 @@
(#:default . 0) (#:default . 0)
(#:bold . 1))) (#:bold . 1)))
;; Returns a list with elements matching pred? predicate.
(define (filter pred? lst)
(let loop ((lst lst)
(res '()))
(if (null? lst)
(reverse res)
(if (pred? (car lst))
(loop (cdr lst)
(cons (car lst) res))
(loop (cdr lst)
res)))))
;; Returns ANSI sequence changing color and/or bold attribute. ;; Returns ANSI sequence changing color and/or bold attribute.
(define (ansi . args) (define (ansi . args)
(let ((argsl (let ((argsl
@ -81,8 +70,6 @@
(define (ansi-tests!) (define (ansi-tests!)
(run-tests (run-tests
ansi ansi
(test-equal? filter (filter odd? '(1 2 3 4)) '(1 3))
(test-equal? filter (filter odd? '(2 4)) '())
(test-equal? ansi (ansi #:red) "\x1b[31m") (test-equal? ansi (ansi #:red) "\x1b[31m")
(test-equal? ansi (ansi #:nonsense) "") (test-equal? ansi (ansi #:nonsense) "")
(test-equal? ansi (ansi #:default) "\x1b[0m") (test-equal? ansi (ansi #:default) "\x1b[0m")

55
utils.scm Normal file
View file

@ -0,0 +1,55 @@
;;
;; utils.scm
;;
;; Various utilities so that no external libraries are needed.
;;
;; ISC License
;;
;; Copyright 2023 Brmlab, z.s.
;; Dominik Pantůček <dominik.pantucek@trustica.cz>
;;
;; Permission to use, copy, modify, and/or distribute this software
;; for any purpose with or without fee is hereby granted, provided
;; that the above copyright notice and this permission notice appear
;; in all copies.
;;
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
;; WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
;; WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
;; AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
;; CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
;; OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
;; NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
;; CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;;
(declare (unit utils))
(module
utils
(
filter
utils-tests!
)
;; Returns a list with elements matching pred? predicate.
(define (filter pred? lst)
(let loop ((lst lst)
(res '()))
(if (null? lst)
(reverse res)
(if (pred? (car lst))
(loop (cdr lst)
(cons (car lst) res))
(loop (cdr lst)
res)))))
;; Performs utils module self-tests.
(define (utils-tests!)
(run-tests
utils
(test-equal? filter (filter odd? '(1 2 3 4)) '(1 3))
(test-equal? filter (filter odd? '(2 4)) '())
))
)