【问题标题】:How to count symbol instead of string length with Fish?如何用 Fish 计算符号而不是字符串长度?
【发布时间】:2019-05-09 00:29:53
【问题描述】:

pull request for pure theme for fish 的讨论中,我们想知道如何计算符号数而不是字符串长度。

这样做将允许以更优雅/可维护/可测试的方式重构我们的代码。

❯ set -l down_arrow "⇣"
❯ string length $down_arrow
1

❯ set -l down_arrow_red (set_color red)"⇣"(set_color normal)
❯ string length $down_arrow_red
15

问题

在第二个字符串中,我们得到了15,但我们关心的是我们得到了一个字形(例如),而不是所有的着色语法。

我们怎样才能用鱼来做到这一点?

【问题讨论】:

    标签: string fish ansi-colors


    【解决方案1】:

    一个可能的solution using sed 是通过这个命令来传递我们的变量:

    sed 's/\x1b\[[0-9;]*m//g'
    

    作为

    ❯ echo $user_and_host | sed 's/\x1b\[[0-9;]*m//g' | string length
    11
    

    测试

    ❯ set -l user (set_color red)root
    ❯ set -l at (set_color grey)@
    ❯ set -l host (set_color blue)remote
    ❯ set user_and_host "$user$at$host"
    
    ❯ echo $user_and_host
    root@remote  
    ❯ string length $user_and_host
    26  # should be 11
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-14
      • 2017-12-05
      • 1970-01-01
      • 2019-02-13
      • 1970-01-01
      相关资源
      最近更新 更多