From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 17358 invoked from network); 24 Jul 2021 10:10:01 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 24 Jul 2021 10:10:01 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20200801; t=1627121401; b=Z+JlmvWKWJZXOYoKZhaAVfaxqBUBNLR7VZWpZ/Xv4WTkSqLpuNPOgDknkf/MiPK/ogrIe3Ag/C 007ufjv0Zb7bp8GuBuAbOi8lmgfyH4hfrF0OzZrksgP/mek2av44zjxhb017QBHiuAQX6lXECo JT1HVXi4TWXkvlM823lLm2XIFYOF+fAEHOyIwLMh/ZHJ7PF/srwjoWdqJjhgMhzdYlJjeIp1cc 7lR8OCe8z0g+/UhAvqupjlhIOdkYzHG+v83bNd94T1RMXh/hWGYw8RGGlQxhNuu3uyYy6XoNoN 3FcUKGmhHH4D41fi3LbXCiikT1SHjZs1D7AvWp6XRwX1Cw==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-vs1-f45.google.com) smtp.remote-ip=209.85.217.45; dkim=pass header.d=gmail.com header.s=20161025 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-20200801; t=1627121401; bh=ldQjao1Otl0+GHmx57ERmXpTq/0cq4kQyi81ENIZb+E=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID:Date:From:References: In-Reply-To:MIME-Version:DKIM-Signature:DKIM-Signature; b=BrH5fEigRYoKckBtNWpvZeLIE0KW6czBBxnv9NFQcytDTvjqwKu5PV8x3rELffGOiLL67v86lW ISU8kCkKifZgYXCE/4b0cf0M60mxasmw+aU7DgRasImMFxDgN6icv/lXgge+7eAxjnYHwP+xS+ +xD6kMnEvLUEROE0TiYlIziI+TJo21hVT58w1awyYSAIIb0bBH0Xi0WRwR+EnwbpwENDjZ2YaO jedKVfUwL++mTSO0XkRhGSwkfOYbTnhP81iPStjP/xFnbIGu2Kct14VuDciX5dcnFAU9ueokNj QOR6Id8ZTBYvT5tFB8ekz/JQYtTFKwQYDu31+gC0IidApA==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20200801; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID :Date:From:References:In-Reply-To:MIME-Version:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=WlYpOwockxHhyei1cTbsADuEqX3XkkYpWYjbK6MZgUA=; b=SDF+a+CVjBmSoaNaQFoe19fF1y 8rh2d0r4W/8o9oEhutGJ6R5jc6yT42yn/4k40jW3tm9R5NR5ga/1IFs/zXh219OuVYDx9M8Nltyuk WsxSmx+SxxLEG8tLLk2FHQE4RWI18pQXbb89JLTQxN+3uctrfKBTLdIaukjtRxdMQGUTpVaN3WDh9 PCRJ861Vw0sr6MCyGaVg+dXr9yPR6rhjNSk3bvrt3FoDTpl7MJ9DhfNqp4C8wNeO9X0CBeTjo+kP0 teX4gooKWlXHOYbsmRASBjHtQzwhoxZVQKQ/fe/h/s0Sc8W/IrmIchAmN46w5fniANUyyWmiyNsXM rsSkyDJQ==; Received: from authenticated user by zero.zsh.org with local id 1m7EbY-000IfJ-Eh; Sat, 24 Jul 2021 10:10:00 +0000 Authentication-Results: zsh.org; iprev=pass (mail-vs1-f45.google.com) smtp.remote-ip=209.85.217.45; dkim=pass header.d=gmail.com header.s=20161025 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-vs1-f45.google.com ([209.85.217.45]:46811) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1m7Eab-000HuZ-Eb; Sat, 24 Jul 2021 10:09:02 +0000 Received: by mail-vs1-f45.google.com with SMTP id e4so2428790vsr.13 for ; Sat, 24 Jul 2021 03:09:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=WlYpOwockxHhyei1cTbsADuEqX3XkkYpWYjbK6MZgUA=; b=aTgWhS+KQPLS+1PhdzVZBEEp+4JRe1V4qWLh1sV0LNZzsCZw7nDFQFWk5PGi0hPPq/ s/Q4uyeBn+D1OGUE77ZEAbbySMXA4iuxq2gwF4/QM74Xx2KdZ6reSy9riBwRLxsfOsa3 HIFeO6yFcRpfUBccoygIID0JRm/PSUIf1AOuQFt4ijDyTB9uwPG2e8Fp67zN+C5SxFsB 0+kt3RWUTIQCvqtKXXXJ8RWshF+oYI+RWQ2+AO7U0Slsf+aUZPjtZb+ViXQdL7DyxbtA DxanS2e8Q/KQ5MVw14U1H3WU3EWLdmQtDfjzisfZkNrELHmZR9/Oh3oe3gRVQ4iOmA+Y QDJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=WlYpOwockxHhyei1cTbsADuEqX3XkkYpWYjbK6MZgUA=; b=I+qW2IbQeexRcac1LIXFEd62sGhQ1hOlcVM9iacEfPhmgJRrOtBGbNeIoWD3ZJaNhB ODv9MZyJn2OsC7idGaCctz6LgAweUqdKde3/FyAolaCBbRlJbf7H6hw3V5cUlWoXbClN QMu6nGqMfBwLJx5RLJjMLF6jiwQ02BHKpjPECfcdLPoU2IVZaPTlQnMdJSlWbmE8ABYF cp4r3A/oxgBdENwmhU/bx0f/EYvxhCZjNdqbQgyD1bPvVz8Ur8z1QQ4oVT7jj4OGoV9G t6bBa+htb9wr/8YIRw1W4o3I7pSNmCrm54jJKe1/DVGaCO2C0xbT92qvfuNL+N1SL24Z fcGw== X-Gm-Message-State: AOAM533OGjyTSwUbzTZsUWBirCleqeJzGthkoXhJ2MFgZKEjQbMybgWf btswYFHRedgmIe/6jXc83nOEYY4AJ37NhafDuCQ= X-Google-Smtp-Source: ABdhPJyf9Iv02BtLX1KnEPX+gr8MKQIms/dnbzT2MN5pkEt0oiC4g8VY8TsdVX75v7p/laNYs/uh7Xi0aoNapAvsGE4= X-Received: by 2002:a67:fc4e:: with SMTP id p14mr7627694vsq.16.1627121339943; Sat, 24 Jul 2021 03:08:59 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ab0:1508:0:0:0:0:0 with HTTP; Sat, 24 Jul 2021 03:08:59 -0700 (PDT) In-Reply-To: References: From: Mikael Magnusson Date: Sat, 24 Jul 2021 12:08:59 +0200 Message-ID: Subject: Re: Use of left- and right-padding with a variable padding char To: Zach Riggle Cc: zsh-users@zsh.org Content-Type: text/plain; charset="UTF-8" X-Seq: 26804 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: On 7/24/21, Zach Riggle wrote: > As always, thank you all so much for your time. It's genuinely > appreciated. > > I want to define a function "center", which takes $1="Some String" and > $2="-" or some other default padding value. > > The Zsh manual on expansion says that I can do something like this, > which works well and as intended: > > function center() { > local text="${1:-hellworld}" > local -i columns=${COLUMNS:-$(tput cols)} > columns=$(( columns/2 )) > echo ${(l:${columns}::=:::r:${columns}::=:::)text} > } > > This works very well, and correctly centers "${text}" across the width > of the terminal, with "=" padding on both sides. Perfect. > > However, I also want to make the padding character ("=" in the above > example) be variable. > > The following does NOT work as intended, and I'm not sure why. My > understanding is that variable expansion is inside-out, so it SHOULD > work. > > function center() { > local text="${1:-hellworld}" > local pad="${2:-=}" > local -i columns=${COLUMNS:-$(tput cols)} > columns=$(( columns/2 )) > echo ${(l:${columns}::${pad}:::r:${columns}::${pad}:::)text} > } > center "hello" "_" > > The value in $1 is correctly centered, but it is padded with the > LITERAL value '${pad}', rather than the expansion of ${pad}. > > I expect there's some documentation that I haven't read yet, any tips > would be useful. Add p and drop the braces, as in echo ${(pl:${columns}::$pad:::r:${columns}::$pad:::)text} p Recognize the same escape sequences as the print builtin in string arguments to any of the flags described below that follow this argument. Alternatively, with this option string arguments may be in the form $var in which case the value of the variable is substituted. Note this form is strict; the string argument does not undergo general parameter expansion. -- Mikael Magnusson