zenity.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. (define-module (zenity)
  2. #:export (zenity
  3. zenity-calendar
  4. zenity-file-selection
  5. zenity-list zenity-checklist
  6. zenity-error zenity-info zenity-question zenity-warning
  7. zenity-password
  8. zenity-entry)
  9. #:use-module (ice-9 popen)
  10. #:use-module (srfi srfi-1)
  11. #:use-module (srfi srfi-11)
  12. #:use-module (srfi srfi-14))
  13. ;; https://help.gnome.org/users/zenity/stable/
  14. ;; Utility functions
  15. (define (drain-input port)
  16. (let ((ch (read-char port)))
  17. (if (eof-object? ch)
  18. '()
  19. (cons ch (drain-input port)))))
  20. (define (chomp text)
  21. (string-trim-right text (char-set #\newline)))
  22. (define (boolean->zenity-boolean b)
  23. (if b
  24. "TRUE"
  25. "FALSE"))
  26. (define (->string thing)
  27. (cond ((string? thing) thing)
  28. ((symbol? thing) (symbol->string thing))
  29. ((number? thing) (number->string thing))
  30. ((boolean? thing) (boolean->zenity-boolean thing))
  31. (else (error "unsupported type in ->string"))))
  32. ;;
  33. (define (zenity . args)
  34. (let* ((pipe (apply open-pipe* OPEN_READ "zenity" args))
  35. (text (list->string (drain-input pipe)))
  36. (ret (close-pipe pipe)))
  37. (values ret text)))
  38. (define (zenity/check args thunk)
  39. (let-values (((ret text) (apply zenity args)))
  40. (cond ((= ret 0) (thunk text))
  41. ((= ret 256) #f)
  42. (else (error "unexpected return code in zenity")))))
  43. ;; Calendar Dialog — Use the --calendar option.
  44. (define (zenity-calendar message)
  45. (zenity/check (list "--calendar" "--date-format=%d/%m/%Y" (string-append "--text=" message))
  46. (lambda (text)
  47. (let ((dmy (string-split (chomp text) #\/)))
  48. (list (cons 'day (string->number (car dmy)))
  49. (cons 'month (string->number (cadr dmy)))
  50. (cons 'year (string->number (caddr dmy))))))))
  51. ;; Color Selection Dialog — Use the --color-selection option.
  52. ;; File Selection Dialog — Use the --file-selection option.
  53. (define* (zenity-file-selection title
  54. #:key (multiple #f)
  55. (directory #f)
  56. (save #f)
  57. (filename #f))
  58. (let ((args (append
  59. (if multiple (list "--multiple") (list))
  60. (if directory (list "--directory") (list))
  61. (if save (list "--save") (list))
  62. (if filename (list (string-append "--filename=" filename)) (list)))))
  63. (let-values (((ret text) (apply zenity "--file-selection"
  64. (string-append "--title=" title)
  65. args)))
  66. (if (= ret 256)
  67. #f
  68. (if multiple
  69. ;; I hope you don't have files with | in the name.
  70. (string-split (chomp text) #\|)
  71. (chomp text))))))
  72. ;; Forms Dialog — Use the --forms option.
  73. ;; List Dialog — Use the --list option.
  74. (define (zenity-list message columns rows)
  75. (let ((columns^ (map (lambda (col) (string-append "--column=" col)) columns))
  76. (rows^ (apply append (map (lambda (row) (map ->string row)) rows))))
  77. (zenity/check (cons "--list" (append columns^ rows^))
  78. (lambda (text)
  79. (assoc (chomp text) rows (lambda (k c) (string=? k (->string c))))))))
  80. (define (zenity-checklist message columns rows)
  81. (let ((columns^ (map (lambda (col) (string-append "--column=" col)) columns))
  82. (rows^ (apply append (map (lambda (row) (map ->string row)) rows))))
  83. (zenity/check (append (list "--list" "--checklist") (append columns^ rows^))
  84. (lambda (text)
  85. (string-split (chomp text) #\|)))))
  86. ;; Message Dialog — Error, Info, Question, Warning
  87. (define (zenity-error message)
  88. (zenity "--error" (string-append "--text=" message))
  89. #t)
  90. (define (zenity-info message)
  91. (zenity "--info" (string-append "--text=" message))
  92. #t)
  93. (define (zenity-question message)
  94. (zenity/check (list "--question" (string-append "--text=" message))
  95. (lambda (_) #t)))
  96. (define (zenity-warning message)
  97. (zenity "--warning" (string-append "--text=" message))
  98. #t)
  99. ;; Notification Icon — Use the --notification option.
  100. ;; Password Dialog — Use the --password option.
  101. (define (zenity-password message)
  102. (zenity/check (list "--password" message (string-append "--text=" message))
  103. chomp))
  104. ;; Progress Dialog — Use the --progress option.
  105. ;; Scale Dialog — Use the --scale option.
  106. ;; Text Entry Dialog — Use the --entry option.
  107. (define (zenity-password message)
  108. (zenity/check (list "--entry" message (string-append "--text=" message))
  109. chomp))
  110. ;; Text Information Dialog — Use the --text-info option.