如何自动调整 wibar 的大小?

发布于 2025-01-16 22:11:15 字数 3521 浏览 0 评论 0原文

我已经配置了 wibar,以便输出自动更新。但有一个问题是输出可能超出wibar的边界。我想出的所有解决方案都效果不佳。我该如何解决这个问题?

示例:
不幸的是,在我有 10 个声望之前我无法插入图像。我只能在 github 上留下一个 指向问题的链接,其中有一张图像显示问题。

代码:

mycpu = wibox.widget { widget = wibox.widget.textbox }

mycputemp = wibox.widget { widget = wibox.widget.textbox }

myram = wibox.widget { widget = wibox.widget.textbox }

mybattery = wibox.widget { widget = wibox.widget.textbox }

myupdates = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#c38a48'></span> . ",
   widget = wibox.widget.textbox
}

mykeyboardlayout = awful.widget.keyboardlayout()
mykeyboardlayout_icon = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#d499e5'></span>",
   widget = wibox.widget.textbox
}

mytextclock = wibox.widget {
   format = ' %H:%M ',
   widget = wibox.widget.textclock
}
mytextclock_icon = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#51afef'></span>",
   widget = wibox.widget.textbox
}

awful.screen.connect_for_each_screen(function(s)
      s.mywibox = awful.wibar {
         screen  = s,
         align   = "right",
         margins = {
            top   = 14,
            right = 14
         },
         width   = 586,
         height  = 42,
         bg      = "#1c252acc",
         widget  = {
            {
               layout = wibox.layout.fixed.horizontal,
               expand = "none",
               mycpu,
               mycputemp,
               myram,
               mybattery,
               myupdates,
               mykeyboardlayout_icon,
               mykeyboardlayout,
               mytextclock_icon,
               mytextclock
            },
            widget = wibox.container.margin
         }
      }
end)

gears.timer {
   timeout   = 1.5,
   call_now  = true,
   autostart = true,
   callback  = function()
      awful.spawn.easy_async_with_shell("cpu",
                                        function(out)
                                           cpu_out = " <span font='Myfont' size='16.5pt' foreground='#ff6c6b'></span> " ..
                                              out:gsub("%\n", "") .. " "
      end)
      awful.spawn.easy_async_with_shell("cat /sys/class/hwmon/hwmon3/temp1_input",
                                        function(out)
                                           mycputemp.markup = " <span font='Myfont' size='16.5pt' foreground='#ffaf00'></span> " ..
                                              math.floor(tonumber(out)/1000+0.5) .. "°C "
      end)
      awful.spawn.easy_async_with_shell("ram",
                                        function(out)
                                           ram_out = " <span font='Myfont' size='16.5pt' foreground='#98be65'></span> " ..
                                              out:gsub("%\n", "") .. " "
      end)
      awful.spawn.easy_async_with_shell("cat /sys/class/power_supply/BAT1/capacity",
                                        function(out)
                                           bat_out = " <span font='MyFont' size='16.5pt' foreground='#46d9ff'></span> " ..
                                              out:gsub("%\n", "") .. "% "
      end)
      mycpu.markup     = cpu_out
      myram.markup     = ram_out
      mybattery.markup = bat_out
   end
}

I have configured wibar so that the output is updated automatically. But there is a problem that the output can go beyond the borders of wibar. All the solutions I came up with don't work very well. How can I solve this problem?

Example:
Unfortunately, I can't insert an image until I have 10 reputations. I can only leave a link to the question on github where there is an image showing the problem.

Code:

mycpu = wibox.widget { widget = wibox.widget.textbox }

mycputemp = wibox.widget { widget = wibox.widget.textbox }

myram = wibox.widget { widget = wibox.widget.textbox }

mybattery = wibox.widget { widget = wibox.widget.textbox }

myupdates = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#c38a48'></span> . ",
   widget = wibox.widget.textbox
}

mykeyboardlayout = awful.widget.keyboardlayout()
mykeyboardlayout_icon = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#d499e5'></span>",
   widget = wibox.widget.textbox
}

