AndyObtiva / glimmer-dsl-libui

Glimmer DSL for LibUI - Prerequisite-Free Ruby Desktop Development Cross-Platform Native GUI Library - The Quickest Way From Zero To GUI - If You Liked Shoes, You'll Love Glimmer! - No need to pre-install any prerequisites. Just install the gem and have platform-independent GUI that just works on Mac, Windows, and Linux.
MIT License
474 stars 15 forks source link

[Proposal] Add a simple example for periodically updating process information (such as CPU/RAM usage or similar) #7

Closed rubyFeedback closed 2 years ago

rubyFeedback commented 2 years ago

Hello Andy and everyone else,

On:

https://github.com/AndyObtiva/glimmer-dsl-libui/blob/master/images/glimmer-dsl-libui-linux-basic-table-progress-bar.png

you showcase the progress bar.

If you happen to have spare time and motivation, would you consider adding an example that showcases CPU usage?

It's fine if that works only on e. g. Linux if that simplifies it - a bit like the program htop, but more a bar or progress counter for CPU usage %. (I forgot which linux command shows it, but I think you can get that information via ruby `` or something... perhaps "ps aux" or some variant. I once wanted to 'port' htop onto ruby-gtk but did not really finish - easy ideas, harder implementations, my mind hops on to the next simpler thing to do :D )

This example could then be added to demo-showcase at the least of glimmer-dsl-libui (although I guess the main selling point for glimmer may be to re-use the same code for getting different "GUIs" to work free, just as the main glimmer entry point shows some examples).

I guess the important part you may ask is "what is the use case", simply to focus on the rationale of the issue request here.

Well, glimmer in general, but also glimmer-dsl-libui in particular, kind of showcases a LOT of features and functionality if you look through it. You also have a long todo-list, which you make available and extend, so I feel a bit bad to suggest new things :D (but I think the issue request here is fairly short-ish).

You already make available the progress-bar as such. So the idea I had is to simply show how this could be done dynamically, e. g. "every 10 seconds update the display" or something. Note that I am not necessarily saying this has to be a progress bar. There are many possibilities to implement it, even just via a drawing canvas and controlling the width and RGBA values. The main point why I think this may be useful is the dynamic aspect, e. g. to show how to dynamically update the display.

(Admittedly it may be better to have it work on OSX and windows too, but I have zero experience with OSX, and on windows, while there are often ways to query stuff via the commandline and powershell, I guess, right now I don't know how easy it is to do so. Often one has to google for obscure things to get things to work from the commandline as-is, so IMO it may be fine to just add a linux example for now, and ask for other people to add code to make it work on these other platforms. You already provide some cross-OS examples, such as tetris or snake I guess, and the various screenshots for these. So the TL;DR part really is about updating the display periodically - I think this could help serve for other examples how to use glimmer in this regard, e. g. any process monitor or UI interface that monitors values that may change, such as in biotechnology monitoring a reactor fed with bacteria producing stuff. We had that in university, although we just used some windows GUI that came with the mini-reactor e. g. these lab-reactors that are perhaps only 30cm in length or so)

AndyObtiva commented 2 years ago

Cool, here is a Work in Progress version working in Windows only (I will add Linux and Mac support next, and maybe will add Memory Usage in a future version after releasing a simple version first):

require 'glimmer-dsl-libui'

include Glimmer

data = [
  ['CPU', '0%', 0],
]

Glimmer::LibUI.timer(1) do
  cpu_percentage_raw_value = `wmic cpu get loadpercentage`
  cpu_percentage_value = cpu_percentage_raw_value.split("\n")[2].to_i
  data[0][1] = "#{cpu_percentage_value}%"
  data[0][2] = cpu_percentage_value
end

window('CPU Percentage', 400, 200) {
  vertical_box {
    table {
      text_column('Name')
      text_column('Value')
      progress_bar_column('Percentage')

      cell_rows data # implicit data-binding
    }
  }
}.show

glimmer-dsl-libui-windows-cpu-percentage

Thanks for the suggestion.

AndyObtiva commented 2 years ago

Done! The example is fully documented over here:

https://github.com/AndyObtiva/glimmer-dsl-libui#cpu-percentage

Code:

https://github.com/AndyObtiva/glimmer-dsl-libui/blob/master/examples/cpu_percentage.rb

Mac

Win

Lin

If you would like to extend further in the future (e.g. add memory usage), please feel free to do so and send me a Pull Request.

By the way, what you asked for regarding doing something periodically is doable with Glimmer::LibUI.timer(seconds, repeat: true) {...} and is already demonstrated in the Timer example (among multiple examples like Snake and Tetris too).

Glimmer::LibUI.timer(1) do
  # Do something every second
end

I noticed that in Windows only, sometimes you need to wrap the code with an extra Glimmer::LibUI.queue_main {} inside a timer block if the code is causing changes to the GUI.