1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【shell】terminal内での簡単なテキスト文書装飾の仕組みを整える

Posted at
  • 文書作成において、見出しや重要情報への色や書式の指定は大事となっている。
  • その際にWord等のツールを利用するのは少し手間。
  • そのため今回はterminalでテキストファイルの簡単な色置換処理を行い表示する方法を記録する。

環境

  • Mac OS X 10.15.6
  • GNU bash, version 5.1.4(1)-release

結果

  • 以下2つの対象のファイルを用意する。

    • target.txt
    • color.sh
  • 対象ファイルの記述を以下のようにする。

target.txt
私は${red}t_o_d${reset}です。

${bold}※todではありません${reset}

よろしくお願いいたします。
color.sh
# !/bin/sh

# リセット
export \
	reset="\e[0m"
# 書式
export \
	bold="\e[1m" \
	small="\e[2m" \
	italic="\e[3m" \
	underline="\e[4m" \
	hide="\e[8m"
# 色
export \
	black="\e[30m" \
	red="\e[31m" \
	green="\e[32m" \
	blue="\e[34m" \
	purple="\e[35m"

changeColor(){
	eval "declare c=\"$1\""
	printf '%b' "$c"
}

while IFS= read -r line;do
	l=$(changeColor "$line")
	printf '%s\n' "$l"
done <<< "$(<"$1")"
  • 実行結果は以下。

test.png

補足

テキストファイル内の記述

  • 対象の文章をscript内のexportされた変数で囲う形で記述する。
  • 記述例は以下。
    • 太字 : ${bold}対象${reset}
    • 下線 : ${underline}対象${reset}
    • 最後に${reset}で閉じる。

まとめ

  • terminal内で簡単な装飾が行える文書作成の仕組みを整えられたので、きちんと活用。

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?