mytextclock = wibox.widget {
   format = ' %H:%M ',
   widget = wibox.widget.textclock
}
mytextclock_icon = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#51afef'></span>",
   widget = wibox.widget.textbox
}

awful.screen.connect_for_each_screen(function(s)
      s.mywibox = awful.wibar {
         screen  = s,
         align   = "right",
         margins = {
            top   = 14,
            right = 14
         },
         width   = 586,
         height  = 42,
         bg      = "#1c252acc",
         widget  = {
            {
               layout = wibox.layout.fixed.horizontal,
               expand = "none",
               mycpu,
               mycputemp,
               myram,
               mybattery,
               myupdates,
               mykeyboardlayout_icon,
               mykeyboardlayout,
               mytextclock_icon,
               mytextclock
            },
            widget = wibox.container.margin
         }
      }
end)

gears.timer {
   timeout   = 1.5,
   call_now  = true,
   autostart = true,
   callback  = function()
      awful.spawn.easy_async_with_shell("cpu",
                                        function(out)
                                           cpu_out = " <span font='Myfont' size='16.5pt' foreground='#ff6c6b'></span> " ..
                                              out:gsub("%\n", "") .. " "
      end)
      awful.spawn.easy_async_with_shell("cat /sys/class/hwmon/hwmon3/temp1_input",
                                        function(out)
                                           mycputemp.markup = " <span font='Myfont' size='16.5pt' foreground='#ffaf00'></span> " ..
                                              math.floor(tonumber(out)/1000+0.5) .. "°C "
      end)
      awful.spawn.easy_async_with_shell("ram",
                                        function(out)
                                           ram_out = " <span font='Myfont' size='16.5pt' foreground='#98be65'></span> " ..
                                              out:gsub("%\n", "") .. " "
      end)
      awful.spawn.easy_async_with_shell("cat /sys/class/power_supply/BAT1/capacity",
                                        function(out)
                                           bat_out = " <span font='MyFont' size='16.5pt' foreground='#46d9ff'></span> " ..
                                              out:gsub("%\n", "") .. "% "
      end)
      mycpu.markup     = cpu_out
      myram.markup     = ram_out
      mybattery.markup = bat_out
   end
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏至、离别 2025-01-23 22:11:15

我能找到的最佳解决方案是使用 awful.popup 而不是 awful.wibar

mycpu = wibox.widget { widget = wibox.widget.textbox }

mycputemp = wibox.widget { widget = wibox.widget.textbox }

myram = wibox.widget { widget = wibox.widget.textbox }

mybattery = wibox.widget { widget = wibox.widget.textbox }

myupdates = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#c38a48'></span> . ",
   widget = wibox.widget.textbox
}

mykeyboardlayout = awful.widget.keyboardlayout()
mykeyboardlayout_icon = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#d499e5'></span>",
   widget = wibox.widget.textbox
}

mytextclock = wibox.widget {
   format = ' %H:%M ',
   widget = wibox.widget.textclock
}
mytextclock_icon = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#51afef'></span>",
   widget = wibox.widget.textbox
}

awful.screen.connect_for_each_screen(function(s)
      s.mywibox = awful.popup {
         screen  = s,
         placement = function(c)
            return awful.placement.top_right(c, { margins = 14 })
         end,
         minimum_height = 42,
         bg      = "#1c252acc",
         widget  = {
            {
               layout = wibox.layout.fixed.horizontal,
               expand = "none",
               mycpu,
               mycputemp,
               myram,
               mybattery,
               myupdates,
               mykeyboardlayout_icon,
               mykeyboardlayout,
               mytextclock_icon,
               mytextclock
            },
            widget = wibox.container.margin
         }
      }

      s.mywibox:struts({top = s.mywibox.minimum_height + beautiful.useless_gap * 2})
end)

