本文介绍怎么设置 MacOS 体系的终端发动音讯,或者说欢迎音讯。

本文介绍的内容相同适用于其他类 Unix 体系。

某种意义上,这是一个无用的小常识,但它确实很有趣。究竟,不是任何事情都要寻求所谓价值,有趣也挺重要的。

登录音讯

每天翻开 terminal 终端,体系默许会打印一串的音讯,如 “Last Login xxx” 之类的音讯。是否想过让这个默许音讯愈加丰厚一些?

如 MacOS 这样的类 Unix 体系默许有两种方法,一种是根据体系的 motd,另一种是经过发动脚本打印音讯。

系列阅览:

更多待续…

motd

首要,经过 motd 的完结计划。motd,即 message of today,类 Unix 体系基本都支撑,咱们只要将发动音讯写入到 /etc/motd中,即可定制终端发动音讯。

关于公用服务器,体系管理员用它可向普通用户发送音讯。而且,有部分 Linux 发行版乃至用它的默许装备给用户发送广告。

演示看作用吧。

将文字 “Hello World. Have a nice day!” 写入 /etc/motd 中,开启一个新的终端。

motd 的缺陷是它是静态内容,假如要更新内容,则需修正文件。

motd 的动态作用完结计划,可根据 crontab 定时更新 motd 中的内容完结 motd 伪动态。不过,这种方法主要是合适于不停机的服务器,关于个人电脑,经常处于关机状况,或许不太合适。

发动脚本

完结真实意义上的动态 motd 音讯,可直接经过体系发动脚本打印环境音讯,如在 ~/.zshrc 中增加欢迎音讯打印脚本,音讯内容可任意定制。

如 MacOS 体系在发动时,经过解析 uname -a 输出内容,打印体系信息和内核版本:

echo `uname -a | cut -d' ' -f1 -f3`

加入到 ~/.zshrc 文件中,作用如下所示:

到这里,终端欢迎音讯定制的基本内容介绍结束。但问题是,这显着不够冷艳,关于这种无用小常识,肯定要 “华而不实”,但现在没有看到任何冷艳之处。

To be continued…

ASCII art 定制欢迎音讯

文字单一枯燥,怎么给欢迎音讯引进不一样的内容?咱们将引进 ASCII art 改造枯燥的发动罅隙。

所谓 ASCII art,是经过 ASCII 字符表达图片的一种方法。在文字比图画更安稳的场合,如终端、或者早上没有图画显示能力的设备,ASCII art 显然是更好的选择。

示例如下:

 _          _ _                            _     _
| |__   ___| | | ___   __      _____  _ __| | __| |
| '_  / _  | |/ _     / / / _ | '__| |/ _` |
| | | |  __/ | | (_) |   V  V / (_) | |  | | (_| |
|_| |_|___|_|_|___/    _/_/ ___/|_|  |_|__,_|
 __________________
< have a nice day! >
 ------------------
           ^__^
           (oo)_______
            (__)       )/
                ||----w |
                ||     ||

{{< image “./2023-11-15-beautify-your-terminal-welcome-message-03.png” >}}

我的终端环境:终端发动音讯 - ASCII art

怎么生成 ASCII art?

ASCII art 一般有 ASCII text 和 ASCII image 两种形式。可经过在线站点生成,或经过一些指令生成。

在线网站

引荐两个东西站点,ASCII GeneratorASCII banner

演示作用,运用 ASCII Generator 生成 ASCII 3D 文字,如下所示:

我的终端环境:终端发动音讯 - ASCII art

如想找一些现成的 ASCII art,检查 ascii-art,其中有一些现成的可供选择。

指令东西

举一反三,东西站点能生成 ASCII art,那必定经过指令也能够做到,必须都是程序完结。

MacOS 一键装置:

brew install figlet cowsay fortune lolcat

figlet 生成 ASCII text

figlet,可用于 ASCII text。它常用于开源项目生成 text banner。

运用演示:

> figlet hell world
 _          _ _                            _     _
| |__   ___| | | ___   __      _____  _ __| | __| |
| '_  / _  | |/ _     / / / _ | '__| |/ _` |
| | | |  __/ | | (_) |   V  V / (_) | |  | | (_| |
|_| |_|___|_|_|___/    _/_/ ___/|_|  |_|__,_|

fortune cowsay 牛说名言

关于 ASCII art 内容,还有常用的一个组合指令:fortune cowsay。

fortune 用于生成随机生成 “英文名言”。

❯ fortune
Things will get better despite our efforts to improve them.
                -- Will Rogers

假如希望有中文内容,可装置阮一峰老师供给的 fortune 中文数据库

cowsay,即 “牛说” 的意思,合作 fortune,会生成一个 “牛说某个格言” 作用。

❯ fortune | cowsay
 ________________________________________
/ Shame is an improper emotion invented  
| by pietists to oppress the human race. |
|                                        |
| -- Robert Preston, Toddy,              |
 "Victor/Victoria"                      /
 ----------------------------------------
           ^__^
           (oo)_______
            (__)       )/
                ||----w |
                ||     ||

