Mryqu's Notes


  • 首页

  • 搜索
close

Shell显示彩色文字

时间: 2020-11-26   |   分类: Tool   Linux     |   阅读: 189 字 ~1分钟

昨天学习一下如何使用shell在屏幕显示彩色文字。解决方案有两种:1. 转义字符 2. tput设置文本颜色。
在Linux上这两种方式都正常工作,在FreeBSD上第二种方式不起作用。
代码如下:

#!/bin/ksh

println() {
  printf "%s\n" $*
}

##########################
# Solution 1
##########################

colorsEnabled() {
  if [ $TERM == 'TERM' ]
  then
    return 0
  fi
  return 1
}

printlnColor() {
  c=$1
  shift
  msg=$*;
  colorsEnabled
  if [ $? == 1 ]
  then
    printf "\033[0;%dm%s\033[0m\n" $c "$msg"
  else
    printf "%s\n" "$msg"
  fi
}

# Success
printlnSuccess() {
  printlnColor 32 $*
}

# Warning
printlnWarning() {
  printlnColor 33 $*
}

# Failure
printlnFailure() {
  printlnColor 31 $*
}

# Verbose
printlnVerbose() {
  printlnColor 35 $*
}

# Emphasis
printlnEmphasis() {
  printlnColor 36 $*
}

# Note
printlnNote() {
  printlnColor 37 $*
}

##########################
# Solution 2
##########################

println_color() {
  c=$1
  shift
  msg=$*;
  tput setaf $c
  printf "%s\n" "$msg"
  tput sgr0
}

# Success
println_success() {
  println_color 2 $*
}

# Warning
println_warning() {
  println_color 3 $*
}

# Failure
println_failure() {
  println_color 1 $*
}

# Verbose
println_verbose() {
  println_color 5 $*
}

# Emphasis
println_emphasis() {
  println_color 6 $*
}

# Note
println_note() {
  println_color 7 $*
}


printlnEmphasis hahaha 123
echo "====================="
println_emphasis hahaha 123

参考

  • ANSI Escape sequences
  • Git shell coloring · GitHub
  • tput

标题:Shell显示彩色文字
作者:mryqu
声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!

#korn# #shell# #color#
sed正则表达式捕获组实践
React-Redux Action链式调用
  • 文章目录
  • 站点概览

Programmer & Architect

662 日志
27 分类
1472 标签
GitHub Twitter FB Page
    • 参考
© 2009 - 2023 Mryqu's Notes
Powered by - Hugo v0.120.4
Theme by - NexT
0%