Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hol-formatter example from the book doesn't work #2794

Open
RobberFox opened this issue Sep 28, 2024 · 4 comments
Open

hol-formatter example from the book doesn't work #2794

RobberFox opened this issue Sep 28, 2024 · 4 comments
Labels
userquestion not quite bugs--inquiries from users

Comments

@RobberFox
Copy link

RobberFox commented Sep 28, 2024

I'm just trying to copy the code from the book to see if it works but it doesn't:

#include <stdlib.h>
#include <notcurses/direct.h>

int main(void){
	const char blue[] = "house";
	const char *b = blue;
	struct ncdirect* n = ncdirect_core_init(NULL, stdout, 0);

	int c, ret = 0;
	if(n){
		while(!ret && (c = getchar()) != EOF){
			if(isalpha(c) && tolower(c) == *b){
				++b;
			}else{
				if(b > blue){
					if(!*b){
						ret |= ncdirect_on_styles(n, NCSTYLE_BOLD);
						ret |= ncdirect_set_fg_rgb(n, 0x0339dc);
					}
					ret |= (printf("%.*s", (int)(b - blue), blue) < 0);
					if(!*b){
						ret |= ncdirect_set_fg_default(n);
						ret |= ncdirect_off_styles(n, NCSTYLE_BOLD);
					}
					b = blue;
				}
				ret |= (putchar(c) == EOF);
			}
		}
		if(b > blue){
			ret |= (printf("%.*s", (int)(b - blue), blue) < 0);
		}
	}
	return (!n || ncdirect_stop(n) || !feof(stdin) || ret) ? EXIT_FAILURE : EXIT_SUCCESS;
}

The code compiles with no errors: gcc -Wall hol-formatter.c -lnotcurses -lnotcurses-core, but when I try to execute echo "house of leaves" | ./a.out, nothing is outputted.
I've tested some executables: notcurses-demo, ncplayer, notcurses-info and they all seem to work fine:

notcurses 3.0.9 on Kitty 0.36.4 (Linux 6.1.0-23-amd64)
51 rows (20px) 126 cols (9px) 1020x1134 rgb+256 colors
gcc-12.2.0 (LE)
terminfo 6.4.20221231 libdeflate 1.14 GPM n/a
avformat 59.27.100 avutil 57.28.100 swscale 6.7.100 avcodec 59.37.100 avdevice 59.7.100
af+ ab+ sum- vpa+ hpa+ sgr0+ op+ fgop+ bgop+ bce- rect-                         
bold+ ital+ struck+ ucurl+ uline+ u7+ ccc+ rgb+ el+                             
utf8+ 2x1+ 2x2+ 3x2+ 4x2+ img+ vid+ indn+ gpm- kbd+                             
default fg 0xc8d3f5 default bg 0x1a1b26 pmouse+                                 
1st gen rgba pixel animation support                   🯁🯂🯃https://notcurses.com 
 ▘▝▀▖▌▞▛▗▚▐▜▄▙▟█⎧ 🬀🬁🬂🬃🬄🬅🬆🬇🬈🬊🬋🬌🬍🬎🬏🬐🬑🬒🬓▌🬔🬕🬖🬗🬘🬙🬚🬛🬜🬝⎫♠♥🯰🯱🯲🯳🯴🯵🯶🯷🯸🯹⅗⅘⅙⅚⅛⎧▕▏⎫┌╥─╥─╥┐🭩⎛⎞
