改变在列号处的颜色

huangapple go评论51阅读模式
英文:

Changing colour at column number

问题

我有一个多行字符串,要求每行的开头使用蓝色前景,而每行的其余部分使用白色前景。用户需要定义一个从哪一列开始,每行的前景色变为白色。

str="
 marinex-dgrul-blue    Digit-white
 marinex-ltrul-blue    Letter-white
 marinex-nmrul-blue    Numeric-white"

然后我会调用我的函数

marinex-ndic 23 "$str"

从第23列开始,文本颜色从蓝色变为白色。
英文:

I have a multiline string to print the beginning of each line with blue foreground, whereas the rest of each line uses a white foreground. The user would define a column number from which the foreground colour on each line changes to white.

str="
 marinex-dgrul-blue    Digit-white
 marinex-ltrul-blue    Letter-white
 marinex-nmrul-blue    Numeric-white"

Then I would call my function

marinex-ndic 23 "$str"

Starting from column number 23, the text colour gets changed from blue to white.

marinex-ndic ()
 {
  ## Show coloured  and label

  local -r rst="$( tput sgr0 )"  # Default Graphic Rendition
  local -r blu="$( tput bold; tput setaf 39 )"  # BLUE
  local -r wht="$( tput bold; tput setaf 15 )"  # WHITE

  printf '%s\n' "$@"  \
    | while IFS="" read -r vl; do
        printf '%s%s%s%s%s\n' "${blu}" "$vla" "$wht" "$vlb" "${rst}"
      done
 }

答案1

得分: 2

以下是翻译好的部分:

假设当前的代码工作正常(例如,正确打印颜色,将输入行读入vl等)在OP的环境中...

我们可以使用bash子字符串来分割$vl变量:

marinex-ndic ()
 {
  local col="$1"            # 保存列号
  shift                     # 移动输入参数(即,从$@中删除'col')
  
  local -r rst="$( tput sgr0 )"  # 默认的图形修饰
  local -r blu="$( tput bold; tput setaf 39 )"  # 蓝色
  local -r wht="$( tput bold; tput setaf 15 )"  # 白色

  printf '%s\n' "$@"  \
    | while IFS="" read -r vl; do
        printf '%s%s%s%s%s\n' "${blu}" "${vl:0:col}" "$wht" "${vl:col}" "${rst}"
        #                               ^^^^^^^^^^^          ^^^^^^^^^
      done
 }

注意:

  • 这个特定的蓝色颜色代码(blu)在我的环境中不起作用;我假设这些颜色在OP的环境中起作用,所以我没有尝试解决实际的颜色代码问题。
  • OP可以决定是否应该添加额外的逻辑来验证$1是否是正整数。
英文:

Assuming the current code works correctly (eg, prints correct colors, reads input lines into vl, etc) in OP's env ...

We can use bash substrings to split the $vl variable:

marinex-ndic ()
 {
  local col="$1"            # save columnm number
  shift                     # shift input params (ie, remove 'col' from $@)
  
  local -r rst="$( tput sgr0 )"  # Default Graphic Rendition
  local -r blu="$( tput bold; tput setaf 39 )"  # BLUE
  local -r wht="$( tput bold; tput setaf 15 )"  # WHITE

  printf '%s\n' "$@"  \
    | while IFS="" read -r vl; do
        printf '%s%s%s%s%s\n' "${blu}" "${vl:0:col}" "$wht" "${vl:col}" "${rst}"
        #                               ^^^^^^^^^^^          ^^^^^^^^^
      done
 }

NOTES:

  • this particular blue color code (blu) is not working in my env; I'm assuming these colors work in OP's env so I haven't attempted to address actual color codes
  • OP can decide if additional logic should be added to the function to verify $1 is a positive integer

答案2

得分: 0

以下代码片段将帮助您更改特定列数的前景颜色:

printf '%s%s%s%s%s\n' "${blu}" "$vl" "$wht" "$vl" "${rst}" | while IFS="" read -r

英文:

The following code snippet will help you change the foreground colour at a specific column number:

printf '%s%s%s%s%s\n' "${blu}" "$vl" "$wht" "$vl" "${rst}" | while IFS="" read -r

答案3

得分: 0

#!/bin/bash

marinex-ndic () {
  local -r col="$1"
  local -r str="$2"
  local -r colp1=$((col+1))
  local -r rst="$( tput sgr0 )"  # 默认图形修饰
  local -r blu="$( tput bold; tput setaf 39 )"  # 蓝色
  local -r wht="$( tput bold; tput setaf 15 )"  # 白色

  paste <(cut -c-${col} <<<"$str" | sed "s/^/${blu}/;s/$/${rst}/")  <(cut -c${colp1}- <<<"$str" | sed "s/^/${wht}/;s/$/${rst}/")
}

str="
 marinex-dgrul-blue    Digit-white
 marinex-ltrul-blue    Letter-white
 marinex-nmrul-blue    Numeric-white"

marinex-ndic 23 "$str"
英文:

You can also use cut & paste:

#!/bin/bash

marinex-ndic () {
  local -r col=&quot;$1&quot;
  local -r str=&quot;$2&quot;
  local -r colp1=$((col+1))
  local -r rst=&quot;$( tput sgr0 )&quot;  # Default Graphic Rendition
  local -r blu=&quot;$( tput bold; tput setaf 39 )&quot;  # BLUE
  local -r wht=&quot;$( tput bold; tput setaf 15 )&quot;  # WHITE

  paste &lt;(cut -c-${col} &lt;&lt;&lt;&quot;$str&quot; | sed &quot;s/^/${blu}/;s/$/${rst}/&quot;)  &lt;(cut -c${colp1}- &lt;&lt;&lt;&quot;$str&quot; | sed &quot;s/^/${wht}/;s/$/${rst}/&quot;)
}

str=&quot;
 marinex-dgrul-blue    Digit-white
 marinex-ltrul-blue    Letter-white
 marinex-nmrul-blue    Numeric-white&quot;

marinex-ndic 23 &quot;$str&quot;

huangapple
  • 本文由 发表于 2023年3月21日 01:51:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793668-4.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定