跳转至内容

Conky/技巧与窍门

来自 ArchWiki

透明度

Conky支持两种不同的透明度类型。伪透明和真透明,后者需要安装并运行一个合成器。如果您启用了真透明但没有运行合成器,您的conky将不会具有真透明度,并且字体、图像以及背景的透明度也将被禁用。

伪透明

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

~/.xinitrc

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

启用真透明

要启用真透明,您必须运行一个合成器,并将以下行添加到conky.config数组内的.conkyrc文件中:

 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}可以显示软件包的总数。

跟踪文本文件

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

显示天气预报

这是通过读取外部网页实现的,通常是专门用于天气预报的网页。请参阅此讨论串。另一个lua天气脚本:此处

显示倒计时器

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

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

显示RSS订阅源

Conky原生支持显示RSS订阅源,无需外部脚本运行并输出到Conky。例如,要显示Planet Arch更新的十个最新标题,并每分钟刷新一次订阅源,您可以在TEXT部分将以下内容放入您的conky.conf文件中:

${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

如果您使用2因素身份验证,您需要使用应用密码

对于方法一、二和三:

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

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

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

方法一

此脚本使用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))

方法二

与方法一相同,但进行了正确的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))

方法三

使用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),可以获得许多其他信息。只需查看PHP关于IMAP的文档:https://php.ac.cn/manual/ref.imap.php

显示活动网络接口的图形

要测试网络接口当前是否活动,可以使用conky的测试变量if_existing,针对接口的operstate。以下是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支持的示例环形脚本 - gist

© . This site is unofficial and not affiliated with Arch Linux.

Content is available under GNU Free Documentation License 1.3 or later unless otherwise noted.