add battery status to kitty tab bar
This commit is contained in:
parent
864040811d
commit
c5d6a56eba
|
@ -1,5 +1,7 @@
|
|||
# pyright: reportMissingImports=false
|
||||
import datetime
|
||||
from datetime import datetime
|
||||
import subprocess
|
||||
import os
|
||||
|
||||
from kitty.boss import get_boss
|
||||
from kitty.fast_data_types import Screen, add_timer
|
||||
|
@ -14,15 +16,7 @@ from kitty.tab_bar import (
|
|||
)
|
||||
from kitty.utils import color_as_int
|
||||
|
||||
|
||||
def calc_draw_spaces(*args) -> int:
|
||||
length = 0
|
||||
for i in args:
|
||||
if not isinstance(i, str):
|
||||
i = str(i)
|
||||
length += len(i)
|
||||
return length
|
||||
|
||||
RIGHT_MARGIN = 1
|
||||
|
||||
def _draw_left_status(
|
||||
draw_data: DrawData,
|
||||
|
@ -62,20 +56,25 @@ def _draw_right_status(draw_data: DrawData, screen: Screen, is_last: bool) -> in
|
|||
return 0
|
||||
|
||||
draw_attributed_string(Formatter.reset, screen)
|
||||
time = datetime.datetime.now().strftime(" %H:%M")
|
||||
date = datetime.datetime.now().strftime(" %d.%m.%Y")
|
||||
time = datetime.now().strftime(" %H:%M")
|
||||
date = datetime.now().strftime(" %d.%m.%Y")
|
||||
# TODO: Figure out how to import psutils so I don't have to call a separate script
|
||||
bat = subprocess.getoutput(os.path.expandvars("~/.local/bin/battery"))
|
||||
|
||||
cells = [
|
||||
(draw_data.active_fg, bat),
|
||||
(draw_data.active_fg, time),
|
||||
(draw_data.inactive_fg, date),
|
||||
]
|
||||
|
||||
right_status_length = calc_draw_spaces(time + " " + date)
|
||||
right_status_length = RIGHT_MARGIN
|
||||
for i in cells:
|
||||
right_status_length += (len(str(i[1])))
|
||||
|
||||
draw_spaces = screen.columns - screen.cursor.x - right_status_length
|
||||
if draw_spaces > 0:
|
||||
screen.draw(" " * draw_spaces)
|
||||
|
||||
cells = [
|
||||
(draw_data.active_fg, time),
|
||||
(draw_data.inactive_fg, date),
|
||||
]
|
||||
|
||||
screen.cursor.fg = 0
|
||||
for color, status in cells:
|
||||
screen.cursor.fg = as_rgb(color_as_int(color))
|
||||
|
@ -109,7 +108,7 @@ def draw_tab(
|
|||
) -> int:
|
||||
global timer_id
|
||||
if timer_id is None:
|
||||
timer_id = add_timer(_redraw_tab_bar, 2.0, True)
|
||||
timer_id = add_timer(_redraw_tab_bar, 15.0, True)
|
||||
_draw_left_status(
|
||||
draw_data,
|
||||
screen,
|
||||
|
|
|
@ -10,12 +10,12 @@ lvim.builtin.alpha.mode = "dashboard"
|
|||
lvim.builtin.terminal.active = true
|
||||
lvim.builtin.nvimtree.active = false
|
||||
lvim.builtin.treesitter.highlight.enabled = true
|
||||
lvim.lsp.automatic_servers_installation = true
|
||||
lvim.lsp.automatic_servers_installation = false
|
||||
vim.g.NERDTreeHijackNetrw = 0
|
||||
vim.g.lf_replace_netrw = 1
|
||||
vim.opt.foldmethod = "expr"
|
||||
vim.opt.foldexpr = "nvim_treesitter#foldexpr()"
|
||||
|
||||
lvim.format_on_save = false
|
||||
|
||||
local formatters = require "lvim.lsp.null-ls.formatters"
|
||||
formatters.setup {
|
||||
|
@ -98,8 +98,9 @@ lvim.plugins = {
|
|||
-- keymappings [view all the defaults by pressing <leader>Lk]
|
||||
lvim.leader = "space"
|
||||
lvim.keys.insert_mode = {
|
||||
["<c-s>"] = "<ESC>:w<CR>i<Right>",
|
||||
["<c-q>"] = "<ESC>:bd<CR>",
|
||||
["<C-S>"] = "<ESC>:w<CR>a",
|
||||
["<C-Q>"] = "<ESC>:bd<CR>",
|
||||
["<C-BS>"] = "<C-W>"
|
||||
}
|
||||
lvim.keys.normal_mode = {
|
||||
["<c-s>"] = ":w<CR>",
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
#!/bin/env python3
|
||||
# coding=UTF-8
|
||||
import psutil
|
||||
|
||||
UNPLUGGED_ICONS = {
|
||||
1: "",
|
||||
2: "",
|
||||
3: "",
|
||||
4: "",
|
||||
5: "",
|
||||
6: "",
|
||||
7: "",
|
||||
8: "",
|
||||
9: "",
|
||||
10: "",
|
||||
}
|
||||
|
||||
PLUGGED_ICONS = {
|
||||
1: "",
|
||||
}
|
||||
|
||||
UNKOWN_ICON = ""
|
||||
|
||||
|
||||
def get_battery():
|
||||
# battery = psutil.sensors_battery()
|
||||
# ret = str(round(battery.percent)) + "% "
|
||||
# if not battery:
|
||||
# return ret + UNKOWN_ICON
|
||||
# if battery.power_plugged:
|
||||
# return ret + PLUGGED_ICONS[min(PLUGGED_ICONS.keys(), key=lambda x: abs(x - battery.percent))]
|
||||
# return ret + UNPLUGGED_ICONS[min(UNPLUGGED_ICONS.keys(), key=lambda x: abs(x - battery.percent))]
|
||||
battery = psutil.sensors_battery()
|
||||
ret = str(round(battery.percent)) + "% "
|
||||
if not battery:
|
||||
return ret + UNKOWN_ICON
|
||||
if battery.power_plugged:
|
||||
return (
|
||||
ret
|
||||
+ PLUGGED_ICONS[
|
||||
min(PLUGGED_ICONS.keys(), key=lambda x: abs(x * 10 - battery.percent))
|
||||
]
|
||||
)
|
||||
return (
|
||||
ret
|
||||
+ UNPLUGGED_ICONS[
|
||||
min(UNPLUGGED_ICONS.keys(), key=lambda x: abs(x * 10 - battery.percent))
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
print(get_battery())
|
Loading…
Reference in New Issue