su8 / pinky-bar

Gather some system information and show it in this program
GNU General Public License v3.0
14 stars 5 forks source link

Make a simplistic format parser/converter #27

Open lasers opened 6 years ago

lasers commented 6 years ago

su Open up a issue and copy and paste your comment 9 in there. One day with more free time or more contributors will make it happen.

re: limitation in customizing and/or formatting

In attempt to understand this issue better, I played with conky a bit.

# ~/.conkyrc
conky.config={
    update_interval=2,
    out_to_console=true,
    out_to_stderr=false,
};
conky.text = [[
 {color_1793d1}OS {color_b294bb}${distribution}
 {color_1793d1}CPU {color_f0c674}${cpu cpu0}
 {color_1793d1}CPU {color_f0c674}${cpu cpu1}% ${cpu cpu2}% ${cpu cpu3}%
 {color_1793d1}MEM {color_f0c674}${mem}/${memmax} (${memperc}%)
 {color_1793d1}HDD {color_f0c674}${fs_used_perc}%
 {color_1793d1}Down {color_f0c674}${downspeed}
 {color_1793d1}Up {color_f0c674}${upspeed}
 {color_1793d1}Kernel {color_f0c674}${kernel}
 {color_1793d1}Load 1 {color_f0c674}${loadavg 1}
 {color_1793d1}Time {color_f0c674}${time %T}
 {color_1793d1}Uptime {color_f0c674}${uptime}
 {color_1793d1}Addrs {color_f0c674}${addrs}
 {color_1793d1}Freq MHZ{color_f0c674}${freq}
 {color_1793d1}Freq GHZ{color_f0c674}${freq_g}
 # ---- omit this line
]]

Output

 {color_1793d1}OS {color_b294bb}Debian
 {color_1793d1}CPU {color_f0c674}2
 {color_1793d1}CPU {color_f0c674}3% 1% 0%
 {color_1793d1}MEM {color_f0c674}5.24GiB/7.71GiB (67%)
 {color_1793d1}HDD {color_f0c674}32%
 {color_1793d1}Down {color_f0c674}0B  
 {color_1793d1}Up {color_f0c674}0B  
 {color_1793d1}Kernel {color_f0c674}4.9.0-6-amd64
 {color_1793d1}Load 1 {color_f0c674}0.39
 {color_1793d1}Time {color_f0c674}02:32:03
 {color_1793d1}Uptime {color_f0c674}6d 14h 0m
 {color_1793d1}Addrs {color_f0c674}0.0.0.0
 {color_1793d1}Freq MHZ{color_f0c674}1267
 {color_1793d1}Freq GHZ{color_f0c674}1.27

Conky want ${color 1793d1}. I used {color_1793d1} supposedly for pinkybar to parse.

If pinkybar can behave like lemonbar, eg echo OUTPUT | pinkybar --format py3status, then pinkybar should be able to support output from any languages without having to be built with.

conky also come with lot of variables... essentially saving you lot of time in writing code and/or fixing bugs for arguments (such as pending unsolved --packages) reducing pinkybar into hopefully more of a simplistic bug-free parser/converter.

If no {color_xxx} are passed, then the output can be shown in any program "without colors" too.

Basically, the goal, I wonder, is to attain something like this:

# ~/.tmux.conf

# personal python script
set -g status-right '#(~/my_output_script.py | pinkybar --format=tmux)'

# use conky scripting
set -g status-right '#(conky | pinkybar --format=tmux)'
echo "{color_1793d1}Pkgs {color_f0c674}$(pinky --pkgs)" | pinkybar --format=py3status)
# OUTPUT: '[\?color=#1793d1&show Pkgs] [\?color=#f0c674&show 1833]'

echo "{color_1793d1}Pkgs {color_f0c674}$(pinky --pkgs)" | pinkybar --format=tmux)
# OUTPUT: '#[fg=#1793d1,bright]Pkgs #[fg=#f0c674,bright]1833]

echo "{color_1793d1}Pkgs {color_f0c674}$(pinky --pkgs)" | pinkybar --format=lemonbar)
# OUTPUT: '%{F#f0c674}Pkgs %{F#f0c674}1833'

pinky is a filler for options not available in conky. You do not have to use pinky. I used pinky in this example because it know I'm on Debian for --pkgs. Maybe file a feature request on conky for missing options.. or users can add their own script.

# pinky (pseudo)
if xx == "--pkgs"
   echo "$(ls /var/lib/dpkg/info/*.list | wc -l)"

One thing I didn't consider is that maybe some format does not support color hex or that it could look bad in some program (such as tmux), but with conky or any other scripting, users can specify different colors so I think it's not going to be a problem anyway.

P.S. conky also support exec and execi (same as exec, but with a specific interval in seconds. The interval can't be less than update_interval in conky configuration.) possibly making it useful for pinky --weather or such. :+1:

Link: https://github.com/brndnmtthws/conky/wiki/Configuration-Variables

lasers commented 6 years ago

My ~/src/f2f/f2f.py script.

#!/usr/bin/env python
from sys import argv, stdin, exit

if __name__ == "__main__":

    if '--to-tmux' in argv:
        string = '#[fg={color},bright]{output}'
    elif '--to-py3status' in argv:
        string = '[\?color=#{color}&show {output}]'
    elif '--to-lemonbar' in argv:
        string = '%{F#{color}}{output}'
    elif '--to-xmobar' in argv:
        string = '<fc=#{color}>{output}'
    else:
        exit('No argument')

    output_string = ''
    for line in stdin:
        output_string += ''.join(line.splitlines())

    output_list = output_string.split('{color_')[1::1]

    for index, parameter in enumerate(output_list):
        end = parameter.find('}')
        color = parameter[:end]
        output = parameter[1 + end:]
        output_list[index] = string.format(color=color, output=output)

    print(''.join(output_list))

Small changes to pinkybar module.

        self.command = 'conky | ~/src/f2f/f2f.py --to-py3status'
        # if not self.command or not self.py3.check_commands(self.command):
        #     raise Exception(STRING_ERROR)

Result: py3status 2018-04-20-082114_3120x1920_scrot lemonbar 2018-04-20-090013_3120x1920_scrot

EDIT: This does not leave conky running in the background so I miss out on different interval time, upload, download, etc... but is definitely better. :+1:

su8 commented 6 years ago

You can add it to the py3status folder too.

su8 commented 6 years ago

I have something in mind to create three --color1,2,3 options that have to be supplied right after --format tmux, but will see how much time that would take. ♥

lasers commented 6 years ago

Close this? Seems like f2f does the job okay unless you have something else in mind?

su8 commented 6 years ago

I'm working on implementing color support via --color1 #000fff with up to three colors that have to supplied right after --format tmux etc.

su8 commented 6 years ago

It's rather tricky, saving my work for next week.

su8 commented 6 years ago

After some sleep I managed to get it working.

pinkybar --conf ~/.config/pinky-tmux.conf

Notice the new --conf option that allows you to use different location and different config according to the used program.

--fmt=tmux
--color1="#[fg=blue,bright]"
--color2="#[fg=magenta,bright]"
--color3="#[fg=yellow,bright]"
su8 commented 6 years ago

The first parser introduced by me was kinda working, but not exactly on 100%, so we need another strategy for this.