System Calls and Terminal Management

This lesson introduces system calls, and how to use them to clear the terminal

Ryan McCombe
Published

Soon, we'll be working with libraries that help us create fully interactive user interfaces. But, before we move on to that, it's worth quickly covering how we can improve the terminal output.

Even though the products we ship to users generally aren't going to be interacted with on the terminal, Command Line Interfaces (CLIs) are very common for internal tools, intended to be used by other developers.

Currently, our output has always been a vertical stream of text. Typically, a better user experience is to update the existing output on the screen, rather than appending new information below the old.

Clearing the Terminal using System Calls

A common technique to accomplish this is to clear the terminal before sending our next output. To clear the terminal, we can send commands to the underlying operating system, using the system() function.

For example, when using the Windows command prompt, we can clear the screen by typing the command "cls" and pressing enter.

Within our C++ program, we can trigger this same behavior by calling system("cls"):

void ClearScreen() {
  system("cls");
}

In Linux and macOS, the equivalent command is "clear". When we're building for Windows, the compiler defines the _WIN32 preprocessor directive, which we can use to ensure our code is sending the correct system command:

void ClearScreen() {
#if defined _WIN32
  system("cls");
#elif
  system("clear");
#endif
}

A Practical Example

We can use this to create a simple clock application, that updates every second:

#include <chrono>
#include <iomanip>
#include <iostream>
#include <thread>

void ClearScreen() {
#if defined _WIN32
  system("cls");
#elif
  system("clear");
#endif
}

int main() {
  using namespace std::chrono;
  while (true) {
    std::string Time{std::format(
      "Time: {:%H:%M:%OS}", system_clock::now()
    )};

    ClearScreen();
    std::cout << Time;
    std::this_thread::sleep_for(seconds(1));
  }
}
Time: 21:24:50

Next, we'll see how we can extend our use of the terminal to get input from our users, letting our software react to their input.

Summary

In this lesson, we explored how to enhance terminal interfaces by using system calls to clear the screen. This fundamental technique not only improves the user experience for CLI tools but also sets a foundation for more advanced UI management in programming.

Key Takeaways:

  • Learned how to use system() function to execute system-specific commands like cls for Windows and clear for Linux/macOS.
  • Understood the use of preprocessor directives to ensure cross-platform compatibility in clearing the terminal.
  • Developed a simple clock application to apply the concept of clearing the terminal screen.
  • Gained insight into the challenges of flickering in terminal output and an introduction to the concept of double buffering.
  • Prepared the groundwork for more advanced topics like interactive user interfaces and handling user inputs.
Next Lesson
Lesson 59 of 60

User Input in the Terminal

This lesson introduces the fundamentals of capturing user input, using std::cin and std::getline()

Have a question about this lesson?
Answers are generated by AI models and may not have been reviewed for accuracy