AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 473788
Accepted
Matthias Braun
Matthias Braun
Asked: 2018-10-08 06:09:03 +0800 CST2018-10-08 06:09:03 +0800 CST 2018-10-08 06:09:03 +0800 CST

简单的摇杆示例

  • 772

我想为我在 Arch Linux 中使用的Sway提供一个简单、平静的状态栏。

到目前为止,我发现的配置使用单独的程序,例如waybar或i3status。status_command虽然它们看起来很棒,但我想保持简单并man sway-bar直接使用。

最好,此状态栏与i3一样好,这应该是可能的,因为 Sway 旨在使其配置与 i3 兼容。

sway
  • 4 4 个回答
  • 24798 Views

4 个回答

  • Voted
  1. Matthias Braun
    2018-10-08T06:09:03+08:002018-10-08T06:09:03+08:00

    我有这个脚本~/.config/sway/status.sh:

    # The Sway configuration file in ~/.config/sway/config calls this script.
    # You should see changes to the status bar after saving this script.
    # If not, do "killall swaybar" and $mod+Shift+c to reload the configuration.
    
    # Produces "21 days", for example
    uptime_formatted=$(uptime | cut -d ',' -f1  | cut -d ' ' -f4,5)
    
    # The abbreviated weekday (e.g., "Sat"), followed by the ISO-formatted date
    # like 2018-10-06 and the time (e.g., 14:01)
    date_formatted=$(date "+%a %F %H:%M")
    
    # Get the Linux version but remove the "-1-ARCH" part
    linux_version=$(uname -r | cut -d '-' -f1)
    
    # Returns the battery status: "Full", "Discharging", or "Charging".
    battery_status=$(cat /sys/class/power_supply/BAT0/status)
    
    # Emojis and characters for the status bar
    # ? ? ? ? ⚡ ? \|
    echo $uptime_formatted ↑ $linux_version ? $battery_status ? $date_formatted
    

    定义状态栏的部分~/.config/sway/config是这样的:

    bar {
        position top
        # Keep in mind that the current directory of this config file is $HOME
        status_command while ~/.config/sway/status.sh; do sleep 1; done
    
        colors {
            # Text color of status bar
            statusline #ffffff
            # Background of status bar
            background #323232
        }
        font pango:DejaVu Sans Mono 10
    }
    

    这就是使用此配置的栏的外观:

    摇杆

    上述设置也适用于i3,结果相同。

    您需要安装适当的字体来呈现表情符号字符,例如:

    pacman -S noto-fonts-emoji
    

    或者

    apt install fonts-noto-color-emoji
    
    • 14
  2. Best Answer
    Matthias Braun
    2019-01-07T06:27:50+08:002019-01-07T06:27:50+08:00

    这是我当前的状态栏:

    状态栏截图声音开启

    静音时:

    状态栏截图声音关闭

    status.sh其中~/.config/sway/config调用的内容:

    # The Sway configuration file in ~/.config/sway/config calls this script.
    # You should see changes to the status bar after saving this script.
    # If not, do "killall swaybar" and $mod+Shift+c to reload the configuration.
    
    # The abbreviated weekday (e.g., "Sat"), followed by the ISO-formatted date
    # like 2018-10-06 and the time (e.g., 14:01). Check `man date` on how to format
    # time and date.
    date_formatted=$(date "+%a %F %H:%M")
    
    # "upower --enumerate | grep 'BAT'" gets the battery name (e.g.,
    # "/org/freedesktop/UPower/devices/battery_BAT0") from all power devices.
    # "upower --show-info" prints battery information from which we get
    # the state (such as "charging" or "fully-charged") and the battery's
    # charge percentage. With awk, we cut away the column containing
    # identifiers. i3 and sway convert the newline between battery state and
    # the charge percentage automatically to a space, producing a result like
    # "charging 59%" or "fully-charged 100%".
    battery_info=$(upower --show-info $(upower --enumerate |\
    grep 'BAT') |\
    egrep "state|percentage" |\
    awk '{print $2}')
    
    # "amixer -M" gets the mapped volume for evaluating the percentage which
    # is more natural to the human ear according to "man amixer".
    # Column number 4 contains the current volume percentage in brackets, e.g.,
    # "[36%]". Column number 6 is "[off]" or "[on]" depending on whether sound
    # is muted or not.
    # "tr -d []" removes brackets around the volume.
    # Adapted from https://bbs.archlinux.org/viewtopic.php?id=89648
    audio_volume=$(amixer -M get Master |\
    awk '/Mono.+/ {print $6=="[off]" ?\
    $4" ?": \
    $4" ?"}' |\
    tr -d [])
    
    # Additional emojis and characters for the status bar:
    # Electricity: ⚡ ↯ ⭍ ?
    # Audio: ? ? ? ? ? ?
    # Separators: \| ❘ ❙ ❚
    # Misc: ? ? ? ? ⭐ ? ↑ ↓ ✉ ✅ ❎
    echo $audio_volume $battery_info ? $date_formatted
    

    这是状态栏的一部分~/.config/sway/config:

    bar {
        position top
    
        # Keep in mind that the current directory of this config file is $HOME
        status_command while ~/.config/sway/status.sh; do sleep 1; done
    
        # https://i3wm.org/docs/userguide.html#_colors
        colors {
            # Text color of status bar
            statusline #f8b500
    
            # Background color of status bar
            background #5e227f
        }
    }
    

    status.sh当使用上面显示的相同块调用时,也可以与i3一起使用。/.config/i3/configbar

    这是我当前 Sway 配置的链接,其中包含status.sh.

    • 6
  3. Nishant
    2019-05-25T22:16:26+08:002019-05-25T22:16:26+08:00

    我喜欢 bash,但我为此使用了 Python 脚本。看起来 Python 的标准库有很多用于这类事情的实用程序。

    from datetime import datetime
    from psutil import disk_usage, sensors_battery
    from psutil._common import bytes2human
    from socket import gethostname, gethostbyname
    from subprocess import check_output
    from sys import stdout
    from time import sleep
    
    def write(data):
        stdout.write('%s\n' % data)
        stdout.flush()
    
    def refresh():
        disk = bytes2human(disk_usage('/').free)
        ip = gethostbyname(gethostname())
        try:
            ssid = check_output("iwgetid -r", shell=True).strip().decode("utf-8")
            ssid = "(%s)" % ssid
        except Exception:
            ssid = "None"
        battery = int(sensors_battery().percent)
        status = "Charging" if sensors_battery().power_plugged else "Discharging"
        date = datetime.now().strftime('%h %d %A %H:%M')
        format = "Space: %s | Internet: %s %s | Battery: %s%% %s | Date: %s"
        write(format % (disk, ip, ssid, battery, status, date))
    
    while True:
        refresh()
        sleep(1)
    

    这是酒吧的屏幕截图:

    状态栏截图

    • 5
  4. JiaHao Xu
    2021-02-16T23:18:01+08:002021-02-16T23:18:01+08:00

    摇摆状态

    我为 i3 和 sway编写了一个轻量级但功能丰富的状态栏swaystatus 。

    它完全用 C/C++ 编写,以使其尽可能轻量级,特别是避免像在 Bash 脚本中那样每秒创建新进程。

    它使用libupower-glib,libasound和之类的库libnm来检索电池、音量和网络信息,而不是使用upower,amixer或nmcli.

    对于背光、负载和 meminfo,它直接从和/sys/class/backlight读取。/proc/loadavg/proc/meminfo

    在我的 x86-64 计算机上,它使用clang-11.

    • 1

相关问题

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    ssh 无法协商:“找不到匹配的密码”,正在拒绝 cbc

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    如何卸载内核模块“nvidia-drm”?

    • 13 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Wong Jia Hau ssh-add 返回:“连接代理时出错:没有这样的文件或目录” 2018-08-24 23:28:13 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve