grokking simplicity and refactoring

This commit is contained in:
Geir Okkenhaug Jerstad 2025-06-16 13:43:21 +02:00
parent 89a7fe100d
commit fb4361d938
67 changed files with 6275 additions and 56 deletions

View file

@ -0,0 +1,38 @@
;; utils/logging/core.scm - Core logging functions
(define-module (utils logging core)
#:use-module (utils logging state)
#:use-module (utils logging output)
#:export (log-with-color
log-debug
log-info
log-warn
log-error
log-success))
;; Impure function: Core logging with color and level checking
(define (log-with-color level color prefix message . args)
"Log message with color if level is appropriate"
(when (should-log? level)
(log-to-port (current-error-port) level color prefix message args)))
;; Specific logging functions - each does one thing well
(define (log-debug message . args)
"Log debug message"
(apply log-with-color 'debug 'cyan "DEBUG" message args))
(define (log-info message . args)
"Log info message"
(apply log-with-color 'info 'blue "INFO " message args))
(define (log-warn message . args)
"Log warning message"
(apply log-with-color 'warn 'yellow "WARN " message args))
(define (log-error message . args)
"Log error message"
(apply log-with-color 'error 'red "ERROR" message args))
(define (log-success message . args)
"Log success message"
(apply log-with-color 'info 'green "SUCCESS" message args))

View file

@ -0,0 +1,42 @@
;; utils/logging/format.scm - Pure logging formatting functions
(define-module (utils logging format)
#:use-module (ice-9 format)
#:use-module (srfi srfi-19)
#:export (format-timestamp
format-log-message
get-color
color-codes))
;; Pure data: ANSI color codes
(define color-codes
'((reset . "\x1b[0m")
(bold . "\x1b[1m")
(red . "\x1b[31m")
(green . "\x1b[32m")
(yellow . "\x1b[33m")
(blue . "\x1b[34m")
(magenta . "\x1b[35m")
(cyan . "\x1b[36m")))
;; Pure function: Get color code by name
(define (get-color name)
"Pure function to get ANSI color code"
(assoc-ref color-codes name))
;; Pure function: Format timestamp
(define (format-timestamp)
"Pure function to format current timestamp"
(date->string (current-date) "~H:~M:~S"))
;; Pure function: Format complete log message
;; Input: level symbol, color symbol, prefix string, message string, args list
;; Output: formatted log message string
(define (format-log-message level color prefix message args)
"Pure function to format a complete log message"
(let ((timestamp (format-timestamp))
(formatted-msg (apply format #f message args))
(color-start (get-color color))
(color-end (get-color 'reset)))
(format #f "~a~a[lab]~a ~a ~a~%"
color-start prefix color-end timestamp formatted-msg)))

View file

@ -0,0 +1,30 @@
;; utils/logging/level.scm - Pure log level management
(define-module (utils logging level)
#:export (log-levels
should-log-pure
validate-log-level))
;; Pure data: Log levels with numeric values for comparison
(define log-levels
'((debug . 0)
(info . 1)
(warn . 2)
(error . 3)))
;; Pure function: Check if message should be logged at given levels
;; Input: current-level symbol, message-level symbol
;; Output: #t if should log, #f otherwise
(define (should-log-pure current-level message-level)
"Pure function to determine if message should be logged"
(let ((current-value (assoc-ref log-levels current-level))
(message-value (assoc-ref log-levels message-level)))
(and current-value message-value
(<= current-value message-value))))
;; Pure function: Validate log level
;; Input: level symbol
;; Output: #t if valid, #f otherwise
(define (validate-log-level level)
"Pure function to validate log level"
(assoc-ref log-levels level))

View file

@ -0,0 +1,23 @@
;; utils/logging/output.scm - Pure logging output functions
(define-module (utils logging output)
#:use-module (utils logging format)
#:use-module (utils logging level)
#:export (log-message-pure
log-to-port))
;; Pure function: Create log message without side effects
;; Input: level, color, prefix, message, args
;; Output: formatted log message string
(define (log-message-pure level color prefix message args)
"Pure function to create formatted log message"
(format-log-message level color prefix message args))
;; Impure function: Write log message to port
;; Input: port, level, color, prefix, message, args
;; Output: unspecified (side effect: writes to port)
(define (log-to-port port level color prefix message args)
"Write formatted log message to specified port"
(let ((formatted-message (log-message-pure level color prefix message args)))
(display formatted-message port)
(force-output port)))

View file

@ -0,0 +1,27 @@
;; utils/logging/spinner.scm - Spinner utility for long operations
(define-module (utils logging spinner)
#:use-module (utils logging core)
#:export (with-spinner))
;; Pure function: Calculate elapsed time
;; Input: start-time, end-time
;; Output: elapsed seconds
(define (calculate-elapsed start-time end-time)
"Pure function to calculate elapsed time"
(- end-time start-time))
;; Impure function: Execute operation with spinner logging
(define (with-spinner message thunk)
"Execute operation with progress logging"
(log-info "~a..." message)
(let ((start-time (current-time)))
(catch #t
(lambda ()
(let ((result (thunk)))
(let ((elapsed (calculate-elapsed start-time (current-time))))
(log-success "~a completed in ~as" message elapsed))
result))
(lambda (key . args)
(log-error "~a failed: ~a ~a" message key args)
(throw key args)))))

View file

@ -0,0 +1,27 @@
;; utils/logging/state.scm - Logging state management
(define-module (utils logging state)
#:use-module (utils logging level)
#:export (get-current-log-level
set-log-level!
should-log?))
;; Mutable state: Current log level
(define current-log-level 'info)
;; Impure function: Get current log level
(define (get-current-log-level)
"Get current log level"
current-log-level)
;; Impure function: Set log level with validation
(define (set-log-level! level)
"Set current log level (with validation)"
(if (validate-log-level level)
(set! current-log-level level)
(error "Invalid log level" level)))
;; Impure function: Check if message should be logged
(define (should-log? level)
"Check if message should be logged at current level"
(should-log-pure current-log-level level))