Take Control of the Terminal using C (Colours, Move Cursor, Clear Screen and more)

  Рет қаралды 14,445

Gregg Ink

Gregg Ink

Күн бұрын

Find the file tc.h on gitlab: gitlab.com/greggink
00:00 Intro (also Why?)
01:42 Hello World program
02:08 Colours
04:37 Clear Screen
05:24 Move Cursor
06:18 Background Colours
07:28 Output to other terminal
12:43 Size of the Terminal
14:42 Resizing
17:31 Alternate Screen
19:09 Turning off Echo
21:33 Outro
If you believe I deserve a free coffee for my efforts, you can buy me one here: ko-fi.com/greggink

Пікірлер: 60
@Jeffrey_Bezos
@Jeffrey_Bezos 3 жыл бұрын
that’s really cool. would love to see more
@Eliasdefi
@Eliasdefi 2 жыл бұрын
Really nice bro i really enjoyed. Keep up the good content!!! Greetings from Argentina!!
@michaelkohlhaas4427
@michaelkohlhaas4427 2 жыл бұрын
Yes, do a full featured follow-up program! Would be awesome! 😎
@HarshAnalysis
@HarshAnalysis 3 жыл бұрын
Great video and great presentation dude. Keep em coming.
@GreggInkCodes
@GreggInkCodes 3 жыл бұрын
Thanks, will do!
@martinausdeutschland
@martinausdeutschland Жыл бұрын
Hi Gregg, thanks for your info. I think about minimalism the same way as you do. Just, for future videos, it would be great, if the virtual terminal would be much bigger. Currently it is about a 9th of the video screen.
@GreggInkCodes
@GreggInkCodes Жыл бұрын
Yeah, It has been pointed out to me. In later videos, I have it fullscreen.
@FelixNielsen
@FelixNielsen Жыл бұрын
This is brilliant. Thanks.
@NOPerative
@NOPerative Жыл бұрын
Neat. Awesome. Good vid. Totally agree with statements regarding independence and familiarity with the tools and language one would use to develop, and the terminal is inescapable (even in Windoze afic). The varied examples and use case scenarios are brilliant, common concerns and illustrated concisely; fantastic work!
@GreggInkCodes
@GreggInkCodes Жыл бұрын
Glad it helped.
@NOPerative
@NOPerative Жыл бұрын
@@GreggInkCodes Did more than help - it inspired. Seriously. Thank you!
@aldrickdev
@aldrickdev 6 ай бұрын
Would love follow up to this. Great video!!
@guillemtz5079
@guillemtz5079 2 жыл бұрын
Hi!, great content. Super inspiring for noobie C programmers like myself. I would love to understand more about bitops/masks and its uses. Thanks! One observation: I reckon the '1' parameter for SGR is bold and '0' is for normal :)
@GreggInkCodes
@GreggInkCodes 2 жыл бұрын
Thanks for the comment. I will consider making a video about that.
@mrpablodelosmarbles7966
@mrpablodelosmarbles7966 3 жыл бұрын
Exactly what i needed! :)
@GreggInkCodes
@GreggInkCodes 3 жыл бұрын
You are welcome.
@mrpablodelosmarbles7966
@mrpablodelosmarbles7966 3 жыл бұрын
@@GreggInkCodes Does this also work on the BSD's?
@GreggInkCodes
@GreggInkCodes 3 жыл бұрын
@@mrpablodelosmarbles7966 Sorry, I have no experience with BSD.
@philipdifrancesca
@philipdifrancesca 9 ай бұрын
Also when you use a library (in any lang/framework) you lose control of your project recursively, because you don't control what other libraries that library calls, and what other-other libraries the other libraries call, and so on... Which is where we are today, with exponentially more computers made of expontentially better hardware, connected by exponentialialy faster networks, but still the user experience is slower than it was 20 years ago :(
@Leozxxz
@Leozxxz 3 жыл бұрын
Nice video, it makes me want to code a vim wannabe lol. 4 years ago, me and a couple o friends started a a shell, but it didnt last much. Maybe in another video you could start one as well.
@weeb3277
@weeb3277 3 жыл бұрын
Why not zoom into your terminal? You have so much space left on the sides and the top and the bottom of the screen. Why not increase the font size too?
@GreggInkCodes
@GreggInkCodes 3 жыл бұрын
Ok, I will try to think of that for next time.
@gabe.henrique
@gabe.henrique 2 жыл бұрын
I tile em nowadays much nicer vybe / less eyestrain / get to flex my batman wallpaper
@TreeLuvBurdpu
@TreeLuvBurdpu 2 ай бұрын
​@@GreggInkCodesyou can zoom the terminal but also 80% of your screen space is used up by useless, unchanging, low-content desktop background. Us coding-junkies like to view these type of videos all day long and that means also on our phones and then dream about them in our sleep. All we care about is the text part, because from that we can render the rest in our dream-canvas.
@philtoa334
@philtoa334 Жыл бұрын
😲 thx !
@xvolcomstyla2942
@xvolcomstyla2942 4 ай бұрын
19:09 if you want to do that in windows you need the termios.h header file and im not sure if this is even possible on dos/windows architecture
@kacperfilipek8461
@kacperfilipek8461 2 жыл бұрын
5:35 why make tc_move_cursor a macro instead of a function?
@Gupatik
@Gupatik Жыл бұрын
underrated content, plz continue if you can, why did you quite so?
@danquinn1773
@danquinn1773 3 жыл бұрын
Very cool! Do you have any advice on listening for keyboard events in a C program? (on Linux/MacOS)
@GreggInkCodes
@GreggInkCodes 3 жыл бұрын
Nothing comes to mind immediately but this seems a good idea as a topic for a future video.
@lanatrzczka
@lanatrzczka Жыл бұрын
I tried it in Windows, discovered that there are some includes from the SYS directory which obviously Win doesn't have. But thank you for the lesson!
@weeb3277
@weeb3277 3 жыл бұрын
bloat != progress
@stevenlalewicz6260
@stevenlalewicz6260 Жыл бұрын
At the moment, I'm trying to learn this stuff myself. I've used ncurses but it does not quite suite my needs and I'd like to see if I can do it myself. I've been looking into some of the APIs you have been using. You have given a lot of great tips. I had no idea the ANSI escape sequences could save and restore the terminal state. Well a video on ANSI escape sequences would be awesome :) or give a link to any resources you have used, thanks.
@GreggInkCodes
@GreggInkCodes Жыл бұрын
It has been a while so I don't remember exactly what I used as resources. I had a quick look through my bookmarks. Of course, not all bookmarks turn out to be equally useful in the end. Anyways, here they are: 1) invisible-island.net/xterm/ctlseqs/ctlseqs.html 2) wiki.bash-hackers.org/scripting/terminalcodes 3) en.wikipedia.org/wiki/ANSI_escape_code
@stevenlalewicz6260
@stevenlalewicz6260 Жыл бұрын
@@GreggInkCodes Thank you very much. The 1st link has far more comprehensible info than I've been able to find so far.
@ucchungnguyen7422
@ucchungnguyen7422 Жыл бұрын
I want to build a function to get keycode from keyboard like getkeycode(uint64_t *c) .How I can build it by using your tc.h header files. I don't know how to cancel your thread in tc_init_input function. Please help me. Many Thanks !!!
@GreggInkCodes
@GreggInkCodes Жыл бұрын
I am a bit confused by your question. This comment is under the first video in the series which doesn't deal with keyboard input. If you want to deal with keyboard input yourself, get tc.h from the gitlab project which belongs to this video (gitlab.com/greggink/youtube_episode_control_terminal/). The second video in this series explains how I deal with keyboard input (kzbin.info/www/bejne/aWLTZ6upn9aYnq8). This has its own gitlab project (gitlab.com/greggink/youtube_episode_terminal_control_2/) and this is where you find tc_init_input. How to cancel the thread in tc_init_input ? Well, I don't know why you would want to do that but if you do, just comment the line beginning with pthread_create(... Unless, I am misunderstanding your question. Hope this helps.
@diceandbricks
@diceandbricks 3 жыл бұрын
Hi, excellent video. Where can I find a reference of the escape codes?
@GreggInkCodes
@GreggInkCodes 3 жыл бұрын
The following link might help ( wiki.bash-hackers.org/scripting/terminalcodes) Note, don't trust anything until you have tested it for yourself. The VT100 is an obsolete terminal which was historically influential and that's why it is mentioned in the link.
@diceandbricks
@diceandbricks 3 жыл бұрын
@@GreggInkCodes by the way I also enjoyed the historical context.
@lineber2
@lineber2 3 жыл бұрын
Good topic, I was trying to watch it on my phone but the emulator (1/4 screen size) was not opened to full screen. So I will have to watch it at home later.
@GreggInkCodes
@GreggInkCodes 3 жыл бұрын
Sorry about that. I am always on my laptop and never think about phones. I will have it fullscreen next time.
@guillemtz5079
@guillemtz5079 2 жыл бұрын
Hi Gregg, one question. Why does one have to subtract 12 to the column value? I can see in my own program, that it's the only way to fit the text in the exact middle. I just would like to understand why. Thanks!
@GreggInkCodes
@GreggInkCodes 2 жыл бұрын
To get the text in the center, you need the empty space on either side to be the same. The empty space is the full width of the screen - the width of the text. Divide the difference by 2 to get equal space on either side. The length of the string in this case is 12. You could use a strlen() to make the solution more general. Of course, what makes this works is the monospace font and the fact that the width of every character matches a column.
@guillemtz5079
@guillemtz5079 2 жыл бұрын
@@GreggInkCodes awesome. cheers!
@ihspan6892
@ihspan6892 Жыл бұрын
Sir, excellent video, but you could make the terminal fill the screen and make the font larger. Thank you.
@GreggInkCodes
@GreggInkCodes Жыл бұрын
Yeah, I realize that now but those were the early days of me making videos.
@weeb3277
@weeb3277 3 жыл бұрын
Can you do a video on just stdout, stdin, stderr?
@GreggInkCodes
@GreggInkCodes 3 жыл бұрын
Anything in particular you would like to know about that?
@hackpace
@hackpace 10 ай бұрын
Please is there any documentation on cursor code just like the color code which is documented in terminal-colors.d 5, i will be grateful if you can help me out thank you
@GreggInkCodes
@GreggInkCodes 10 ай бұрын
Yes, there is. ( man7.org/linux/man-pages/man4/console_codes.4.html ) I played around with it a bit to test, it's working. e.g. write(1, "\033[30G", 5 ); will move cursor to column 30 of current row.
@hackpace
@hackpace 10 ай бұрын
@@GreggInkCodes thanks bro, it good to know you
@frakman1
@frakman1 9 ай бұрын
14:15 What's the magic number -12 for?
@nexus1g
@nexus1g 11 ай бұрын
I really appreciate this video. I came across it trying to find out how to understand the gobbledegook that is, e.g., "\x1B[0;31m," as I came across similar formats to do a platform-agnostic screen clear. What does all of that represent? It reads to me like an escape sequence, but I'm not sure what exactly it's doing.
@nexus1g
@nexus1g 11 ай бұрын
I just got the idea to do an exact match search for the string of characters and found that this relates to ANSI 6429--right? Finding this really helps me understand what's going on now.
@GreggInkCodes
@GreggInkCodes 11 ай бұрын
The "\x1B[0;31m," is a control escape sequence. The "\x," means that whatever follows is a hexadecimal number. Then we have 1B in hex or 27 in base 10. This is the ASCII code for Escape, hence the whole thing being called an escape sequence. What follows after that is just a code to control what you want the terminal to do: change colour, clear screen, etc.
@nexus1g
@nexus1g 11 ай бұрын
@@GreggInkCodes Thanks! Now I'm even more curious. Do you know the reason they drop the standard leading zero before the x in the hex? Is it for brevity or to be more universally agreeable with compilers and CLI's? Maybe some other reason? And that makes me also wonder what are we "escaping" from? Is it similar to a system() call to the CLI outside of the binary itself?
@kacperfilipek8461
@kacperfilipek8461 2 жыл бұрын
5:35 why make tc_move_cursor a macro instead of a function?
@GreggInkCodes
@GreggInkCodes 2 жыл бұрын
Functions produce a small amount of overhead as they require a new layer on the stack. Macros don't have that overhead and thus are more efficient. For very simple functions, I like to implement them as macros.
Arenas, strings and Scuffed Templates in C
12:28
VoxelRifts
Рет қаралды 80 М.
Understanding text for C Programmers (UTF-8, Unicode, ASCII)
14:36
Became invisible for one day!  #funny #wednesday #memes
00:25
Watch Me
Рет қаралды 59 МЛН
DEC VT320: The Classic 1987 Library Computer Terminal
10:22
Linux Terminal Game from Scratch I - Termios, ANSI Escape Codes
28:52
Programming a multiplayer game from scratch in 7 DAYS
18:28
How different are C and C++? Can I still say C/C++?
10:25
Jacob Sorber
Рет қаралды 219 М.
why do header files even exist?
10:53
Low Level Learning
Рет қаралды 379 М.
Draw pixels on screen in C or C++
35:31
Arte Birklaus
Рет қаралды 3,3 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 289 М.
Advanced C: The UB and optimizations that trick good programmers.
1:12:34
Eskil Steenberg
Рет қаралды 162 М.
Features of a Modern Terminal Emulator
17:59
spudlyo
Рет қаралды 7 М.