Untitled

 avatar
unknown
scheme
a year ago
3.2 kB
7
Indexable
#lang racket

(define/contract (remove-strint-padding s)
  [-> string? string?]
  (if [eqv? (string-ref s 0) #\0]
      (string-replace s "0" "" #:all? #f)
      s))

(define/contract (format-time t)
  [-> string? string?]
  (define t_ (map remove-strint-padding (string-split t ":")))
  (string-append (first  t_) "h "
                 (second t_) "m "
                 (third  t_) "s"))

(define/contract (run-command* cmd [arg null])
  [->* [string?] [string?] string?]
  (with-output-to-string
   (λ () (if (null? arg)
             (system* (find-executable-path cmd))
             (system* (find-executable-path cmd) arg)))))

(define/contract (get-os) 
  [-> string?]
  (last (string-split (last (filter (λ (line) (string-prefix? line "PRETTY_NAME="))
                                    (file->lines "/etc/os-release"))) "=")))

(define/contract (get-kernel)
  [-> string?]
  (string-trim (run-command* "uname" "-r")))

(define/contract (get-host) ; this uses pfetch's method
  [-> string?]
  (define name "/sys/devices/virtual/dmi/id/product_name")
  (define ver "/sys/devices/virtual/dmi/id/product_version")
  (define full-name (list name ver))
  (string-trim (apply string-append (map file->string full-name))))

(define/contract (get-username)
  [-> string?]
  (string-trim (run-command* "whoami")))

(define/contract (get-hostname)
  [-> string?]
  (string-trim (run-command* "hostname")))

(define/contract (get-uptime)
  [-> string?]
  (first (string-split (run-command* "w") " ")))

(define/contract (get-memstr-mib)
  [-> string?]
  (define free 
   (string-split (string-normalize-spaces (run-command* "free" "--mebi")) " "))
  (string-append (ninth free) "M"
                 " / "
                 (eighth free) "M"))

(define/contract (get-shell)
  [-> string?]
  (define file-lines (file->lines "/etc/passwd"))
  (define user (get-username)) ; to speed up the code
  (define (user-line? l) (string-contains? l user))
  (define user-line (first (filter user-line? file-lines)))
  (define shell (last (string-split user-line ":")))
  shell)

(define red     "\033[0;31m")
(define green   "\033[0;32m")
(define yellow  "\033[0;33m")
(define blue    "\033[0;34m")
(define magenta "\033[0;35m")
(define cyan    "\033[0;36m")
(define clear   "\033[0m")

; lines of ascii art with right and left padding
(define l1 (~a red "    ___     "))
(define l2 (~a red"   (" clear ".." red " |    "))
(define l3 (~a red "   (" yellow "<>" red " |    "))
(define l4 (~a red "  / " clear "__" red "  \\   "))
(define l5 (~a red " ( " clear "/  \\" red " /|  "))
(define l6 (~a yellow "_" red "/\\" clear " __)" red "/" yellow " _" red ") "))
(define l7 (~a yellow "\\/" red "-____" yellow "\\/   " clear))

(printf
"~a  ~a~a~a@~a~a~a
~a  ~aos    ~a ~a 
~a  ~ahost  ~a ~a 
~a  ~akernel~a ~a 
~a  ~auptime~a ~a 
~a  ~ashell ~a ~a 
~a  ~amemory~a ~a\n"
l1 yellow (get-username) clear yellow (get-hostname) clear
l2 red clear (string-replace (get-os) "\"" "")
l3 red clear (get-host)
l4 red clear (get-kernel)
l5 red clear (format-time (get-uptime))
l6 red clear (get-shell)
l7 red clear (get-memstr-mib))
Editor is loading...
Leave a Comment