Conky/技巧与提示

出自 ArchWiki

透明度

Conky 支持两种不同类型的透明度:伪透明和真透明。真透明需要安装并运行合成管理器。如果您启用真透明但没有运行合成管理器,则 Conky 将不会是 Alpha 透明的,字体、图像以及背景的透明度都将无法启用。

伪透明

Conky 默认启用伪透明。伪透明的工作原理是从根窗口复制背景图像,并将相关部分用作 Conky 的背景。某些窗口管理器将背景壁纸设置在高于根窗口的级别,这可能导致 Conky 出现灰色背景。要解决此问题,您需要手动设置。以下是使用 feh 的示例:

~/.xinitrc

sleep 1 && feh --bg-center ~/background.png &

启用真透明

要启用真透明,您必须运行合成管理器,并将以下行添加到 .conkyrc 的 conky.config 数组中

 conky.config = {
    own_window = true,
    own_window_transparent = true,
    own_window_argb_visual = true,
    own_window_type = 'desktop',
 }

如果窗口类型 “desktop” 不起作用,请尝试将其更改为 normal。如果仍然不起作用,请尝试其他选项:dockpaneloverride

注意: Xfce 需要启用合成,请参阅 [1]

半透明

要在真透明模式下实现半透明,必须在 Conky 配置文件中使用以下设置

 conky.config = {
    own_window = true,
    own_window_transparent = false,
    own_window_argb_visual = true,
    own_window_argb_value = 90,
    own_window_type = 'desktop',
 }

要降低 Conky 窗口的透明度,可以将 own_window_argb_value 的值增加到 255。

显示软件包更新信息

pacman-contrib 提供了一个名为 checkupdates 的脚本,用于显示来自官方仓库的软件包更新。使用 ${execi 3600 checkupdates | wc -l} 来显示软件包总数。

tail 文本文件

conky 能够将 conky(1) § tail 文件输出到您的桌面,这对于文本文件非常有用。例如,读取 /var/log/ 文件以显示各种日志消息。这些文件中的大多数只能由 root 读取,但不建议以 root 身份运行 conky,因此您需要将自己添加到 log 用户组。此外,由于 systemd 日志文件是二进制文件,因此此功能不如以前那么有用。但是,可以使用 lua 脚本来实现。

显示天气预报

这可以通过读取外部网页来实现,通常是专门用于天气预报的页面。请参阅此帖。另一个 Lua 天气脚本:这里

显示倒计时器

ConkyTimer 是一个简单的倒计时器,用于显示已定义任务的剩余时间。

使用 conkytimer "<任务描述>" <分钟> 启动计时器。

显示 RSS 订阅

Conky 具有原生显示 RSS 订阅的功能,无需外部脚本运行并输出到 Conky 中。例如,要显示最近十个 Planet Arch 更新的标题并每分钟刷新订阅,您可以将以下内容放入 conky.confTEXT 部分

