Author Topic: ncurses  (Read 2708 times)

Ad_Rienks

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
ncurses
« on: February 13, 2023, 08:16:29 AM »
It is off-topic, I guess; I searched this forum for ncurses and found it mentioned only once.
My question for anyone who is interested in ncurses.

Chatting with Chat-GPT I wanted to know how the GNU Midnight Commander app was build. Eventually Chat-GPT provided me with this example program:
Code: [Select]
#include <ncurses.h>
#include <string.h>

int main()
{
    // Initialize the ncurses library
    initscr();

    // Enable the use of color in the terminal
    start_color();

    // Disable line buffering and character echoing
    cbreak();
    noecho();

    // Hide the cursor
    curs_set(0);

    // Use the default color pair (white on black)
    attron(COLOR_PAIR(0));

    // Clear the screen and move the cursor to the top left corner
    clear();
    move(0, 0);

    // Print the welcome message
    char message[] = "Welcome to the C Programming Language!";
    mvprintw(LINES / 2, (COLS - strlen(message)) / 2, "%s", message);

    // Refresh the screen
    refresh();

    // Wait for the user to press any key
    getch();

    // Clean up and exit
    endwin();
    return 0;
}


I also asked which compilers could cope with this code:
Quote
The ncurses.h header file is part of the ncurses library, which is a popular library for creating text-based user interfaces in a terminal emulator. The ncurses library is widely supported by many compilers and platforms, including:

GCC (GNU Compiler Collection): This is a popular open-source compiler that supports a wide range of platforms, including Linux, Unix, and macOS.

Clang: This is a free, open-source compiler for the C, C++, and Objective-C programming languages. It is widely used on macOS and is also available on Linux and other platforms.

MinGW: This is a collection of header files and import libraries that allow you to use GCC to compile Windows applications. MinGW provides a native Windows port of the ncurses library, allowing you to compile and run ncurses-based applications on Windows.

Visual Studio: This is a proprietary integrated development environment (IDE) developed by Microsoft. It includes a compiler for the C and C++ programming languages and supports the creation of Windows applications. Visual Studio does not include the ncurses library by default, but it is possible to obtain and use the library with Visual Studio by installing it as a third-party library.

These are just a few of the many compilers that support the ncurses library and can be used to compile code that uses the ncurses.h header file. You should be able to use any of these compilers to compile the example code I provided earlier, or other code that uses the ncurses library.






Anyone? If have virtually no experience in GCC, CLang, MingW. I have used Visual Studio from time to time, but not with 'third-party libraries'.
Anyone having some experience in this field? I'm also curious if the above sample code compiles and runs.

Thanks for any tips, tricks!

Ed Davis

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: ncurses
« Reply #1 on: February 13, 2023, 10:20:40 AM »
I tried your example on both Linux and Windows 10.
I used gcc on both systems.
Seems to work fine.

I used (n)curses quite a bit a few years ago.  It is a good text mode library.  It is similar to the old conio library on Windows, but curses has lots more capabilities.
It is _the_ standard text mode library for Linux.

That being said, you can accomplish much the same thing using ANSI/VT100 escape sequences, which now a days most terminals support.
VT100 escape sequences are even supported by cmd.exe in Windows 10 and later (remember the old ansi.sys we used in the DOS days)?

Curses and/or VT100 or even the Windows Console API are only needed if you want to use colors and position the cursor in text mode.  Or read "special" keys like F1 and friends.

What is it that you want to do?

Ad_Rienks

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: ncurses
« Reply #2 on: February 13, 2023, 10:47:47 AM »
Hello Ed!

I think I remember your name from many moons ago! Glad that you reach out to help!

Quote
What is it that you want to do?

What I want: together with some friends we're working on an app, eventually leading to an accounting application. Now, the problem is that the one is programming in C, and the other has old (console only) code that needs to be updated and possibly enhanced. Both are not very much into BCX.
So, can you advice me how I could be helpful, in the best and possibly the easiest way? I can write functions in BCX, even in C, if it's not too complicated. Which of the alternatives you mention could be the best way to go?

And; you talk about GCC, how have you set that up, which commands to use? With batch files?

I forgot to mention: I'm on Windows 10. And we want to use colors and position the cursor in text mode; it should be highly interactive, as you should understand for an accounting program!

Thank you again, I appreciate your help!
« Last Edit: February 13, 2023, 11:01:39 AM by Ad_Rienks »

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2392
    • View Profile
Re: ncurses
« Reply #3 on: February 13, 2023, 11:36:53 AM »
Here's my 2¢

