aboutsummaryrefslogtreecommitdiff
path: root/code.scm
blob: af6c7b8c85560ad9f8b9902fb99b835e172c7032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(define-module (code)
  #:export (extract sort* color-if
                    STR-YELLOW STR-RESET
                    print-vcomponent))

(use-modules (srfi srfi-19)
             (srfi srfi-19 util)
             (srfi srfi-26)
             (vcalendar)
             (util))

(define (extract field)
  (cut get-attr <> field))

;;; This function borrowed from web-ics (calendar util) 
(define* (sort* items comperator #:optional (get identity))
  "A sort function more in line with how python's sorted works"
  (sort items (lambda (a b)
                (comperator (get a)
                            (get b)))))

(define STR-YELLOW "\x1b[0;33m")
(define STR-RESET "\x1b[m")


(define-syntax-rule (color-if pred color body ...)
  (let ((pred-value pred))
    (format #f "~a~a~a"
            (if pred-value color "")
            (begin body ...)
            (if pred-value STR-RESET ""))))


(define* (print-vcomponent comp #:optional (depth 0))
  (let ((kvs (map (lambda (key) (cons key (get-attr comp key)))
                  (attributes comp))))
    (format #t "~a <~a> :: ~:a~%"
            (make-string depth #\:)
            (type comp) comp)
    (for-each-in kvs
                 (lambda (kv)
                   (let ((key (car kv))
                         (value (cdr kv)))
                     (format #t "~a ~20@a: ~a~%"
                             (make-string depth #\:)
                             key value))))
    (for-each-in (children comp)
                 (cut print-vcomponent <> (1+ depth)))))