gears.timer {
   timeout   = 1.5,
   call_now  = true,
   autostart = true,
   callback  = function()
      awful.spawn.easy_async_with_shell("cpu",
                                        function(out)
                                           mycpu.markup = " <span font='Myfont' size='16.5pt' foreground='#ff6c6b'></span> " ..
                                              out:gsub("%\n", "") .. " "
      end)
      awful.spawn.easy_async_with_shell("cat /sys/class/hwmon/hwmon3/temp1_input",
                                        function(out)
                                           mycputemp.markup = " <span font='Myfont' size='16.5pt' foreground='#ffaf00'></span> " ..
                                              math.floor(tonumber(out)/1000+0.5) .. "°C "
      end)
      awful.spawn.easy_async_with_shell("ram",
                                        function(out)
                                           myram.markup = " <span font='Myfont' size='16.5pt' foreground='#98be65'></span> " ..
                                              out:gsub("%\n", "") .. " "
      end)
      awful.spawn.easy_async_with_shell("cat /sys/class/power_supply/BAT1/capacity",
                                        function(out)
                                           mybattery.markup = " <span font='MyFont' size='16.5pt' foreground='#46d9ff'></span> " ..
                                              out:gsub("%\n", "") .. "% "
      end)
   end
}

The best solution I could find was to use awful.popup instead of awful.wibar.

mycpu = wibox.widget { widget = wibox.widget.textbox }

mycputemp = wibox.widget { widget = wibox.widget.textbox }

myram = wibox.widget { widget = wibox.widget.textbox }

mybattery = wibox.widget { widget = wibox.widget.textbox }

myupdates = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#c38a48'></span> . ",
   widget = wibox.widget.textbox
}

mykeyboardlayout = awful.widget.keyboardlayout()
mykeyboardlayout_icon = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#d499e5'></span>",
   widget = wibox.widget.textbox
}

mytextclock = wibox.widget {
   format = ' %H:%M ',
   widget = wibox.widget.textclock
}
mytextclock_icon = wibox.widget {
   markup = " <span font='MyFont' size='16.5pt' foreground='#51afef'></span>",
   widget = wibox.widget.textbox
}

awful.screen.connect_for_each_screen(function(s)
      s.mywibox = awful.popup {
         screen  = s,
         placement = function(c)
            return awful.placement.top_right(c, { margins = 14 })
         end,
         minimum_height = 42,
         bg      = "#1c252acc",
         widget  = {
            {
               layout = wibox.layout.fixed.horizontal,
               expand = "none",
               mycpu,
               mycputemp,
               myram,
               mybattery,
               myupdates,
               mykeyboardlayout_icon,
               mykeyboardlayout,
               mytextclock_icon,
               mytextclock
            },
            widget = wibox.container.margin
         }
      }

      s.mywibox:struts({top = s.mywibox.minimum_height + beautiful.useless_gap * 2})
end)

gears.timer {
   timeout   = 1.5,
   call_now  = true,
   autostart = true,
   callback  = function()
      awful.spawn.easy_async_with_shell("cpu",
                                        function(out)
                                           mycpu.markup = " <span font='Myfont' size='16.5pt' foreground='#ff6c6b'></span> " ..
                                              out:gsub("%\n", "") .. " "
      end)
      awful.spawn.easy_async_with_shell("cat /sys/class/hwmon/hwmon3/temp1_input",
                                        function(out)
                                           mycputemp.markup = " <span font='Myfont' size='16.5pt' foreground='#ffaf00'></span> " ..
                                              math.floor(tonumber(out)/1000+0.5) .. "°C "
      end)
      awful.spawn.easy_async_with_shell("ram",
                                        function(out)
                                           myram.markup = " <span font='Myfont' size='16.5pt' foreground='#98be65'></span> " ..
                                              out:gsub("%\n", "") .. " "
      end)
      awful.spawn.easy_async_with_shell("cat /sys/class/power_supply/BAT1/capacity",
                                        function(out)
                                           mybattery.markup = " <span font='MyFont' size='16.5pt' foreground='#46d9ff'></span> " ..
                                              out:gsub("%\n", "") .. "% "
      end)
   end
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文