Add (guix ui).
[jackhill/guix/guix.git] / guix / ui.scm
CommitLineData
073c34d7
LC
1;;; Guix --- Nix package management from Guile. -*- coding: utf-8 -*-
2;;; Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
3;;;
4;;; This file is part of Guix.
5;;;
6;;; Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (guix ui)
20 #:use-module (guix utils)
21 #:use-module (guix store)
22 #:use-module (guix packages)
23 #:use-module (srfi srfi-26)
24 #:use-module (srfi srfi-34)
25 #:export (_
26 N_
27 leave
28 call-with-error-handling
29 with-error-handling))
30
31;;; Commentary:
32;;;
33;;; User interface facilities for command-line tools.
34;;;
35;;; Code:
36
37(define %gettext-domain
38 "guix")
39
40(define _ (cut gettext <> %gettext-domain))
41(define N_ (cut ngettext <> <> <> %gettext-domain))
42
43(define-syntax-rule (leave fmt args ...)
44 "Format FMT and ARGS to the error port and exit."
45 (begin
46 (format (current-error-port) fmt args ...)
47 (exit 1)))
48
49(define (call-with-error-handling thunk)
50 "Call THUNK within a user-friendly error handler."
51 (guard (c ((package-input-error? c)
52 (let* ((package (package-error-package c))
53 (input (package-error-invalid-input c))
54 (location (package-location package))
55 (file (location-file location))
56 (line (location-line location))
57 (column (location-column location)))
58 (leave (_ "~a:~a:~a: error: package `~a' has an invalid input: ~s~%")
59 file line column
60 (package-full-name package) input)))
61 ((nix-protocol-error? c)
62 ;; FIXME: Server-provided error messages aren't i18n'd.
63 (leave (_ "error: build failed: ~a~%")
64 (nix-protocol-error-message c))))
65 (thunk)))
66
67(define-syntax with-error-handling
68 (syntax-rules ()
69 "Run BODY within a user-friendly error condition handler."
70 ((_ body ...)
71 (call-with-error-handling
72 (lambda ()
73 body ...)))))
74
75;;; ui.scm ends here