I assume your new console-mode accounting program is being written exclusively for Windows.

If all you need is positioning the cursor, setting text color and so on, BCX already has the traditional
BASIC commands and functions to do those things.  The runtime codes have easy to understand
wrappers that make the necessary calls to the Win32Api, so you don't need to reinvent the wheel.

I wrote a console-mode text editor (ED) in PowerBasic for DOS back in the late 1980's.  I later converted
ED to BCX Basic.  There is a stable version with BCX source code from 2011 that you can examine here:

https://bcxbasiccoders.com/archives/YahooGroups/Source_Code/
https://bcxbasiccoders.com/archives/YahooGroups/Source_Code/Ed%203.102.Zip

You likely won't need any code from ED for your project, the BCX Help file is your friend.  Still,  ED
shows it is possible to write a scrolling, paging, colorizing console app with menus using only BCX.





« Last Edit: February 13, 2023, 11:45:55 AM by MrBcx »

Ed Davis

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: ncurses
« Reply #4 on: February 13, 2023, 11:57:08 AM »
What I want: together with some friends we're working on an app, eventually leading to an
accounting application. Now, the problem is that the one is programming in C, and the
other has old (console only) code that needs to be updated and possibly enhanced. Both are
not very much into BCX.

So, can you advice me how I could be helpful, in the best and possibly the easiest way? I
can write functions in BCX, even in C, if it's not too complicated. Which of the
alternatives you mention could be the best way to go?

It depends on the platform.

If you are targeting only Windows, then I would:

* Find the best Conio library you can
* Or roll your own.  It isn't that hard, just takes some time.  I did for a shareware program I worked on years ago.
* Find a different console library, that meets your requirements.  I have seen some on github.
* Play around with n(curses), and if it meets your needs, go for it!

If you are targeting Windows and Linux, then I would:

* Possibly use (n)curses.  But note that I don't think it is as good on Windows as it is on Linux - that is why I wrote my own.  But that was from 6 years ago, and maybe it was just my slow machine.
* Using VT100 escape sequences, write your own Windows/Linux library. As above, I have also done this - again, not that hard, just takes time.
* Find a different library that meets your requirements. As above, I have seen some on github.

Quote
And; you talk about GCC, how have you set that up, which commands to use? With batch
files?


I downloaded TDM gcc. https://jmeubank.github.io/tdm-gcc/

I downloaded ncurses from: https://invisible-mirror.net/datafiles/release/mingw64.zip

It was several (or 6!) years ago when I did this, but I believe I just followed the directions in the ncurses .zip file.

Quote
I forgot to mention: I'm on Windows 10. And we want to use colors and position the cursor
in text mode; it should be highly interactive, as you should understand for an accounting
program!

Ok, so based on that, see my notes above.

Update: I just saw Mr. BCX's post.  Yes, by all means, you can use the BCX console routines.  And like he said, take ED for a spin to see just what it can do.  I have, and ED works well.  Much better than I do (pun intended).
« Last Edit: February 13, 2023, 11:59:31 AM by Ed Davis »

Ad_Rienks

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: ncurses
« Reply #5 on: February 13, 2023, 12:11:20 PM »
MrBCX, thank you for your reply.

I downloaded ED and took a look; this application is certainly worth to check! Especially, the way you got rid of the maximize-, minimize- and close-button, in fact, all that is normally in the 'head' of a console!

There is one thing you might have overlooked: I mentioned (maybe no too explicitly) that we are aiming at using the full screen width and height. As the GNU Midnight Commander prog does! ED does not do that, it only uses half my screen width.
That is what I was 'discussing' with Chat-GPT, leading to suggesting the ncurses library, amongst others.

Maybe ncurses isn't the way to go - we'll try every route, and keep the best.
But, as I said before, I think I can learn from studying your code; that's what I'll do and, btw, I've already done in the past!
« Last Edit: February 13, 2023, 12:17:09 PM by Ad_Rienks »

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2392
    • View Profile
Re: ncurses
« Reply #6 on: February 13, 2023, 05:01:51 PM »
I mentioned (maybe not too explicitly) that we are aiming at using the full screen width and height. 

Good luck with that.  I predict a boat load of wasted time and frustration is coming your way.

In the good old days, you could stuff the keyboard buffer with ALT + ENTER to trigger full screen mode in a command prompt.  The closest I've come in Windows 10 is setting the command prompt properties layout buffer to 9999 for width and height and fiddling with font size.  And even then, I still get scrollbars bottom and side.  Not ideal.

