From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Davide Masserut via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#65604: [PATCH] Display the exit code if the last command failed in Eshell Date: Wed, 30 Aug 2023 00:24:28 +0200 Message-ID: <87cyz5wjtm.fsf@mssdvd.com> Reply-To: Davide Masserut Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="36722"; mail-complaints-to="usenet@ciao.gmane.io" To: 65604@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Aug 30 01:07:26 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qb7nx-0009Kf-OV for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 30 Aug 2023 01:07:26 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qb7mo-0000fn-TZ; Tue, 29 Aug 2023 19:06:14 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qb7SB-0005gg-Im for bug-gnu-emacs@gnu.org; Tue, 29 Aug 2023 18:44:55 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qb7SB-0002oe-9a for bug-gnu-emacs@gnu.org; Tue, 29 Aug 2023 18:44:55 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qb7SI-0002Jj-3U for bug-gnu-emacs@gnu.org; Tue, 29 Aug 2023 18:45:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Davide Masserut Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 29 Aug 2023 22:45:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 65604 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16933490678838 (code B ref -1); Tue, 29 Aug 2023 22:45:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 29 Aug 2023 22:44:27 +0000 Original-Received: from localhost ([127.0.0.1]:51777 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qb7Ri-0002IP-7Q for submit@debbugs.gnu.org; Tue, 29 Aug 2023 18:44:27 -0400 Original-Received: from lists.gnu.org ([2001:470:142::17]:38488) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qb7Rf-0002IA-3Y for submit@debbugs.gnu.org; Tue, 29 Aug 2023 18:44:24 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qb7RS-0004x9-8u for bug-gnu-emacs@gnu.org; Tue, 29 Aug 2023 18:44:10 -0400 Original-Received: from out-248.mta0.migadu.com ([91.218.175.248]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qb7RO-0002i0-OT for bug-gnu-emacs@gnu.org; Tue, 29 Aug 2023 18:44:10 -0400 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mssdvd.com; s=key1; t=1693349034; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=G/MVQE88Dp2Kwul2RNOKDmHHETyRzOKn3VCoQ0shVq8=; b=vJ6zFLO4MzLWHggyQH6SeXlZ5YI0ED6yDH3yjcYaS/FcVbg7UdEHueHVfRL4Uh50Ms/Cae RX+PTg3fJC13hUW4Sbke8rcxKFezr5gvIvrH+ag1hd5G6wfMkckBKIHe2fl3lOvDgNHF4f LxlvSDR0MSwmIKuyP3eop6S6P4vUOaRfxM+odG8vFk37erOw/N9YL+UxsuZFSQAhV4Zvvj A7kQWYctG30roZvMOU/8Nt/JPa6OVcQbnGdy4ue+qe2EF7XTwN0pseH/b82+NXzLNBp/Si 5TTSmbLUqkrhU4QwK6UhA7c2G1Dr2MQQfPmgvSvZK12+825SEfRKG4dqisHsoA== X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=91.218.175.248; envelope-from=dm@mssdvd.com; helo=out-248.mta0.migadu.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:268681 Archived-At: --=-=-= Content-Type: text/plain; format=flowed Tags: patch There are commands that fail without printing any messages, but set specific error codes. This patch extends the default prompt function to show the exit code of the previous failed command. Before: ~ $ false ~ $ After: ~ $ false ~ [1] $ I believe this is a good default, since it is displayed only when a error occurs and hopefully makes debugging easier by showing the error code without further input. In GNU Emacs 30.0.50 (build 10, x86_64-pc-linux-gnu, GTK+ Version 3.24.38, cairo version 1.17.8) of 2023-08-29 built on T480s Repository revision: ed77dc17f657d99ccf23778c14f06f7226f478f0 Repository branch: master System Description: Arch Linux Configured using: 'configure -C --prefix /home/davide/.local --with-pgtk --with-native-compilation --enable-link-time-optimization --enable-locallisppath=/usr/share/emacs/site-lisp/ 'CFLAGS=-march=native -O2'' --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-Display-the-exit-code-if-the-last-command-failed-in-.patch >From a0e2f7bda33767e8ce104dd32c76e4d3d6c1a2f8 Mon Sep 17 00:00:00 2001 From: Davide Masserut Date: Tue, 29 Aug 2023 22:33:48 +0200 Subject: [PATCH] Display the exit code if the last command failed in Eshell * etc/NEWS: Announce change. * lisp/eshell/em-prompt.el (eshell-prompt-function): Insert the exit code if last command failed. * lisp/eshell/esh-io.el (eshell-last-command-status): Make it buffer-local. * test/lisp/eshell/em-prompt-tests.el (em-prompt-test/after-failure) (em-prompt-test/next-previous-prompt-with) (em-prompt-test/forward-backward-matching-input-with): New tests. --- etc/NEWS | 3 +++ lisp/eshell/em-prompt.el | 2 ++ lisp/eshell/esh-io.el | 2 +- test/lisp/eshell/em-prompt-tests.el | 37 +++++++++++++++++++++++++---- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 9a98db8c83a..9622e57f476 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -370,6 +370,9 @@ to load the edited aliases. Running 'rgrep' in Eshell now uses the Emacs grep facility instead of calling external rgrep. +--- +*** The Eshell prompt now shows the exit code if the last command failed. + ** Pcomplete --- diff --git a/lisp/eshell/em-prompt.el b/lisp/eshell/em-prompt.el index 42f8f273b52..692b579d02d 100644 --- a/lisp/eshell/em-prompt.el +++ b/lisp/eshell/em-prompt.el @@ -50,6 +50,8 @@ eshell-prompt-load-hook (defcustom eshell-prompt-function (lambda () (concat (abbreviate-file-name (eshell/pwd)) + (unless (eshell-exit-success-p) + (format " [%d]" eshell-last-command-status)) (if (= (file-user-uid) 0) " # " " $ "))) "A function that returns the Eshell prompt string. Make sure to update `eshell-prompt-regexp' so that it will match your diff --git a/lisp/eshell/esh-io.el b/lisp/eshell/esh-io.el index c07f871dd37..cd0cee6e21d 100644 --- a/lisp/eshell/esh-io.el +++ b/lisp/eshell/esh-io.el @@ -170,7 +170,7 @@ eshell-redirection-operators-alist (defvar eshell-current-handles nil) -(defvar eshell-last-command-status 0 +(defvar-local eshell-last-command-status 0 "The exit code from the last command. 0 if successful.") (defvar eshell-last-command-result nil diff --git a/test/lisp/eshell/em-prompt-tests.el b/test/lisp/eshell/em-prompt-tests.el index 93bf9d84ab3..c90f417cefd 100644 --- a/test/lisp/eshell/em-prompt-tests.el +++ b/test/lisp/eshell/em-prompt-tests.el @@ -34,6 +34,25 @@ ;;; Tests: +(ert-deftest em-prompt-test/after-failure () + "Check that current prompt shows the exit code of the last failed command." + (with-temp-eshell + (let ((debug-on-error nil)) + (eshell-insert-command "(zerop \"foo\")")) + (let ((current-prompt (field-string (1- (point))))) + (should (equal-including-properties + current-prompt + (propertize + (concat (directory-file-name default-directory) + (unless (eshell-exit-success-p) + (format " [%d]" eshell-last-command-status)) + (if (= (file-user-uid) 0) " # " " $ ")) + 'read-only t + 'field 'prompt + 'font-lock-face 'eshell-prompt + 'front-sticky '(read-only field font-lock-face) + 'rear-nonsticky '(read-only field font-lock-face))))))) + (ert-deftest em-prompt-test/field-properties () "Check that field properties are properly set on Eshell output/prompts." (with-temp-eshell @@ -88,6 +107,8 @@ em-prompt-test--with-multiline (defun em-prompt-test/next-previous-prompt-with () "Helper for checking forward/backward navigation of old prompts." (with-temp-eshell + (let ((debug-on-error nil)) + (eshell-insert-command "(zerop \"foo\")")) ; A failed command. (eshell-insert-command "echo one") (eshell-insert-command "echo two") (eshell-insert-command "echo three") @@ -99,8 +120,11 @@ em-prompt-test/next-previous-prompt-with (end-of-line) (eshell-previous-prompt 2) (should (equal (eshell-get-old-input) "echo one")) - ;; Go forward three prompts. - (eshell-next-prompt 3) + ;; Go back one prompt. + (eshell-previous-prompt 1) + (should (equal (eshell-get-old-input) "(zerop \"foo\")")) + ;; Go forward four prompts. + (eshell-next-prompt 4) (should (equal (eshell-get-old-input) "echo fou")))) (ert-deftest em-prompt-test/next-previous-prompt () @@ -115,6 +139,8 @@ em-prompt-test/next-previous-prompt-multiline (defun em-prompt-test/forward-backward-matching-input-with () "Helper for checking forward/backward navigation via regexps." (with-temp-eshell + (let ((debug-on-error nil)) + (eshell-insert-command "(zerop \"foo\")")) ; A failed command. (eshell-insert-command "echo one") (eshell-insert-command "printnl something else") (eshell-insert-command "echo two") @@ -127,8 +153,11 @@ em-prompt-test/forward-backward-matching-input-with (end-of-line) (eshell-backward-matching-input "echo" 2) (should (equal (eshell-get-old-input) "echo one")) - ;; Go forward three prompts. - (eshell-forward-matching-input "echo" 3) + ;; Go back one prompt. + (eshell-previous-prompt 1) + (should (equal (eshell-get-old-input) "(zerop \"foo\")")) + ;; Go forward four prompts. + (eshell-forward-matching-input "echo" 4) (should (equal (eshell-get-old-input) "echo fou")))) (ert-deftest em-prompt-test/forward-backward-matching-input () -- 2.42.0 --=-=-=--