默许的 “牛” 的形象是可修正的,如换成 “羊”,经过 -f 修正。

指令如下:

❯ fortune | cowsay -f sheep
 ________________________________________
/ Winny and I lived in a house that ran  
| on static electricity... If you wanted |
| to run the blender, you had to rub     |
| balloons on your head... if you wanted |
| to cook, you had to pull off a sweater |
| real quick...                          |
|                                        |
 -- Steven Wright                       /
 ----------------------------------------
  
   
       __
      UooU.'@@@@@@`.
      __/(@@@@@@@@@@)
           (@@@@@@@@)
           `YY~~~~YY'
            ||    ||

要检查支撑的一切形象,经过如下指令:

> cowsay -l
Cow files in /usr/local/Cellar/cowsay/3.04_1/share/cows:
beavis.zen blowfish bong bud-frogs bunny cheese cower daemon default dragon
dragon-and-cow elephant elephant-in-snake eyes flaming-sheep ghostbusters
head-in hellokitty kiss kitty koala kosh luke-koala meow milk moofasa moose
mutilated ren satanic sheep skeleton small stegosaurus stimpy supermilker
surgery three-eyes turkey turtle tux udder vader vader-koala www

假如想定制更多形象,可参阅上面目录 /usr/local/Cellar/cowsay/3.04_1/share/cows 中的案例,经过 -f 执行也是能够的。

lolcat – 随机彩虹上色

lolcat,可用于给输出进行随机的彩虹上色,如给以上输出 ASCII 文字上色,可经过 lolcat 完结随机彩虹作用,作用如下:

或给 cowsay 的输出上色,作用如下:

程序完结

假如想经过编程完结文字转 ASCII 或图片也是能够做到的。当然,其实也没啥技术含量,依靠的 Python 的强壮的三方库支撑。

文字转 ASCII,可经过 pyfiglet 库完结,而图片能够来 pillow 库完结。

pip install pyfiglet
pip install pillow
pip install click

完好代码的 gist 地址ascii-generator。张贴一份完好代码,如下所示:

import sys
import click
import pyfiglet
import PIL.Image
def image_to_ascii(path):
    try:
        img = PIL.Image.open(path)
    except:
        print(path, "Unable to find image")
        exit(1)
    width, height = img.size
    aspect_ratio = height / width
    new_width = 50
    new_height = aspect_ratio * new_width * 0.55
    img = img.resize((new_width, int(new_height)))
    img = img.convert("L")
    chars = [" ", "J", "D", "%", "*", "P", " ", "Y", "$", ",", " "]
    pixels = img.getdata()
    new_pixels = [chars[pixel // 25] for pixel in pixels]
    new_pixels = "".join(new_pixels)
    new_pixels_count = len(new_pixels)
    ascii_image = [
        new_pixels[index : index   new_width]
        for index in range(0, new_pixels_count, new_width)
    ]
    return "n".join(ascii_image)
def text_to_ascii(text):
    return pyfiglet.figlet_format(text)
@click.command()
@click.option(
    "--type",
    "type_",
    default="text",
    help="source type, unavailable options: text, image",
)
@click.option("--source", help="text or image path")
def main(type_, source):
    if type_ == "text":
        ascii = text_to_ascii(source)
    elif type_ == "image":
        ascii = image_to_ascii(source)
    else:
        return
    print(ascii)
if __name__ == "__main__":
    main()

其中的参数如宽度还有字符,都是可调节的,运用起来也都十分简略。

文字转 ASCII:

> python ascii_image.py --type text --source "hello world"
 _          _ _                            _     _ 
| |__   ___| | | ___   __      _____  _ __| | __| |
| '_  / _  | |/ _     / / / _ | '__| |/ _` |
| | | |  __/ | | (_) |   V  V / (_) | |  | | (_| |
|_| |_|___|_|_|___/    _/_/ ___/|_|  |_|__,_|

图片转 ASCII:

我的终端环境:终端发动音讯 - ASCII art

完结设置

静态内容,现在只要将生成的 ASCII art 写入到 motd 文件。假如希望静态文本中的输出保持色彩,可经过 lolcat -f 将色彩信息也导入到文本中。

动态内容,将类似于 fortune | cowsay | locat 加入到发动脚本 ~/.zshrc 即可。

到此,开始大功搞成。

总结

经过本文,我的古怪常识又增加了。对了,文中的 ASCII image 的原图是我的博客头像。

最终,下期介绍怎么在欢迎音讯中展示更丰厚的信息,如:

我的终端环境:终端发动音讯 - ASCII art

我的博文:我的终端环境:无用小常识 – 终端发动音讯装备