前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tput应用:简易时钟实现

tput应用:简易时钟实现

作者头像
PedroQin
发布2020-07-13 17:46:10
8030
发布2020-07-13 17:46:10
举报
文章被收录于专栏:WriteSimpleDemoWriteSimpleDemo

github

https://github.com/pedroqin/clock_by_tput

目的

通过简易时钟的实现,熟悉tput各功能参数

实现

主要实现功能(文末视频演示)

  • 时钟功能(字符画显示)
  • 秒级进度显示
  • 跟随窗口大小变化

主要实现代码

代码语言:javascript
复制
#!/bin/bash

whereami=`cd $(dirname $0);pwd`

# 使用已完成的脚本工具实现时间的字符画转换
Ascii_Signature_dir="$whereami/Ascii_Signature/"
Ascii_Signature="$Ascii_Signature_dir/ascii_signature.sh -n -s "
# source dictionary
. $Ascii_Signature_dir/font/doom

......

# 获取窗口大小
function terminal_size()
{ 
    terminal_cols="$(tput cols)"
    terminal_rows="$(tput lines)"
}

# 获取字符画大小
function ASCII_Art_size() 
{
    ......
}
# 显示时钟
function display_clock()
{
    ......
        # 光标位置控制
        tput cup $row $clock_col
    ......
}
# 初始化时钟
function init_clock()
{
    ......
    # 获取终端属性是否支持 bce
    if tput bce; then 
        clear
    else # Do it the hard way
        # 控制光标到开头左侧
        tput home
        echo -n "$blank_screen"
    fi
}

function display()
{
    ......
    init_clock

    while true; do
        # 控制光标到指定位置
        tput cup $clock_row $clock_col
        display_clock
        # 显示 秒 进度条背景
        tput cup $progress_row $progress_col
        echo -n ${FG_BLACK}
        echo -n "$second_bar"
        tput cup $progress_row $progress_col
        echo -n ${FG_WHITE}
        ......
        # 获取当前时间后刷新 秒 进度条前景
        while true;do
            # date +%S 秒数,00-59
            i=$(date +%S|bc)
            [ -z "$ii" ] && ii=$i
            # 秒数增加,输出白色 # 覆盖背景深色 #
            [ $i -gt $ii ] && echo -n "#"
            # 59之后归0,跳出while循环进行下次时钟显示
            [ $i -lt $ii ] && unset ii && break
            sleep $refresh_rate
            ii=$i
            # 获取 WINCH 信号后,停止当前显示(退出当前函数)
            [ $IF_WINCH -ne 0 ] && return
        done
    done 
}

# 接收到 SIGINT 信号后,关闭所有样式,恢复指针到正常状态,恢复屏幕内容
trap 'tput sgr0; tput cnorm; tput rmcup || clear; exit 0' SIGINT
# 接收到 WINCH (窗口大小变化)信号,将变量置1
trap 'IF_WINCH=1' WINCH

# 终端前景/背景颜色控制
BG_BLUE="$(tput setab 4)"
FG_BLACK="$(tput setaf 0)"
FG_WHITE="$(tput setaf 7)"

# 保存当前屏幕内容,隐藏指针
tput smcup; tput civis

# 主循环
ASCII_Art_size
while true; do
    start_flag=0
    IF_WINCH=0
    display
done

知识点

获取终端属性

Capname

Description

longname

Full name of the terminal type

lines

Number of lines in the terminal

cols

Number of columns in the terminal

colors

Number of colors available

控制光标

Capname

Description

sc

Save the cursor position

rc

Restore the cursor position

home

Move the cursor to upper left corner (0,0)

cup <row> <col>

Move the cursor to position row, col

cud1

Move the cursor down 1 line

cuu1

Move the cursor up 1 line

civis

Set to cursor to be invisible

cnorm

Set the cursor to its normal state

文本样式

Capname

Description

bold

Start bold text

smul

Start underlined text

rmul

End underlined text

rev

Start reverse video

blink

Start blinking text

invis

Start invisible text

smso

Start "standout" mode

rmso

End "standout" mode

sgr0

Turn off all attributes

setaf <value>

Set foreground color

setab <value>

Set background color

文本颜色

Value

Color

0

Black

1

Red

2

Green

3

Yellow

4

Blue

5

Magenta

6

Cyan

7

White

8

Not used

9

Reset to default color

清屏

Capname

Description

smcup

Save screen contents

rmcup

Restore screen contents

el

Clear from the cursor to the end of the line

el1

Clear from the cursor to the beginning of the line

ed

Clear from the cursor to the end of the screen

clear

Clear the entire screen and home the cursor

视频演示

参考链接

man 1 tput man 5 terminfo tput(http://linuxcommand.org/lc3_adv_tput.php)

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 WriteSimpleDemo 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • github
  • 目的
  • 实现
    • 主要实现功能(文末视频演示)
      • 主要实现代码
      • 知识点
        • 获取终端属性
          • 控制光标
            • 文本样式
              • 文本颜色
                • 清屏
                • 视频演示
                • 参考链接
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档