Search code examples
schemegnuguile

Alter REPL to display username, hostname and current working directory?


In Guile's REPL the prompt is scheme@(guile-user)>, but I want it to show my-name@hostname(current-working-directory)>. Is there a way to do this?


Solution

  • IN system/repl/common in the guile scheme distribution you can see the repl-prompt implementation:

    (define (repl-prompt repl)
      (cond
        ((repl-option-ref repl 'prompt)
         => (lambda (prompt) (prompt repl)))
        (else
          (format #f "~A@~A~A> " (language-name (repl-language repl))
            (module-name (current-module))
            (let ((level (length (cond
                                  ((fluid-ref *repl-stack*) => cdr)
                                  (else '())))))
              (if (zero? level) "" (format #f " [~a]" level)))))))
    

    This indicates that you have a repl option 'prompt which is a lambda

    (lambda (repl) ...)
    

    (But it can also be a simple string) Than can outputs anything you want.

    You have,

    https://www.gnu.org/software/guile/manual/html_node/System-Commands.html#System-Commands

    so you can do

    scheme@(guile-user)> ,option prompt ">"
    >
    >(+ 1 2)
    $1 = 3
    >,option prompt (lambda (repl) ">>")
    >>(+ 2 3)
    $2 = 5
    >>
    

    But what to do if you want to add the prompt in your .guile file?

    If you put these inside .guile (before the prompt is created)

    (use-modules (system repl common))
    (repl-default-option-set! 'prompt ">>>")
    

    you will get

    >>> (+ 1 2)
    3
    

    You can create new repls as well, but that's for another question

    For your specific example you can try

    > ,option prompt (lambda (repl) 
                        (format #f "~a@~a(~a)>" 
                                (getenv "USER") 
                                (vector-ref (uname) 1) 
                                (getcwd)))
    

    (but on one line) and get

     stis@lapwine(/home/stis/src/guile/module/system/repl)> (+ 1 2)
     3
    

    Hope that this helps.