stumpwm-devel
[Top][All Lists]
Advanced

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

[PATCH/BUG] add optional REMOVE-NEWLINE to `run-shell-command' (was Re:


From: Luca Capello
Subject: [PATCH/BUG] add optional REMOVE-NEWLINE to `run-shell-command' (was Re: [STUMP] My .stumpwmrc and a modified display-keybinding)
Date: Sun, 14 Jan 2007 22:29:01 +0100
User-agent: Gnus/5.110006 (No Gnus v0.6) Emacs/22.0.92 (gnu/linux)

Hello!

On Wed, 10 Jan 2007 01:41:51 +0100, Luca Capello wrote:
> Thanks to matley (Luigi Panzeri) for having found the error,
> i.e. the escaping of the \n.  Thus, now we've two methods (again,
> empty lines added for better reading):
> =====
> STUMPWM> (defun test-newline-tr ()
>   (run-shell-command "echo -e \"Hello,\\n world\" | tr -d '\\n'" t))
> TEST-NEWLINE-TR
[...]
> STUMPWM> STUMPWM> (defun test-newline-tr ()
>   (run-shell-command "echo -e \"Hello,\\n world\" | tr -d \\\\n" t))
> STYLE-WARNING: redefining TEST-NEWLINE-TR in DEFUN
> TEST-NEWLINE-TR
[...]
> I'd go for the former, any comments?

Actually, I've a problem with `run-shell-command' on SBCL 0.9.16.0 and
1.0.0.0 (Debian testing and unstable).  A simple .stumpwmrc [1]:
=====
(in-package :stumpwm)
(setf *screen-mode-line-format*
      (list '(:eval (show-battery-charge)) " | "))

(defun show-battery-charge ()
  (string-right-trim '(#\Newline)
                     (run-shell-command "acpitool -b | cut -d , -f 2" t)))
=====

Now, as soon as launch "C-t ; mode-line", StumpWM crashes.  The same
happens with `show-battery-charge' defined as:

  (defun show-battery-charge ()
    (run-shell-command "acpitool -b | cut -d , -f 2 | tr -d '\\n'" t))

or the the similar one with "tr -d \\\\n".  The output in the terminal
where I started StumpWM from is "Caught The value NIL is not of type
CHARACTER. at the top level. Please report this".

The only way I can get it working is defining the function as the
following, not a nice definition and for sure not a practical one if
you've different similar functions.

  (defun show-battery-charge ()
    (substitute
      #\Space #\Newline
      (run-shell-command "acpitool -b | cut -d , -f 2 | tr -d '\\n'" t)))

While I am at it and as I guess removing the newline character from a
`run-shell-command' output when COLLECT-OUTPUT-P is T could be useful,
I propose the following patch (which doesn't solve my problem, but
anyway it could be an improvement).

--8<---------------cut here---------------start------------->8---
--- user.lisp   12 Jan 2007 15:39:25 +0100      1.82
+++ user.lisp   14 Jan 2007 22:27:51 +0100      
@@ -303,13 +303,18 @@
                          (namestring file)
                          namestring))))
 
-(defun run-shell-command (cmd &optional collect-output-p)
+(defun run-shell-command (cmd &optional collect-output-p remove-newline-p)
   "Run a shell command in the background or wait for it to finish
-and collect the output if COLLECT-OUTPUT-P is T. Warning! if
-COLLECT-OUTPUT-P is stumpwm will hang until your command
-returns..which could be forever if you're not careful."
+and collect the output if COLLECT-OUTPUT-P is T.  If REMOVE-NEWLINE-P
+is T, remove the last newline character using `string-right-trim'.
+
+Warning! if COLLECT-OUTPUT-P is stumpwm will hang until your
+command returns..which could be forever if you're not careful."
   (if collect-output-p
-      (run-prog-collect-output *shell-program* "-c" cmd)
+      (let ((output (run-prog-collect-output *shell-program* "-c" cmd)))
+       (if remove-newline-p
+           (string-right-trim '(#\Newline) output)
+           output))
       (run-prog *shell-program* :args (list "-c" cmd) :wait nil)))
 
 (define-stumpwm-command "exec" ((cmd :shell "/bin/sh -c "))
--8<---------------cut here---------------end--------------->8---

Thx, bye,
Gismo / Luca

Footnotes: 
[1] http://sourceforge.net/projects/acpitool/

Attachment: pgpalIdFJaxAT.pgp
Description: PGP signature


reply via email to

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