${rss https://planet.archlinux.org/rss20.xml 300 item_titles 10 }

如果您想显示 Arch 论坛 RSS 订阅,请添加此行

${rss https://bbs.archlinux.org/extern.php?action=feed&type=rss 300 item_titles 4}

其中 300 是刷新间隔(以秒为单位,默认为 15 分钟),4 是您希望显示的条目数。

显示当月日历

您可以使用以下 Lua 脚本来显示日历。它使用 color1 和您配置中的默认颜色。使用等宽字体效果最佳。

 #!/usr/bin/env lua
 
 conky_color = "${color1}%2d${color}"
 
 t = os.date('*t', os.time())
 year, month, currentday = t.year, t.month, t.day
 
 daystart = os.date("*t",os.time{year=year,month=month,day=01}).wday
 
 month_name = os.date("%B")
 
 days_in_month = {
     31, 28, 31, 30, 31, 30, 
     31, 31, 30, 31, 30, 31
 }
 
 -- check for leap year
 -- Any year that is evenly divisible by 4 is a leap year
 -- Any year that is evenly divisible by 100 is a leap year if
 -- it is also evenly divisible by 400.
 LeapYear = function (year)
     return year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
 end
 
 if LeapYear(year) then
     days_in_month[2] = 29
 end
 
 title_start = (20 - (string.len(month_name) + 5)) / 2
 
 title = string.rep(" ", math.floor(title_start+0.5)) .. -- add padding to center the title
         (" %s %s\n Su Mo Tu We Th Fr Sa\n"):format(month_name, year)
 
 io.write(title)
 
 function seq(a,b)
     if a > b then
         return
     else
         return a, seq(a+1,b)
     end 
 end
 
 days = days_in_month[month]
 
 io.write(
     string.format(
         string.rep("   ", daystart-1) ..
         string.rep(" %2d", days), seq(1,days)
     ):gsub(string.rep(".",21),"%0\n")
      :gsub(("%2d"):format(currentday),
            (conky_color):format(currentday)
      ) .. "\n"
 )

在您的 conky.conf 中,您可以放置以下内容,确保路径与您保存脚本的位置匹配。

 conky.text = [[
 ${execpi 3600 ~/.config/conky/cal.lua}
 ]]

显示 rTorrent 统计信息

请参阅此帖

显示 WordPress 博客统计信息

这可以通过使用名为 ConkyPress 的 Python 编写的扩展来实现。

显示新邮件数量

Conky 内置支持 IMAP 和 POP3,但不支持通过 SSL 访问。Conky 的 FAQ 建议为此使用 stunnel,并在此处提供了一个示例配置 这里

按如下方式修改 /etc/stunnel/stunnel.conf,然后启动 stunnel.service

  1. TLS 服务器的服务级别配置
[imap]
client = yes
accept  = 143
connect = imap.gmail.com:143
protocol = imap
sslVersion = TLSv1
# Service-level configuration for SSL server
[imaps]
client = yes
accept  = 993
connect = imap.gmail.com:993

然后将以下内容添加到 conky.conf

conky.config = {
    imap = "localhost username password [-i 120] [-f 'inbox'] [-p 993]",
}
conky.text {
    Inbox: ${imap_unseen}/${imap_messages}
}

Gmail

如果您使用两步验证,则需要使用应用密码

对于方法 1、2 和 3

在方便的位置(例如在 ~/.scripts/ 中)创建以下文件之一。

然后将以下字符串添加到您的 conky.conf 中,以便每五分钟(300 秒)检查您的 Gmail 帐户是否有新邮件并显示

${execi 300 python ~/.scripts/gmail.py}

方法 1

此脚本使用 Gmail 的 Atom API 检索新邮件的数量。

gmail.py
#!/usr/bin/env python3

import urllib.request

email = 'your email'
password = 'your password'

# Set up authentication for gmail
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm='mail.google.com',
                          uri='https://mail.google.com/',
                          user=email,
                          passwd=password)
opener = urllib.request.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib.request.install_opener(opener)

gmailurl = 'https://mail.google.com/gmail/feed/atom'
with urllib.request.urlopen(gmailurl) as page:
    contents = page.read().decode('utf-8')

ifrom = contents.index('<fullcount>') + 11
ito   = contents.index('</fullcount>')

fullcount = contents[ifrom:ito]

print('{} new emails'.format(fullcount))

方法 2

与方法 1 相同,但执行正确的 XML 解析。

gmail.py
#!/usr/bin/env python3

import urllib.request
from xml.etree import ElementTree as etree

email = 'your email'
password = 'your password'

# Set up authentication for gmail
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm='mail.google.com',
                          uri='https://mail.google.com/',
                          user=email,
                          passwd=password)
opener = urllib.request.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib.request.install_opener(opener)

gmailurl = 'https://mail.google.com/gmail/feed/atom'
NS = '{http://purl.org/atom/ns#}'
with urllib.request.urlopen(gmailurl) as source:
    tree = etree.parse(source)
fullcount = tree.find(NS + 'fullcount').text

print('{} new emails'.format(fullcount))

方法 3

相同的方式,但使用 curlgrepsed

gmail.sh
#!/usr/bin/sh

curl -s -u email:password https://mail.google.com/mail/feed/atom | grep fullcount | sed 's/<[^0-9]*>//g'

