guix-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fwd: Re: Patch file for colorize module


From: Ricardo Wurmus
Subject: Re: Fwd: Re: Patch file for colorize module
Date: Thu, 31 May 2018 21:28:24 +0200
User-agent: mu4e 1.0; emacs 26.1

Hi Sahithi,

>> Have you started on working on this yet?  If so,could you please give us
>> an update on your progress via email?
>>
> I have started out using different functions like
>
> |1) regexp-match 2) ||string-contains which resulted same output for
> strings

The procedures tell you if something matched.

> then i tried 1)
> string-match 2) string-substitute ended up using string substitute so
> that the result can be colored one.

“string-match” either returns #f if the expression didn’t match or it
returns a match structure that tells you *how* the expression was
matched.  It is especially useful with match groups that are marked with
parentheses in the regular expression.  See below for an example.

> But I failed executing it. File is
> attached, Can u suggest where I went wrong.

One obvious failing is in the arguments to “make-soft-port”.  It takes a
vector of five procedures, but you gave it a vector of one procedure
followed by an expression beginning with “regexp-substitute/global” and
then three more procedures.

You need to give it five procedures wrapped in a vector.

How about doing it this way:

--8<---------------cut here---------------start------------->8---
;; The port to which we write our potentially colorized strings
(define target-port (current-output-port))

(define (handle-string str)
  "Match on the input string STR and return a new string with added
  color sequences."
  ;; TODO: match on str and pass the modified string to the output port
  (display str target-port))

(define my-colorful-port
  (make-soft-port
   (vector
    (lambda (c) (write c target-port))
    handle-string
    (lambda () (display "." target-port))
    (lambda () (char-upcase (read-char)))
    (lambda () (display "@" target-port)))
   "rw"))

;;;; Some tests!

(display "Hello there!" my-colorful-port)  ; no colours
(display "starting phase “Big gorilla” — watch out!" my-colorful-port)
(display "phase “Big gorilla” failed" my-colorful-port)
(display "I heard phase “Big gorilla” failed" my-colorful-port) ; no colours 
here
;;; …and so on…
--8<---------------cut here---------------end--------------->8---

Now all you need to do is work on the “handle-string” procedure.

I suggest using simpler matching procedures at first.  To get started
try “string-prefix?” and use it with the string “starting phase”.  This
won’t work with regular expressions, though.

While you *can* use “regexp-substitute/global”, I don’t think it’s a
good fit here, because we may want to extend the string matching
features, which is difficult to do with “regexp-substitute/global”.
Instead, try to match regular expressions one by one with “string-match”
and then operate on the match structure it returns.  If it returns #f
you can move on to the next expression.  If none match you just return
the original string.  If one matches you *rebuild* the string, but with
colours applied.

Here’s an example:

  (define str  "My name is Al Jarreau and I’m 76 years old.")
  (define expr "(My name is )(.*)( and I’m )(.*)( years old.)")

These are five match groups and we want to modify the second and fourth,
so we can do this:

(or (and=> (string-match expr str)
           (lambda (m)
             (string-append
              (match:substring m 1)
              (string-upcase (match:substring m 2))
              (match:substring m 3)
              (string-reverse (match:substring m 4))
              (match:substring m 5))))
    ;; Didn’t match, so return unmodified string.
    str)

If you don’t understand this example please look up the procedures in
the Guile manual.

> As per IRC discussion with Ricardo, I tried installing emacs and
> running a shell.

That is correct.  We were trying to take a look at the features
guix-build-log-minor-mode provides, but we didn’t get that far.

--
Ricardo




reply via email to

[Prev in Thread] Current Thread [Next in Thread]