From patchwork Sat Apr 9 20:23:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 38473 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 4C69F27BBEA; Sat, 9 Apr 2022 21:25:35 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 194B227BBE9 for ; Sat, 9 Apr 2022 21:25:35 +0100 (BST) Received: from localhost ([::1]:45394 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ndHeI-00089P-9P for patchwork@mira.cbaines.net; Sat, 09 Apr 2022 16:25:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33200) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ndHdq-0007t8-CV for guix-patches@gnu.org; Sat, 09 Apr 2022 16:25:06 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:44855) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ndHdn-0001Ok-Ny 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-0002ph-Jp for guix-patches@gnu.org; Sat, 09 Apr 2022 16:25:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#54823] [PATCH 2/3] colors: Add 'colorize-full-matches'. 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.164953586710823 (code B ref 54823); Sat, 09 Apr 2022 20:25:03 +0000 Received: (at 54823) by debbugs.gnu.org; 9 Apr 2022 20:24:27 +0000 Received: from localhost ([127.0.0.1]:38750 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ndHdC-0002oV-H6 for submit@debbugs.gnu.org; Sat, 09 Apr 2022 16:24:26 -0400 Received: from eggs.gnu.org ([209.51.188.92]:39766) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ndHcw-0002nW-2s for 54823@debbugs.gnu.org; Sat, 09 Apr 2022 16:24:25 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:56252) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ndHcq-00019D-SK; Sat, 09 Apr 2022 16:24:04 -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=KWSC9tQImhYSyjPeKeFnX3+JHLwlWqc4xophC5mie/s=; b=EA5n2nB4oqrqLR9fomSU Nz7apqzSShZzghfFXFnokVmZqZFWQv6tI/1ughvz/VNumjsD9zER/gOiai09G7lyo6sNcC10UCVe5 xNIabDZjm6AbO0Az/nt1HtSLRowVRNdsgsPH4tFZtFkrMSXNGg1RiDON+T1UkKX0Uh9ktZ9AZNk92 cS5sKWD9nphINhrlepFtKRqgJbG37vs3tx6yTd5+OBguFdFOndn3Re6d3LaSdnYs3PHG9pYjGO3YJ FAoso+YIgZpklqF7Amca4LWgncrXksm92O9CDqJkgaW/7upQ/2v7zGBn47dz2KMspiiTQx3SgseJK mt6Mw5kMiO1XBg==; 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 1ndHcq-0004pO-2I; Sat, 09 Apr 2022 16:24:04 -0400 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Sat, 9 Apr 2022 22:23:43 +0200 Message-Id: <20220409202344.32090-2-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 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+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * guix/colors.scm (colorize-full-matches): New procedure. --- guix/colors.scm | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/guix/colors.scm b/guix/colors.scm index 3fd36c68ef..543f4c3ec5 100644 --- a/guix/colors.scm +++ b/guix/colors.scm @@ -36,6 +36,7 @@ (define-module (guix colors) highlight/warn dim + colorize-full-matches color-rules color-output? isatty?* @@ -153,6 +154,27 @@ (define highlight (coloring-procedure (color BOLD))) (define highlight/warn (coloring-procedure (color BOLD MAGENTA))) (define dim (coloring-procedure (color DARK))) +(define (colorize-full-matches rules) + "Return a procedure that, given a string, colorizes according to RULES. +RULES must be a list of regexp/color pairs; the whole match of a regexp is +colorized with the corresponding color." + (define proc + (lambda (str) + (if (string-index str #\nul) + str + (let loop ((rules rules)) + (match rules + (() + str) + (((regexp . color) . rest) + (match (regexp-exec regexp str) + (#f (loop rest)) + (m (string-append (proc (match:prefix m)) + (colorize-string (match:substring m) + color) + (proc (match:suffix m))))))))))) + proc) + (define (colorize-matches rules) "Return a procedure that, when passed a string, returns that string colorized according to RULES. RULES must be a list of tuples like: