From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id 2MeYMLTrUWKZbAAAgWs5BA (envelope-from ) for ; Sat, 09 Apr 2022 22:25:24 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id uL9LLbTrUWJOhwAAauVa8A (envelope-from ) for ; Sat, 09 Apr 2022 22:25:24 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 418971689D for ; Sat, 9 Apr 2022 22:25:24 +0200 (CEST) Received: from localhost ([::1]:45120 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ndHe6-0007ua-VR for larch@yhetil.org; Sat, 09 Apr 2022 16:25:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33208) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ndHdq-0007tB-Es for guix-patches@gnu.org; Sat, 09 Apr 2022 16:25:06 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:44854) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ndHdn-0001Mk-AO for guix-patches@gnu.org; Sat, 09 Apr 2022 16:25:06 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ndHdn-0002pa-5P for guix-patches@gnu.org; Sat, 09 Apr 2022 16:25:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#54823] [PATCH 3/3] ui: Highlight package and service search results. Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 09 Apr 2022 20:25:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54823 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 54823@debbugs.gnu.org Cc: Ludovic =?UTF-8?Q?Court=C3=A8s?= Received: via spool by 54823-submit@debbugs.gnu.org id=B54823.164953585410792 (code B ref 54823); Sat, 09 Apr 2022 20:25:03 +0000 Received: (at 54823) by debbugs.gnu.org; 9 Apr 2022 20:24:14 +0000 Received: from localhost ([127.0.0.1]:38748 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ndHd0-0002o0-0j for submit@debbugs.gnu.org; Sat, 09 Apr 2022 16:24:14 -0400 Received: from eggs.gnu.org ([209.51.188.92]:39772) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ndHcw-0002na-Uu for 54823@debbugs.gnu.org; Sat, 09 Apr 2022 16:24:11 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:56254) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ndHcr-00019Q-Nu; Sat, 09 Apr 2022 16:24:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To: From; bh=tSLsbOfsvB/ioBLaz5CX73+dh3ZrTyt+7F3n5tCGacI=; b=oshUlNHR6HCp6TBUECRE /YlFIHcEMZD0s634ALhWP/17dnm88nfDUf5TjMEN0qs/ES2vPrQulcP6JjIKqj6olXYIfvWZGxJE+ ucKSo1VZAcZw6vFMddH1g0+CULc14qTgw2g7CjUYXcGAeUFCVljqRG/SZO/VxweOxZ3vsmY1Qt3ar BcWfDjlfpXbpxtDB/i0yD8pEvN/akH+Lfmnb58zPhyDA0GHsmjRtYhYQV0rn2+WAm7ACBEvuESrWE aFfuH+bYBggHyrS4LC8jnoWe0uABOe4qPnrn0v5mgR5dwT9Nq5FP7ELSqyQJXCPIDnmcnERpJNIOJ DEqSNz3yuT2TQQ==; Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201]:58105 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ndHcr-0004pO-2k; Sat, 09 Apr 2022 16:24:05 -0400 From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Date: Sat, 9 Apr 2022 22:23:44 +0200 Message-Id: <20220409202344.32090-3-ludo@gnu.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220409202344.32090-1-ludo@gnu.org> References: <20220409202344.32090-1-ludo@gnu.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1649535924; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=LGccAb7PmBVaVtPVI7JJwkTMo3MhLtg4fdL45lPKy18=; b=WkCawzCrkssWw8xEgfA7hDh0+LcVhjvPZgPgEfP/RA5vFza+vWDw8gFbf3ezJk6Es6POGX k69kOmvLhptWH7EnGgr5peAGQv8NwaGNumb1nPPp/q0REcHdrkLE8ZNQ0sv7YMvrawZIrk RjyDw1x9O63hd9YgoHld6920788HGg4vBUsZXo6akeGswJn8BlcC4ufeVhXDIoXSjUYjxi UyZda8kfh0odd2+Wga8vmrihJ6o+z1d3mr0N1NdNIRnSePiIhDS7hkMFaIqND8MPmLXsb+ ezAD6OS9KfGNWFbd+Q9sfI5JLOZa0MyuKjBgPABKDDaj6zuJS/PebFcl+YfNHA== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1649535924; a=rsa-sha256; cv=none; b=bQDHmJywnaDi3FLgJuy40PNsNKfTjj94S99M+TWFx+UjuwK3JvvW4JTEKq5MoC4rBcFH99 wYoiZJtY/KMQos+aMtio0WRGxSu1k8VNZMGZumtejXsBlsc/y8CY6DY+uKM7CWlM3nfR2D POaPpXDYft8SujuIDZSOcxZ0nWDD7CKavcLw9KROiYN6zHCCa6PC/SmhJQ2AlCTouXoOxo 9XmFdaLrI2BNUdWFsFi4GPeuDoikDDUAxBCQLegDyLRkRLASi7SoJ8sedKcp10yLwLDj7j kjE+Ce6r24SoQxAU+jv/5hi0E38TKaUKapz+6nyxrFoRuuKmobZzPI3H5Delkw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gnu.org header.s=fencepost-gnu-org header.b=oshUlNHR; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -2.77 Authentication-Results: aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gnu.org header.s=fencepost-gnu-org header.b=oshUlNHR; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 418971689D X-Spam-Score: -2.77 X-Migadu-Scanner: scn0.migadu.com X-TUID: BmuDfXIaSGEz * guix/ui.scm (package->recutils): Add #:highlighting parameter and use it. (display-search-results): Add #:regexps parameter; call 'colorize-full-matches' and pass #:highlighting. * guix/scripts/package.scm (process-query): Pass #:regexps to 'display-search-results'. * guix/scripts/home.scm (search): Likewise. * guix/scripts/system/search.scm (service-type->recutils): Add #:highlighting parameter and use it. --- guix/scripts/home.scm | 1 + guix/scripts/package.scm | 3 +- guix/scripts/system/search.scm | 30 +++++++++++------- guix/ui.scm | 57 ++++++++++++++++++++++------------ 4 files changed, 60 insertions(+), 31 deletions(-) diff --git a/guix/scripts/home.scm b/guix/scripts/home.scm index 341d83943d..f43bf865a7 100644 --- a/guix/scripts/home.scm +++ b/guix/scripts/home.scm @@ -733,6 +733,7 @@ (define (search . args) (leave-on-EPIPE (display-search-results matches (current-output-port) #:print service-type->recutils + #:regexps regexps #:command "guix home search"))))) diff --git a/guix/scripts/package.scm b/guix/scripts/package.scm index 22ee8a2485..d007005607 100644 --- a/guix/scripts/package.scm +++ b/guix/scripts/package.scm @@ -885,7 +885,8 @@ (define (diff-profiles profile numbers) (regexps (map (cut make-regexp* <> regexp/icase) patterns)) (matches (find-packages-by-description regexps))) (leave-on-EPIPE - (display-search-results matches (current-output-port))) + (display-search-results matches (current-output-port) + #:regexps regexps)) #t)) (('show _) diff --git a/guix/scripts/system/search.scm b/guix/scripts/system/search.scm index 2a237e03d9..d70ed266f4 100644 --- a/guix/scripts/system/search.scm +++ b/guix/scripts/system/search.scm @@ -20,7 +20,7 @@ (define-module (guix scripts system search) #:use-module (guix ui) #:use-module (guix utils) - #:autoload (guix colors) (highlight supports-hyperlinks?) + #:autoload (guix colors) (color-output? highlight supports-hyperlinks?) #:autoload (guix diagnostics) (location->hyperlink) #:use-module (gnu services) #:use-module (gnu services shepherd) @@ -70,10 +70,12 @@ (define* (service-type->recutils type port #:optional (width (%text-width)) #:key (extra-fields '()) - (hyperlinks? (supports-hyperlinks? port))) + (hyperlinks? (supports-hyperlinks? port)) + (highlighting identity)) "Write to PORT a recutils record of TYPE, arranging to fit within WIDTH columns. When HYPERLINKS? is true, emit hyperlink escape sequences when -appropriate." +appropriate. Pass the description through HIGHLIGHTING, a one-argument +procedure that may return a colorized version of its argument." (define port* (or (pager-wrapped-port port) port)) @@ -90,6 +92,11 @@ (define (extensions->recutils extensions) (fill-paragraph list width* (string-length "extends: "))))) + (define highlighting* + (if (color-output? port*) + highlighting + identity)) + ;; Note: Don't i18n field names so that people can post-process it. (format port "name: ~a~%" (highlight (symbol->string (service-type-name type)) @@ -114,14 +121,15 @@ (define (extensions->recutils extensions) (when (service-type-description type) (format port "~a~%" - (string->recutils - (string-trim-right - (parameterize ((%text-width width*)) - (texi->plain-text - (string-append "description: " - (or (and=> (service-type-description type) P_) - "")))) - #\newline)))) + (highlighting* + (string->recutils + (string-trim-right + (parameterize ((%text-width width*)) + (texi->plain-text + (string-append "description: " + (or (and=> (service-type-description type) P_) + "")))) + #\newline))))) (for-each (match-lambda ((field . value) diff --git a/guix/ui.scm b/guix/ui.scm index 555a614faa..cb68a07c6c 100644 --- a/guix/ui.scm +++ b/guix/ui.scm @@ -1485,10 +1485,13 @@ (define (string->recutils str) (define* (package->recutils p port #:optional (width (%text-width)) #:key (hyperlinks? (supports-hyperlinks? port)) - (extra-fields '())) + (extra-fields '()) + (highlighting identity)) "Write to PORT a `recutils' record of package P, arranging to fit within WIDTH columns. EXTRA-FIELDS is a list of symbol/value pairs to emit. When -HYPERLINKS? is true, emit hyperlink escape sequences when appropriate." +HYPERLINKS? is true, emit hyperlink escape sequences when appropriate. Pass +the synopsis and description through HIGHLIGHTING, a one-argument procedure +that may return a colorized version of its argument." (define port* (or (pager-wrapped-port port) port)) @@ -1510,6 +1513,11 @@ (define (dependencies->recutils packages) (define (packagerecutils - (string-trim-right - (parameterize ((%text-width width*)) - ;; Call 'texi->plain-text' on the concatenated string to account - ;; for the width of "description:" in paragraph filling. - (texi->plain-text* - p - (string-append "description: " - (or (and=> (package-description p) P_) - "")))) - #\newline))) + (highlighting* + (string->recutils + (string-trim-right + (parameterize ((%text-width width*)) + ;; Call 'texi->plain-text' on the concatenated string to account + ;; for the width of "description:" in paragraph filling. + (texi->plain-text* + p + (string-append "description: " + (or (and=> (package-description p) P_) + "")))) + #\newline)))) (for-each (match-lambda ((field . value) (let ((field (symbol->string field))) @@ -1707,10 +1717,12 @@ (define-syntax with-paginated-output-port (define* (display-search-results matches port #:key + (regexps '()) (command "guix search") (print package->recutils)) "Display MATCHES, a list of object/score pairs, by calling PRINT on each of -them. If PORT is a terminal, print at most a full screen of results." +them. If PORT is a terminal, print at most a full screen of results. REGEXPS +is a list of regexps to highlight in search results." (define first-line (port-line port)) @@ -1721,6 +1733,12 @@ (define max-rows (define (line-count str) (string-count str #\newline)) + (define highlighting + (let ((match-color (color ON-RED BOLD))) + (colorize-full-matches (map (lambda (regexp) + (cons regexp match-color)) + regexps)))) + (with-paginated-output-port paginated (let loop ((matches matches)) (match matches @@ -1728,7 +1746,8 @@ (define (line-count str) (let* ((links? (supports-hyperlinks? port))) (print package paginated #:hyperlinks? links? - #:extra-fields `((relevance . ,score))) + #:extra-fields `((relevance . ,score)) + #:highlighting highlighting) (loop rest))) (() #t))))) -- 2.35.1