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

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 พ.ย. 2024

ความคิดเห็น • 62

  • @brunoferreira1125
    @brunoferreira1125 หลายเดือนก่อน

    great video - thanks Gregg. This is the glue I was missing in my C studies....

  • @martinausdeutschland
    @martinausdeutschland ปีที่แล้ว +7

    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  ปีที่แล้ว +3

      Yeah, It has been pointed out to me. In later videos, I have it fullscreen.

  • @michaelkohlhaas4427
    @michaelkohlhaas4427 3 ปีที่แล้ว +1

    Yes, do a full featured follow-up program! Would be awesome! 😎

  • @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  ปีที่แล้ว +1

      Glad it helped.

    • @NOPerative
      @NOPerative ปีที่แล้ว

      @@GreggInkCodes Did more than help - it inspired. Seriously. Thank you!

  • @Jeffrey_Bezos
    @Jeffrey_Bezos 3 ปีที่แล้ว +1

    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!!

  • @FelixNielsen
    @FelixNielsen ปีที่แล้ว

    This is brilliant. Thanks.

  • @aldrickdev
    @aldrickdev 11 หลายเดือนก่อน

    Would love follow up to this. Great video!!

  • @guillemtz5079
    @guillemtz5079 2 ปีที่แล้ว +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 ปีที่แล้ว +1

      Thanks for the comment. I will consider making a video about that.

  • @HarshAnalysis
    @HarshAnalysis 3 ปีที่แล้ว

    Great video and great presentation dude. Keep em coming.

  • @weeb3277
    @weeb3277 3 ปีที่แล้ว +6

    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 ปีที่แล้ว +2

      Ok, I will try to think of that for next time.

    • @gabe.henrique
      @gabe.henrique 3 ปีที่แล้ว

      I tile em nowadays much nicer vybe / less eyestrain / get to flex my batman wallpaper

    • @TreeLuvBurdpu
      @TreeLuvBurdpu 6 หลายเดือนก่อน

      ​@@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.

  • @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.

  • @diceandbricks
    @diceandbricks 3 ปีที่แล้ว +1

    Hi, excellent video. Where can I find a reference of the escape codes?

    • @GreggInkCodes
      @GreggInkCodes  3 ปีที่แล้ว +2

      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.

  • @philtoa334
    @philtoa334 2 ปีที่แล้ว

    😲 thx !

  • @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!

  • @danquinn1773
    @danquinn1773 3 ปีที่แล้ว +1

    Very cool! Do you have any advice on listening for keyboard events in a C program? (on Linux/MacOS)

    • @GreggInkCodes
      @GreggInkCodes  3 ปีที่แล้ว +1

      Nothing comes to mind immediately but this seems a good idea as a topic for a future video.

  • @ihspan6892
    @ihspan6892 2 ปีที่แล้ว +1

    Sir, excellent video, but you could make the terminal fill the screen and make the font larger. Thank you.

    • @GreggInkCodes
      @GreggInkCodes  2 ปีที่แล้ว

      Yeah, I realize that now but those were the early days of me making videos.

  • @hackpace
    @hackpace ปีที่แล้ว

    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  ปีที่แล้ว +1

      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 ปีที่แล้ว

      @@GreggInkCodes thanks bro, it good to know you

  • @ucchungnguyen7422
    @ucchungnguyen7422 2 ปีที่แล้ว

    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  2 ปีที่แล้ว +1

      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 (th-cam.com/video/31p6xsjqehM/w-d-xo.html). 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.

  • @Leozxxz
    @Leozxxz 3 ปีที่แล้ว +2

    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 ปีที่แล้ว +3

    bloat != progress

  • @philipdifrancesca
    @philipdifrancesca ปีที่แล้ว +1

    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 :(

  • @stevenlalewicz6260
    @stevenlalewicz6260 2 ปีที่แล้ว

    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  2 ปีที่แล้ว +2

      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 2 ปีที่แล้ว

      @@GreggInkCodes Thank you very much. The 1st link has far more comprehensible info than I've been able to find so far.

  • @Gupatik
    @Gupatik 2 ปีที่แล้ว

    underrated content, plz continue if you can, why did you quite so?

  • @xvolcomstyla2942
    @xvolcomstyla2942 8 หลายเดือนก่อน

    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

    • @DatBoi_TheGudBIAS
      @DatBoi_TheGudBIAS 2 หลายเดือนก่อน

      Include windows.h and u have the windows API console functions to use

  • @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 ปีที่แล้ว +3

      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!

  • @frakman1
    @frakman1 ปีที่แล้ว

    14:15 What's the magic number -12 for?

    • @GreggInkCodes
      @GreggInkCodes  ปีที่แล้ว

      12 is the length of the string "Hello World!" It is needed to calculate where to display the string so it will be centered.

  • @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.

  • @weeb3277
    @weeb3277 3 ปีที่แล้ว +1

    Can you do a video on just stdout, stdin, stderr?

    • @GreggInkCodes
      @GreggInkCodes  3 ปีที่แล้ว

      Anything in particular you would like to know about that?

  • @nexus1g
    @nexus1g ปีที่แล้ว

    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 ปีที่แล้ว

      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  ปีที่แล้ว

      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 ปีที่แล้ว

      @@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 ปีที่แล้ว +1

      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.

  • @kacperfilipek8461
    @kacperfilipek8461 2 ปีที่แล้ว

    5:35 why make tc_move_cursor a macro instead of a function?