emailpassword 替换为您的数据。

使用 Perl 的 IMAP + SSL

Conky 内置支持 IMAP 帐户,但不支持 SSL。这可以使用来自 此论坛帖子 的脚本来提供。这需要 Perl/CPAN 模块 Mail::IMAPClient 和 IO::Socket::SSL,它们分别在 perl-mail-imapclientperl-io-socket-ssl 软件包中

conky 可以读取的位置(例如在 ~/.scripts/ 中)创建一个名为 imap.pl 的文件。在此文件中,添加(进行适当的更改)

imap.pl
#!/usr/bin/perl
 
# by gxmsgx
# description: get the count of unread messages on imap

use strict;
use Mail::IMAPClient;
use IO::Socket::SSL;

my $username = 'example.username'; 
my $password = 'password123'; 

my $socket = IO::Socket::SSL->new(
  PeerAddr => 'imap.server',
  PeerPort => 993
 )
 or die "socket(): $@";
 
my $client = Mail::IMAPClient->new(
  Socket   => $socket,
  User     => $username,
  Password => $password,
 )
 or die "new(): $@";
 
if ($client->IsAuthenticated()) {
  my $msgct;

  $client->select("INBOX");
  $msgct = $client->unseen_count||'0';
  print "$msgct\n";
}

$client->logout();

添加到 conky.conf

${execi 300 ~/.scripts/imap.pl} 

或者您保存文件的任何位置。

如果您使用 Gmail,您可能需要生成一个应用程序专用密码。

或者,您可以像上面所示使用 stunnel:#Gmail

使用 PHP 的 IMAP

另一种使用 PHP 的替代方案。需要安装 PHP,并且必须在 /etc/php/php.ini 中取消注释 extension=imap

然后在 conky 可以读取的位置(例如在 ~/.scripts/ 中)创建一个名为 imap.php 的文件。使该文件可执行

在此文件中,添加(进行适当的更改)

imap.php
#!/usr/bin/php
<?php
// See https://php.ac.cn/manual/function.imap-open.php for more information about
// the mailbox string in the first parameter of imap_open.
// This example is ready to use with Office 365 Exchange Mails,
// just replace your username (=email address) and the password.
$mbox = imap_open("{outlook.office365.com:993/imap/ssl/novalidate-cert}", "username", "password");

// Total number of emails
$nrTotal = imap_num_msg($mbox);
 
// Number of unseen emails. There are other ways using imap_status to count
// unseen messages, but they don't work with Office 365 Exchange. This one does.
$unseen = imap_search($mbox, 'UNSEEN');
$nrUnseen = $unseen ? count($unseen) : 0;
 
// Display the result, format as you like.
echo $nrUnseen.'/'.$nrTotal;
 
// Not needed, because the connection is closed after the script end.
// For the sake of clean public available scripts, we are nice to
// the imap server and close the connection manually.
imap_close($mbox);

添加到 conky.conf

${execi 300 ~/.scripts/imap.php} 

或者您保存文件的任何位置。

此脚本显示 A/B,其中 A 是未见邮件的数量,B 是邮箱中邮件的总数。还有很多其他信息可以通过许多 PHP 函数获得,例如 imap_Status (https://php.ac.cn/manual/function.imap-status.php)。只需查看有关 IMAP 的 PHP 文档:https://php.ac.cn/manual/ref.imap.php

显示活动网络接口图形

要测试网络接口当前是否处于活动状态,您可以在接口的 operstate 上使用测试 Conky 变量 if_existing。以下是 wlo1 的示例 

draw_graph_borders yes 
${if_existing /sys/class/net/wlo1/operstate up}
${color #0077ff}Net Down:$color ${downspeed wlo1}      ${color #0077ff}Net Up:$color ${upspeed wlo1}
${color #0077ff}${downspeedgraph wlo1 32,155 104E8B 0077ff} $alignr${color #0077ff}${upspeedgraph wlo1 32,155 104E8B 0077ff}
${endif}

这是预期结果 

https://i.imgur.com/pQQbsP6.png

用户贡献的配置示例

  • 带有 NVIDIA 支持的示例 rings 脚本 - gist