From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ricardo Wurmus Subject: Re: Status of Submitted Patches Date: Sat, 12 May 2018 00:21:18 +0200 Message-ID: <87r2mhdeap.fsf@elephly.net> References: <8ea5d026-fab9-7b12-198e-610ad7743cb2@swecha.net> <871sfxev9w.fsf@elephly.net> <7626275c-3eee-bb05-ab9d-4c88ec6f0329@swecha.net> <87r2nvjte6.fsf@elephly.net> <5ab51417-b635-9725-9f48-3bc3f9b61fdf@swecha.net> <87tvsko2wd.fsf@elephly.net> <7290013c-990d-3f7d-d8db-38e090ed766a@swecha.net> <87zi28kt82.fsf@elephly.net> <8573e97d-d107-cde6-cd17-35f4ef6d2de3@swecha.net> <87k1takumm.fsf@elephly.net> <87o9hycwl6.fsf@elephly.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:44401) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fHGfK-0000XG-A4 for guix-devel@gnu.org; Fri, 11 May 2018 18:37:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fHGfH-00013x-8V for guix-devel@gnu.org; Fri, 11 May 2018 18:37:30 -0400 Received: from sender-of-o51.zoho.com ([135.84.80.216]:21134) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fHGfH-00013n-1B for guix-devel@gnu.org; Fri, 11 May 2018 18:37:27 -0400 In-reply-to: List-Id: "Development of GNU Guix and the GNU System distribution." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org Sender: "Guix-devel" To: Sahithi Yarlagadda Cc: guix-devel@gnu.org Hi Sahithi, >>> According to the Guile manual >>> >>> Ports are the way that Guile performs input and output. Guile can >>> read in characters or bytes from an =E2=80=9Cinput port=E2=80=9D, or= write them out >>> to an =E2=80=9Coutput port=E2=80=9D. >>> >>> Ports allow the programmer to ignore the details of input/output source= s >>> and sinks and concentrate on shuffling characters or bytes around. We >>> don=E2=80=99t need to know if the port is connected to a terminal or a = file or a >>> network socket. >>> >>> A programmer can implement custom ports with =E2=80=9Cmake-soft-port=E2= =80=9D and a >>> vector of up to five procedures that provide implementations of certain >>> operations (such as writing a string to the port). A silly example >>> would be a custom port that reads characters from standard input and >>> turns them to upper-case characters before passing them to whoever read= s >>> from the custom port. > (use-modules (ice-9 binary-ports)) > (define stdout (current-output-port)) > (define stdin (current-input-port)) > (display stdin) > (newline) > (display stdout) > (newline) These definitions have no effect on the code that follows. =E2=80=9Cstdout= =E2=80=9D and =E2=80=9Cstdin=E2=80=9D have no special meaning. You are just defining var= iables with these names, but you could give them any names. Giving them names has no meaningful effect, because you are not using the names later anyway. > (write (char-upcase(read-char(current-input-port)))) Here you are reading a character from the current input port. The result is fed to =E2=80=9Cchar-upcase=E2=80=9D, which turns it into an uppe= r-case variant, and then you write that character to the current default output port. While this achieves the goal for a single character it does not constitute a custom port. Have you read the documentation for =E2=80=9Cmake-custom-port=E2=80=9D in the Guile manual? >>> With what you know about terminal escape codes and the port facility >>> that is used in =E2=80=9C(guix store)=E2=80=9D, can you describe an app= roach to add >>> colours to some text fragments before they are printed? There are at >>> least two ways this can be done; one of the two options is more >>> complicated but might result in a clearer and more modular >>> implementation. > > For this i tried using [=E2=80=A6] Actually, I was looking for a description of the two options, not an implementation :) Can you think of two possible ways to add colours to text fragments, and describe them in a few sentences? If you can only think of one approach, please describe only that approach. I guess that you wanted the value of =E2=80=9Ca=E2=80=9D to be printed in c= olours, but the code says otherwise. Here you are just displaying a string that contains the character =E2=80=9Ca=E2=80=9D, not the value of the variable w= ith the name =E2=80=9Ca=E2=80=9D. > (define a (char-upcase(read-char (current-input-port)))) [=E2=80=A6] > (define b (colorize-string a '(GREEN))) > (display b) [=E2=80=A6] > 4058: 2 [#] > In /home/sripathroy/Documents/GNUGuix/experiments/sam.scm: > 17: 1 [#] > In unknown file: > ?: 0 [string-append "\x1b[32m" #\S "\x1b[0m"] > > ERROR: In procedure string-append: > ERROR: In procedure string-append: Wrong type (expecting string): #\S What type is the value in the variable with name =E2=80=9Ca=E2=80=9D? =E2= =80=9Cread-char=E2=80=9D takes a port and returns a single character from it, =E2=80=9Cchar-upcase=E2=80= =9D takes a character and returns a different character, so =E2=80=9Ca=E2=80=9D holds a= character. As you know, the implementation of =E2=80=9Ccolorize-string=E2=80=9D intern= ally glues a bunch of strings together: a terminal escape sequence to set the colour, the string to be coloured, and a terminal escape sequence to disable the colour. You gave a character to the procedure, but it expects a string. You can convert strings to characters, but this is not what should be done here. If you did that for the string =E2=80=9Chello=E2=80=9D you woul= d enable colours, print an =E2=80=9Ch=E2=80=9D, disable colours, enable colours, pri= nt an =E2=80=9Ce=E2=80=9D, disable colours, etc. That=E2=80=99s rather wasteful because you really wa= nt to only enable colours once, print the string, and then disable colours again. But we=E2=80=99re getting ahead of ourselves. Getting back to the original problem: can you think of and shortly describe two possible ways to alter, filter or augment the output that is sent to the =E2=80=9Ccurrent-build-output-port=E2=80=9D in =E2=80=9C(guix store)=E2=80= =9D? Try to describe this at a higher level, ignoring a concrete implementation. (Bits are for computers, ideas are for humans.) -- Ricardo