yatmsu
3/20/2013 - 7:12 AM

drawing in terminal

drawing in terminal

#!/usr/local/bin/gosh

(use gauche.parseopt)

(define-constant ANSI_ESCAPE_BG_COLOR_BASE 40)

(define (ansi-escape str color-num)
  (apply string-append
         (map (apply$ string)
              (list `(#\escape #\[ ,@(string->list (x->string color-num)) #\m)
                    (string->list str)
                    `(#\escape #\[ ,@'(#\0) #\m)))))

(define (draw ls)
  (for-each
   (^l (for-each
        (^x (if-let1 n (string->number x)
                     (display (ansi-escape " " (+ n ANSI_ESCAPE_BG_COLOR_BASE)))
                     (display " ")))
        l)
       (print))
   ls))

(define (usage cmd)
  (print "usage: " cmd " file1.dat file2.dat ... filen.dat")
  (print "exapmle: % echo \'0123 4567\' | " cmd)
  (print (draw '(("0" "1" "2" "3" " " "4" "5" "6" "7"))))
  (exit))

(define (input->list)
  (define (line->list val)
    (map string (string->list val)))
  (let rec ((acc '()))
    (let1 val (read-line)
      (if (eof-object? val)
          (reverse acc)
          (rec (cons (line->list val) acc))))))

(define (list->double ls)
  (fold-right (^ (x acc)
                 (cons x (cons x acc)))
              '()
              ls))

(define (main args)
  (let-args (cdr args)
      ((help "h|help" => (cut usage (car args)))
       (else (opt . _)
             (print "Unknown option : " opt)
             (usage (car args)))
       . rest)
    (let1 data (if (null? rest)
                   (input->list)
                   (apply map (^ rows (apply append rows))
                          (map (cut with-input-from-file <> input->list)
                               rest)))
      (print (draw (map list->double data))))))
                   
           0000    
        00077770   
      00777777770  
    0077777777770  
   07777777770770  
  077000077777070  
 070055550777770   
0700555555077770   
07050355555077770  
07005003003507770  
 0700303300307770  
  07030330330770   
  0703333370770    
 00070333307700    
0577070000777770   
05550777770000770  
05577007705777070  
05777777705557770  
05557777705577770  
05577777705777770  
05770777705557770  
05550777705577770  
 0570775770575770  
 0505755755055750  
  0555555555055550 
   000000000 0000  
                   
               00  
             00330 
            033320 
          0033320  
        003333220  
 000000033332220   
0333333332222220   
 02222333322220    
  0002222333200    
    00002222220    
    030000022220   
   00300300002220  
  04000030000400   
 040000000000040   
 044400044444440   
 00444444400040    
0330444440044440   
03304444033044440  
 004404403304440   
 040440040044440   
 040444440044440   
 044044440044440   
 044404444044440   
 0444400444044040  
044444444444004440 
 0000000000000000  
                   
                   
                   
  0000 00000    0 
 02222022222000020
 022222222222222220
 02022222222222220 
  0226662266222020 
  0206662266262220 
   00333022222220  
     000322222220  
    032032332220   
  000303333322000  
 06663333332066660 
  0603333000222020 
  022003060003330  
033320002033023330 
033302022033322330 
0330 022003332230  
 00 060066000000   
     06600660      
     0603300       
      03333000     
     0222222220    
      06666660     
      0266660      
     02226620      
      000000       
                   
                   
   0 00 0000000    
  01011011111110   
 010111111111000   
 0111111111111110  
  011111111111100  
 01113113111111110 
 0101310011111110  
  00100031133100   
    030333133110   
  0003033330000    
 0110337330000     
 01103333011000    
  001033007703330  
 0101000110033330  
033011011330113330 
033301111333111330 
0333 077033311130  
 000 01110000000   
      01033010     
      0033300      
     011111110     
      01111110     
      711110       
     07771170      
      000000