How does htop display work? - shell

I want to write a program that displays its output in the shell like htop does, as opposed to just running the program and using the watch command. I have looked through the htop source code and am still a little lost. Is there a output beyond tty in the shell that is used, or are all the htop panels custom and opening an output like that is not a native task for a shell like bash?
Thanks.

htop author here. What you're looking for is the NCurses library.
With NCurses, you can implement a full-screen textmode application in C.
It provides a set of primitives for "drawing" on the terminal: ie, move() to go to a X-Y coordinate on the screen, functions to change colors, to erase part of the screen, etc. It also provides some high-level constructs such as "windows" through which you can scroll parts of the screen separately, but in htop I don't use that and implement my scrollable panels drawing them "by hand" using the lower-level primitives.

Related

Win32 segmented, double buffered console

Why do I even bother with this? Well, it's a pretty handy debug tool for large programs.
The concept is the following: I would like to split the console area into two parts:
- the upper part double buffered (I already know how to do this)
- the lower part functioning as a regular standard output (as in: scrolls, responds to scroll control, etc.)
An example image:
What I would like to achieve is to keep the persistent part always on top, while the scrollable part indeed scrolls, and responds to the scroll control on the right.
I have some knowledge of the required functions (CreateConsoleScreenBuffer, SetConsoleActiveScreenBuffer, SetConsoleCursorPosition, etc.), but I'm struggling with the implementation.
I know it is possible, because I saw it in other programs. Therefore I would like to ask for suggestions on how to do this right.

How do I clear a screen efficiently

Whenever I try to make a game or something that requires the rapid clearing of a screen, I've used system("CLS"). I now know that that is not good to use so I tried using a for loop to print new lines rapidly. While this works it looks really weird and less clear. I've seen people try to use STRING to clear a screen but whenever I try it it never works. How can I clear my screen efficiently and cleanly.
There's no standard way to clear the screen. But in any case, actually clearing the terminal and redrawing will be far too slow and glitchy for many games.
You should use either your OS's native terminal handling functions, or a cross-platform library that abstracts them.
I'd recommend ncurses, which is the latter. It basically provides an interface akin to a double-buffered graphics API, and handles all of the actual terminal twiddling so the frame swap is as fast as possible.

X11 graphics context not rendering anything

I've been looking through the Xlib docs attempting to create a simple XWindows application. I'm able to get a window up and running, modify its background colour using pixel colours, etc.
Unfortunately, when I try and create a graphics content and render some of the primitives (Rectangles/Arcs etc) nothing is rendered.
I then built and ran the example here to make sure I wasn't missing something and it also just rendered the background with none of the primitives.
Can anyone explain what I may be missing here?
If it matters I'm running Fedora 23 on kernel 4.4.1 using Gnome shell.
You need to add event loop and move your drawing to occur after you receive expose event ( also make sure you set event mask when you create window or with XSelectInput call ). Likely the result of your drawing is disposed at some point and because you don't react to "window are is damaged, need to re-paint" notification all you see is window background
Take a look at this example

How can I capture the screen with Haskell on Mac OS X?

How can I capture the screen with Haskell on Mac OS X?
I've read Screen capture in Haskell?. But I'm working on a Mac Mini. So, the Windows solution is not applicable and the GTK solution does not work because it only captures a black screen. GTK in Macs only captures black screens.
How can I capture the screen with … and OpenGL?
Only with some luck. OpenGL is primarily a drawing API and the contents of the main framebuffer are undefined unless it's drawn to by OpenGL functions themself. That OpenGL could be abused was due to the way graphics system did manage their on-screen windows' framebuffers: After a window without predefined background color/brush was created, its initial framebuffer content was simply everything that was on the screen right before the window's creation. If a OpenGL context is created on top of this, the framebuffer could be read out using glReadPixels, that way creating a screenshot.
Today window compositing has become the norm which makes abusing OpenGL for taking screenshots almost impossible. With compositing each window has its own off-screen framebuffer and the screen's contents are composited only at the end. If you used that method outlined above, which relies on uninitialized memory containing the desired content, on a compositing window system, the results will vary wildly, between solid clear color, over wildly distorted junk fragments, to data noise.
Since taking a screenshot reliably must take into account a lot of idiosyncrasy of the system this is to happen on, it's virtually impossible to write a truly portable screenshot program.
And OpenGL is definitely the wrong tool for it, no matter that people (including myself) were able to abuse it for such in the past.
I programmed this C code to capture the screen of Macs and to show it in an OpenGL window through the function glDrawPixels:
opengl-capture.c
http://pastebin.com/pMH2rDNH
Coding the FFI for Haskell is quite trivial. I'll do it soon.
This might be useful to find the solution in C:
NeHe Productions - Using gluUnProject
http://nehe.gamedev.net/article/using_gluunproject/16013/
Apple Mailing Lists - Re: Screen snapshot example code posted
http://lists.apple.com/archives/cocoa-dev/2005/Aug/msg00901.html
Compiling OpenGL programs on Windows, Linux and OS X
http://goanna.cs.rmit.edu.au/~gl/teaching/Interactive3D/2012/compiling.html
Grab Mac OS Screen using GL_RGB format

How to capture a part of a screen using Ruby on Windows?

Instead of using some third party app, I'd like to write an app in Ruby that when invoked, will capture the full screen and save it in c:\screenshot\snap000001.png
The graphic package is readily there, but how can you capture a region from the full screen so as to save it?
This program is to be invoked by some hot-key, such as setting it to be running when CTRL-PrtScn is pressed, or CTRL-CTRL (both control on left and right), or ALT-ALT.
I haven't tried it (I'm not on windows). But you could use Win32::Screenshot.
While looking around, I've found the following, which does the screenshot using that library
width, height, bitmap = Win32::Screenshot.desktop
img_lst = ImageList.new
img_lst.from_blob(bitmap)
img_lst.write('public/screen.png')
And should write your screenshot as a png file.

Resources