From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: from zero.zsh.org (zero.zsh.org [IPv6:2a02:898:31:0:48:4558:7a:7368]) by inbox.vuxu.org (Postfix) with ESMTP id 95B9E20CDF for ; Mon, 13 May 2024 22:55:02 +0200 (CEST) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1715633702; b=M+bQyXEjujTYAvVpC2tvMTiWkoEJpLEAIgI9xCjGwkcNmdjAzJ/xlhzWXxjpYTEfozjbvMGUlD nCouV8PBm8KCH/7L5gy59m+r0Lpug0DvHLDdWtj48aiCeuWT/d3iPasi0HvyfmvXJHvFviXn0f hRpmXgytn9TdzQMH6qA/04nMmBAVl6yAICbziPGbicflfSBf+eYaMIwAOkSRbT31pPpdV6Kdmq tUd7iofl1M6RvHMQuu6W2NkpG5k6hxrN4+GALBx2+5TS9e2PqRY6iRX4PG3G4JYCoYfW62oQz1 auT0+3PnIfpp92mXwrb4e1AvYq15ttRqAs94SAN0Vx5CWQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-vs1-f51.google.com) smtp.remote-ip=209.85.217.51; dkim=pass header.d=gmail.com header.s=20230601 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1715633702; bh=UdwCFi0/thg68v3qqJYGFZsr+r2/fXM0Q13anIi/XqQ=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:Content-Type:Cc:To:Subject: Message-ID:Date:From:In-Reply-To:References:MIME-Version:DKIM-Signature: DKIM-Signature; b=U+eejgABGCRlkmxSSnqp9CgPgcNl6EjPJYw7P3ZpKg+6leoGBTOAD1rm00/T+ROXfll7iAE1o+ XLkroS1yFdxaV/yKqE/8AwPlvOFhi6k0gaMkUmEcw1sIQ7qUKrCgvESUCtscAcdN6YzyttPyGu nIa6CpqpUVsMwhUSSLBLnKE3nYpdhdYDtqx3Phet2G23BaO1de4Ax/KvjIpV6Wyn6KGcciR7lU Eoz00IpPrlSfZQ0lSrFs+EKa3CEQ9I3C8haLuSslMttCgNHOBRuV3ytNPIysuGtR7uJy6je5Xh CsGQNCdL1wKzIDOjxCP0/HhpTaX+rLpGgXokV4QMA0JaOw==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Transfer-Encoding: Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To:References: MIME-Version:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=3Bgzyu+ByYVopH0Z6NVVgcagyFtpbbrovE1StbiuTBA=; b=FfYpfddBTiC/GLGvledNp4uT+d 4a4WRncbz1sSsD8o93XzfeGbR1y3J9OMQPdQauX6Coamo7KNm3B/2gLmsyF69N5/5mUK0yOa359HP R0coVKw2uE3Zvag9y6c2dVY0HyorTsadlBmKwtgF+oWxiZ1ximwqvs7fvv4UKnGl19uiJdUkEGLZU Ke671NFUqAtSgTSlAviQxbUjxv/fP6WZdNChUc1mGF7vTlJ2NHndilh/bcVbzF9Xld3Du/KGF5JZm TId+ljrGwkRzRFr85zHpFtkynX1DxR3MAhfjJaPVPNT0XSFWC6Q3bxt9o1XQpq0SuvDWSY13vfDb9 Gm1LP2yw==; Received: by zero.zsh.org with local id 1s6chJ-000JxV-Pq; Mon, 13 May 2024 20:55:01 +0000 Authentication-Results: zsh.org; iprev=pass (mail-vs1-f51.google.com) smtp.remote-ip=209.85.217.51; dkim=pass header.d=gmail.com header.s=20230601 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-vs1-f51.google.com ([209.85.217.51]:44070) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1s6cgW-000JET-OJ; Mon, 13 May 2024 20:54:14 +0000 Received: by mail-vs1-f51.google.com with SMTP id ada2fe7eead31-47eff9b3c23so1467263137.0 for ; Mon, 13 May 2024 13:54:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715633651; x=1716238451; darn=zsh.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=3Bgzyu+ByYVopH0Z6NVVgcagyFtpbbrovE1StbiuTBA=; b=QORRStmTo+kTX8YsNFByxTSDlOUWMxm/llpa8uvJ489OdFeI9fhomhttS+b3xYHL4n POgL6wZs0bHkkZaqxVxkBSlQgAdvIwXOZJzpLfDPREWTrmTDJg2C2xDcK0Q7WO07v1Qk tBAE50FhwN5Y/zXPJJg/PoeCkF3zqr162q0Sq6FURXhgQNB8aBDEkMx/MHReYQKOcESV SiushEf+/PDLT2MeUIoIreebZN2SGK+ZT29T6NB5rNK/c4Wg6Td/tWQfQY7hEYsGf3ro uhRNJj+RmkbOTRNR+ZvMDR5SFm8sbTK/55TfR0rtVe3TZGN+7SFH16v6oaMrrJ1ewgvI bh8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715633651; x=1716238451; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3Bgzyu+ByYVopH0Z6NVVgcagyFtpbbrovE1StbiuTBA=; b=E4fmp+RA78ehxRDTdsIoBiYIcInqGX7zKBMTkxpA3CNoK7uj6PdNkHAi7Nihk/b++P DCzT4rg5r8aeAOS8SFE6Nacw96fQc0VZFRIc5SRAsA8aqIt/5jlHGDryBWG0ihMn5T6y m6G4+yNBGvfsXkdSy6a1H9/rM1s/HlVmCaoRKugaSfKyF9gM73JY7Wh1yQDVJ5vfuBeW tgW/R46NED8nyIV0o5hGCwWgpQ/tX1qwlxFUhbpciy2oOApEqx+9/ymYmQrYG7q51Kf/ dHBx8l2LiZGd9oyw2CGOgNxU/BRBuF1RetA4/xodb4+aN1uG0GpgDBboiRJPGD6eU3i5 /nGw== X-Gm-Message-State: AOJu0YxYQ7bfNohq4rGEak7iTY078tcXwlgSUCeP4Y7BUbVkgu0r0yzX oylwm5i6C6fqPxdklNZj+EgU1Gu2L9xwtx+HnuC4q6A6tlsI5+EA8pTcptmS7z2kIUgl7FPdiyt KmMlJ7/hauY7vFkpLUUBCz4p8bQ== X-Google-Smtp-Source: AGHT+IHAY47UtnZ0RMSbfwzarOdWUbyAfIB8Csy9Dr48qXBvE27lQkbXvZCUl0FpfvIc3YMCbg/beN/04JjsNXyIjKE= X-Received: by 2002:a05:6102:5494:b0:47e:d83:3bab with SMTP id ada2fe7eead31-4814ab5c355mr5821089137.6.1715633651198; Mon, 13 May 2024 13:54:11 -0700 (PDT) MIME-Version: 1.0 References: <8d1d947e-75af-4a9a-b726-dabe9858b2b0@gmail.com> In-Reply-To: From: Rick Bowen Date: Mon, 13 May 2024 13:53:55 -0700 Message-ID: Subject: Re: colorizing printfs To: Kannan Varadhan Cc: Zsh Users Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Seq: 29932 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: I know I'm late, but I do like a colorful terminal... Kannan - I think you may be overcomplicating it - the StackExchange post certainly does! I personally find overly complex parameter expansion somewhat distasteful. Keep It Simple. The first part of the top answer is actually preferred, but it was answered in the context of the original question, rather than "how can I print colors in ZSH?" Specifically, line 13 is what you're looking for: print -P "%F{$color}$ip%f\n" print -P is what you probably want - unless you're trying to do something portable (a.k.a. bash), in which case you can't use autoload colors or zsh prompt expansion anyways, and should just stick to the more portable style of ANSI escape codes with 8 or 256 colors or truecolor modes. This is the "old fashioned" (portable) way of printing colors: printf "\e[32m%s\e[0m\n" "this is green" printf "\e[92m%s\e[0m\n" "this is bright green" printf "\e[38;5;120m%s\e[0m\n" "this is probably green" printf "\e[38;2;0;255;0m%s\e[0m\n" "as green as it gets" But in ZSH we can just use 'print -P' (see also: print in zshbuiltins(1)). Note: this doesn't require "autoload colors'' text=3D"What color is grass?" print -P "%F{green}$text%f" Using "autoload colors": autoload -Uz colors && colors text=3D"grass is green" echo "Did you know, ${fg[green]}${text}${fg[default]} but the sky is no= t" Under the hood, all we're doing is emitting "Select Graphic Rendition (SGR)" terminal control sequences - e.g. "ESC [ ATTR m" where ESC is one of: \e or \033 or \x1b followed by a literal "[", then an SGR attribute number and maybe optional parameters separated by a semi-colon ";", finally ending in the letter "m". printf-style SGR examples with "autoload colors" associative array and PROMPT_PERCENT/print -P equivalents: 1. \e[31m =3D \033[31m =3D \x1b[31m =3D red foreground using the terminal's 16 color palette =3D $fg[red] =3D $color[red] =3D %F{red} =3D %F{1} 2. \e[91m =3D "bright" red foreground =3D $fg_bold[red] =3D %B%F{red} =3D %= F{9} 3. \033[41m =3D red background =3D $bg[red] =3D %K{red} =3D %K{1} 4. \x1b[38;2;255;0;0m =3D red foreground using 24-bit (truecolor) RGB 5. \x1b[48;2;255;0;0m =3D red background using 24-bit (truecolor) RGB 6. \x1b[38;5;196m =3D red foreground using the terminal's 256 color palette =3D %F{196} 7. \e[0m =3D reset all foreground/background/bold/italic/underline/blinking =3D $colors[none] 8. \e[39m =3D reset foreground color to default =3D $fg[default] =3D %f 9. \e[49m =3D reset background color to default =3D $bg[default] =3D %k A rainbow of resources are available for the color curious: - README_FIRST.txt: https://en.wikipedia.org/wiki/ANSI_escape_code - zshcontrib(1) "Other Functions" - https://zsh.sourceforge.io/Doc/Release/User-Contributions.html#Other-Functi= ons - zshmisc(1) "Expansion of Prompt Sequences: Visual Effects" - https://zsh.sourceforge.io/Doc/Release/Prompt-Expansion.html#Visual-effects - zshzle(1) "Character Highlighting" - https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html#Character-Highl= ighting - zshmodules(1) "zsh/nearcolor module" - https://zsh.sourceforge.io/Doc/Release/Zsh-Modules.html#The-zsh_002fnearcol= or-Module - https://github.com/termstandard/colors - https://terminalguide.namepad.de/ Rick Thanks, Rick On Sat, May 11, 2024 at 5:19=E2=80=AFPM Kannan Varadhan wrote: > > > On 5/11/24 07:37, Roman Perepelitsa wrote: > > On Sat, May 11, 2024 at 4:27=E2=80=AFPM Mikael Magnusson wrote: > > On Sat, May 11, 2024 at 2:54=E2=80=AFAM Kannan Varadhan wrote: > > ~=E2=A6=92printf '%s.%s.%s\n' "${(%):-%F{blue}%B}" "test" "${(%):-%b}${(%= ):-%f}" > %B}.test. > > You probably have a badly made / cargo culted precmd() function > active. > > I am sorry, I did not follow this. > > That output is actually expected. The right curly must be escaped. > > printf '%s.%s.%s\n' "${(%):-%F{blue\}%B}" "test" "${(%):-%b}${(%):-%f= }" > > Yes, this works, > > Is this something that I missed in the documentation? > > Many useful, useable variants, thank you for these. > > However, it's easier to use `print -P`: > > print -P '%F{blue}%Btest%b%f' > > Or, when printing $var: > > print -rP '%F{blue}%B'${var//\%/%%}'%b%f' > > Alternatively: > > print -Pn '%F{blue}%B' > print -rn -- $var > print -P '%b%f' > > Another alternative: > > print -r -- ${(%):-'%F{blue}%B'${var//\%/%%}'%b%f'} > > > Kannan