Bash在用户输入时格式化80行

Bash在用户输入时格式化80行,bash,Bash,我想知道是否有可能确保description或srcohflp超过80行文本,它将换行并转到下一行,以相同的格式创建一个*。我正在查看折叠,但不知道是否可以重新格式化,以便显示带有*的换行符。 例如: /* * Filename: BOB * Author: Whatever * Description: Hi lets just pretend this is over 80 character * and let this continue like thi

我想知道是否有可能确保description或srcohflp超过80行文本,它将换行并转到下一行,以相同的格式创建一个
*
。我正在查看折叠,但不知道是否可以重新格式化,以便显示带有
*
的换行符。 例如:

/*
 * Filename: BOB
 * Author: Whatever
 * Description: Hi lets just pretend this is over 80 character
 *              and let this continue like this
 */
-


这里有一个小功能可能会有所帮助,或者至少可以为您指明正确的方向:

#!/bin/bash

author='whatever'
description='Hello lets just pretend this is over 80 character and let this continue like this blah blah blah blah blah blah blah blah fdjsklf fsdf a f dsg a g asg sg g asgd a sdg sggsag   g asdg sa g s gd sag  '

function format_description() {
    local start

    fmt -w 60 <<< "$description" | while read line
    do
        if ! ((start++))
        then
            echo " * Description: $line"
        else
            echo " *              $line"
        fi
    done
}

format_description "$description"

这里有一种方法-它不会搜索C风格注释的完整语法(即不是开头
/*
和结尾
*/
),但它可能有用:

$ echo " * this is a somewhat longish string with a prefix" | fmt -10 -p " * "
 * this
 * is a
 * somewhat
 * longish
 * string
 * with a
 * prefix
$

/*在开始和结束时发生了什么?
 * Description: Hello lets just pretend this is over 80 character and let
 *              this continue like this blah blah blah blah blah blah blah
 *              blah fdjsklf fsdf a f dsg a g asg sg g asgd a sdg sggsag
 *              g asdg sa g s gd sag
$ echo " * this is a somewhat longish string with a prefix" | fmt -10 -p " * "
 * this
 * is a
 * somewhat
 * longish
 * string
 * with a
 * prefix
$