I also tried my translation of Raymond Chen's full screen mode toggle (which works great in GUI apps), and it did indeed toggle console screen size, but it never reached full screen mode like one would want.  Maybe curses or ncurses will work perfectly with Windows 10/11 with no fuss.  Maybe pigs will fly too.

ref: https://bcxbasiccoders.com/smf/index.php?topic=652.msg3066#msg3066


MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2392
    • View Profile
Re: ncurses
« Reply #7 on: February 13, 2023, 06:58:15 PM »
Ad,

Here's something hopefully a bit more useful for you.  First the BCX code:

Code: [Select]

COLOR 7, 0
CLS
COLOR 15, 1
PRINT "0        10        20        30        40        50        60        70        80        90        100       110       120       130       140       150       160"
LOCATE 2, 100
COLOR 4, 0
PRINT "Hello, World"
COLOR 7, 0
PAUSE


Attached is a screenshot of my desktop with this app running.  It is not full screen but about as close
as I've been able to come.  To get to this point, I needed to adjust the command prompt properties
which I've provided screen shots of.  My screen is 1920 x 1080 and 96 DPI.

You can see in the app screenshot that "Hello, World" is correctly positioned at column 100.
The number 1 ( of 160 ) marks column number 160.  You cannot see the whole number 160 due
to all the various factors.

This forum software crops off my desktop snapshot but if you download app.png and open it in
Windows, this will all make sense.  Alternatively, you can right click on the image in your browser and
select Open Image in New Tab or New Window, depending on your browser.


« Last Edit: February 13, 2023, 07:39:12 PM by MrBcx »

Ad_Rienks

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: ncurses
« Reply #8 on: February 13, 2023, 07:58:16 PM »
MrBcx,

Quote
Here's something hopefully a bit more useful for you.

Thank you for your wisdom and example. I will keep your warnings in mind.

airr

  • Sr. Member
  • ****
  • Posts: 252
    • View Profile
Re: ncurses
« Reply #9 on: June 10, 2023, 06:47:10 PM »
A Few Months Later....

So I came across this post on a lazy Saturday afternoon, and was wondering how to launch the console full screen.

I'm on Windows 11, which uses the Windows Terminal app.

Here's what I tried:

Code: [Select]
sendAltEnter()

COLOR 7, 0
CLS
COLOR 15, 1
PRINT "0        10        20        30        40        50        60        70        80        90        100       110       120       130       140       150       160"
LOCATE 2, 100
COLOR 4, 0
PRINT "Hello, World"
COLOR 7, 0
PAUSE


sub sendAltEnter()
    ' Simulate pressing Alt key
    keybd_event(VK_MENU, 0, 0, 0)

    ' Simulate pressing Enter key
    keybd_event(VK_RETURN, 0, 0, 0)

    ' Simulate releasing Enter key
    keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0)

    ' Simulate releasing Alt key
    keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0)
end sub


Seems to work, screenshot attached.

AIR.

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2392
    • View Profile
Re: ncurses
« Reply #10 on: June 10, 2023, 08:23:39 PM »
Unfortunately, no love under conhost on Windows 10.  C'est la Vie!

airr

  • Sr. Member
  • ****
  • Posts: 252
    • View Profile
Re: ncurses
« Reply #11 on: June 12, 2023, 11:44:04 AM »
Unfortunately, no love under conhost on Windows 10.  C'est la Vie!

Can you try this version?

Code: [Select]
fullscreen()
COLOR 7, 0
CLS
COLOR 15, 1
PRINT "0        10        20        30        40        50        60        70        80        90        100       110       120       130       140       150       160"
LOCATE 2, 100
COLOR 4, 0
PRINT "Hello, World"
COLOR 7, 0
PAUSE
fullscreen()

sub fullscreen()
    keybd_event(VK_MENU,0x38,0,0);
    keybd_event(VK_RETURN,0x1c,0,0);
    keybd_event(VK_RETURN,0x1c,KEYEVENTF_KEYUP,0);
    keybd_event(VK_MENU,0x38,KEYEVENTF_KEYUP,0);
end sub

What the fullscreen sub does/should do is toggle the fullscreen mode.

I tested this (compiled using Pelles) in both Windows Terminal and Conhost on Windows 10 and Windows 11.

AIR.

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2392
    • View Profile
Re: ncurses
« Reply #12 on: June 12, 2023, 11:57:38 AM »
Perfect!

I pasted your code into BED, built and ran the app from inside BED.  Hello full screen.

This is going in my snippets folder.  Thanks again AIR