╲╿╱ ◨◧ ◪◩ ◖◗ ⫷⫸ ⎩🬟🬠🬡🬢🬣🬤🬥🬦🬧▐🬨🬩🬪🬫🬬🬭🬮🬯🬰🬱🬲🬳🬴🬵🬶🬷🬸🬹🬺🬻█⎭♦♣¼½¾⅐⅑⅒⅓⅔⅕⅖⅜⅝⅞⅟↉⎪🮇▎⎪├╜╓╫╖╙┤🭫⎜⎟
╾╳╼ ◲◱ ◶◵ 🮣🮠 🮤🮥◜◝ ◿◺ 🮞🮟 ◢◣ ┌┐─ ┏┓━ ╭╮─ ╔╗═ 🭽🭾▁♟♜♞⩘▵△▹▷▿▽◃◁⭡⭣⭠⭢⭧⭩⭦⭨⎪🮈▍⎪├─╨╫╨─┤┇⎜⎟
╱╽╲ ◳◰ ◷◴ 🮡🮢 🮦🮧◟◞ ◹◸ 🮝🮜 ◥◤ └┘│ ┗┛┃ ╰╯│ ╚╝║ 🭼🭿🭵♝♛♚⩗▴⏶⯅▲▸⏵⯈▶▾⏷⯆▼◂⏴⯇◀⎪▐▌⎪╞═╤╬╤═╡┋⎜⎟
⎡⠀⠁⠈⠉⠂⠃⠊⠋⠐⠑⠘⠙⠒⠓⠚⠛⠄⠅⠌⠍⠆⠇⠎⠏⠔⠕⠜⠝⠖⠗⠞⠟⠠⠡⠨⠩⠢⠣⠪⠫⠰⠱⠸⠹⠲⠳⠺⠻⠤⠥⠬⠭⠦⠧⠮⠯⠴⠵⠼⠽⠶⠷⠾⠿⎤⎨🮉▋⎬╞╕╘╬╛╒╡┊⎜⎟
⎢⡀⡁⡈⡉⡂⡃⡊⡋⡐⡑⡘⡙⡒⡓⡚⡛⡄⡅⡌⡍⡆⡇⡎⡏⡔⡕⡜⡝⡖⡗⡞⡟⡠⡡⡨⡩⡢⡣⡪⡫⡰⡱⡸⡹⡲⡳⡺⡻⡤⡥⡬⡭⡦⡧⡮⡯⡴⡵⡼⡽⡶⡷⡾⡿⎥⎪🮊▊⎪└┴─╨─┴┘╏⎝⎠
⎢⢀⢁⢈⢉⢂⢃⢊⢋⢐⢑⢘⢙⢒⢓⢚⢛⢄⢅⢌⢍⢆⢇⢎⢏⢔⢕⢜⢝⢖⢗⢞⢟⢠⢡⢨⢩⢢⢣⢪⢫⢰⢱⢸⢹⢲⢳⢺⢻⢤⢥⢬⢭⢦⢧⢮⢯⢴⢵⢼⢽⢶⢷⢾⢿⎥⎪🮋▉⎪╭──╮⟬⟭╔╗≶≷
⎣⣀⣁⣈⣉⣂⣃⣊⣋⣐⣑⣘⣙⣒⣓⣚⣛⣄⣅⣌⣍⣆⣇⣎⣏⣔⣕⣜⣝⣖⣗⣞⣟⣠⣡⣨⣩⣢⣣⣪⣫⣰⣱⣸⣹⣲⣳⣺⣻⣤⣥⣬⣭⣦⣧⣮⣯⣴⣵⣼⣽⣶⣷⣾⣿⎦⎪██⎪│╭╮│╔═╝║⊆⊇
 ▔🭶🭷🭸🭹🭺🭻▁ 🭁🭌 🭂🭍 🭃🭎 🭄🭏 🭅🭐 🭆🭑 🭇🬼 🭈🬽 🭉🬾 🭊🬿 🭋🭀 ₀₁₂₃₄₅₆₇₈₉ ⎛ ▁▂▃▄▅▆▇█🭫⎞⎪🭨🭪⎪╰╯││║╔═╝⊴⊵
 ▏🭰🭱🭲🭳🭴🭵▕ 🭒🭝 🭓🭞 🭔🭟 🭕🭠 🭖🭡 🭧🭜 🭢🭗 🭣🭘 🭤🭙 🭥🭚 🭦🭛 ⁰¹²³⁴⁵⁶⁷⁸⁹ ⎝ ▔🮂🮃▀🮄🮅🮆█🭩⎠⎩🭪🭨⎭⧒⧑╰╯╚╝❨❩⟃⟄                       
 
17 renders, 2.70ms (144.38µs min, 159.01µs avg, 305.16µs max)
17 rasters, 1.23ms (63.31µs min, 72.17µs avg, 86.16µs max)
17 writes, 1.44ms (33.07µs min, 84.52µs avg, 860.85µs max)
63.87KiB (103B min, 3.76KiB avg, 60.19KiB max) 0 inputs Ghpa: 0
0 failed renders, 0 failed rasters, 0 refreshes, 0 input errors
RGB emits:elides: def 2:1300 fg 1:2477 bg 1159:17
Cell emits:elides: 2478:215934 (98.87%) 99.85% 99.96% 1.45%
Bmap emits:elides: 1:0 (0.00%) 36.11KiB (56.54%) SuM: 0 (0.00%)

What's the problem?

P.S. I'm just complete noobie trying to use this library for my university project. I might be doing something wrong.

@RobberFox RobberFox added the userquestion not quite bugs--inquiries from users label Sep 28, 2024
@kfazz
Copy link

kfazz commented Sep 30, 2024

The book targets v1.2.4 of notcurses, so if you're using the latest release YMMV.
I tried some good old printf debugging, and it looks like the while loop doesn't run.
Adding a call to getchar() before ncdirect_core_init() 'fixes' it. No idea why yet though.

@kfazz
Copy link

kfazz commented Sep 30, 2024

Looks like the first character of input is treated as an escape. You can also just run hol-formatter directly and type into it to test. It will consume the first char, and then work as intended.
image
This is the same as the book version, with the NCDIRECT_OPTION_VERBOSE flag passed to ncdirect_core_init()

@RobberFox
Copy link
Author

Since the book can have broken code - are the man pages the only up-to-date resource left?

@kfazz
Copy link

kfazz commented Sep 30, 2024

There's also the wiki:
https://nick-black.com/dankwiki/index.php/Notcurses

autogenerated api docs w/ doxygen:
https://notcurses.com/html/

and demo progs in the src tree:
https://github.com/dankamongmen/notcurses/tree/master/src

checkout the poc directory, the demo, etc.

You may also get code completion / function docs in your IDE depending on your setup.
(I get this in vscode out of the box with the library installed in the standard location via debian package, for example)

It's also probably instructive to try the demos first, and see what is/isn't broken.
Only the first two examples use direct mode, the other ones might still work.

The code from the books is online as well, in case you're typing these in out of the book:
https://github.com/dirty-south-supercomputing/technicalreports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
userquestion not quite bugs--inquiries from users
Projects
None yet
Development

No branches or pull requests

2 participants