英文:
Changing colour at column number
问题
我有一个多行字符串,要求每行的开头使用蓝色前景色,而每行的其余部分使用白色前景色。用户将定义一个列数,从该列数开始,每行的前景色将变为白色。
然后,我将调用我的函数
marinex-ndic 23 "$str"
从列号 23 开始,文本颜色从蓝色变为白色。
marinex-ndic ()
 {
  ## 显示带颜色的标签
  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}" "$vla" "${wht}" "$vlb" "${rst}"
      done
 }
英文:
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 
$1is a positive integer 
答案2
得分: 0
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
英文:
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    数字-white
 marinex-ltrul-blue    字母-white
 marinex-nmrul-blue    数字-white"
marinex-ndic 23 "$str"
<details>
<summary>英文:</summary>
You can also use cut & paste:
#!/bin/bash
marinex-ndic () {
local -r col="$1"
local -r str="$2"
local -r colp1=$((col+1))
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
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"
</details>
				通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论