Display Students
A function made to display all students data from database
About: This function is responsible for displaying the contents of students files within a displaying window with with buttons support to view the preview and next student or leave. It reads the file line by line and prints each line on the window until either the end of the file is reached or the maximum number of lines to display is reached.
1. displayFilesContent()
function:
displayFilesContent()
function:1.1: Description:
This function reads the contents of a file specified by the filename
parameter and displays them on the window win
. It displays the lines of the file until either the end of the file is reached or the maximum number of lines, specified by the pageHeight
parameter, is displayed.
1.2: Syntax:
Short form:
Full form:
1.3: Parameters:
1.4: Input:
The function requires a valid window object (win
) created using the ncurses library. It also requires a valid filename
pointing to the file to be read and displayed. The pageHeight
parameter determines the maximum number of lines that can be displayed on the window.
1.5: Output:
The function reads the specified file line by line and displays each line on the window using the wprintw()
function from the ncurses library. It stops displaying lines when either the end of the file is reached or the maximum number of lines defined by pageHeight
is displayed.
1.6: Usage:
We use this function when we want to display the contents of a file on a window created with the ncurses library. Provide the appropriate window, file name, and page height to display the desired file contents
2. displayStudents()
function:
displayStudents()
function:2.1: Description:
This function displays the list of students by opening a window, reading student files from a directory, and allowing the user to navigate through the files and view their contents.
2.2: Syntax:
Short form:
Full form:
2.3: Parameters:
This function does not take any parameters.
2.4: Function Tasks:
Initializing ncurses screen and setting input mode:
initscr()
initializes the ncurses library and prepares the terminal for use.cbreak()
disables line buffering so that input characters are immediately available.noecho()
disables character echoing, so user input is not displayed on the screen.keypad(stdscr, TRUE)
enables the keypad of the user's terminal for special keys.
Getting the maximum screen dimensions:
getmaxyx(stdscr, max_y, max_x)
retrieves the maximum number of rows and columns available on the screen and stores them inmax_y
andmax_x
variables.
Setting the page height:
Calculates the
pageHeight
by subtracting 4 from the maximum number of rows. This ensures space is left for buttons and title.
Creating a new window for displaying student information:
newwin(max_y - 4, max_x, 2, 0)
creates a new window with the specified dimensions (height:max_y - 4
, width:max_x
) starting at coordinates (2, 0).refresh()
updates the screen to reflect the changes.
Opening the directory and counting the number of student files:
opendir("database")
opens the "database" directory and returns a directory stream pointer (dir
) to read its contents.readdir(dir)
reads the next entry from the directory stream, andentry
points to the current entry.entry->d_type == DT_REG
checks if the current entry is a regular file (not a directory or special file).The
totalPages
variable keeps track of the number of student files found.
Displaying error messages if the directory is empty or cannot be opened:
Creating and displaying the title window:
newwin(1, max_x, 0, 0)
creates a new window with a height of 1 row, spans the entire width of the screen (max_x
), and starts at coordinates (0, 0).wattron(titleWin, A_BOLD | A_REVERSE)
enables bold and reverse attributes for the title window.mvwprintw(titleWin, 0, (max_x - 12) / 2, " Students List ")
moves the cursor to row 0 and to the center of the window horizontally, then prints the text " Students List ".wattroff(titleWin, A_BOLD | A_REVERSE)
disables the bold and reverse attributes for the title window.refresh()
updates the screen to reflect the changes.wrefresh(titleWin)
updates the title window to reflect the changes.
Creating and displaying the button window:
newwin(1, max_x, max_y - 2, 0)
creates a new window with a height of 1 row, spans the entire width of the screen (max_x
), and starts two rows above the bottom of the screen (max_y - 2
).mvwprintw(buttonWin, 0, 2, "Previous: Left Arrow")
moves the cursor to row 0 and column 2, then prints the text "Previous: Left Arrow".mvwprintw(buttonWin, 0, (max_x - 12) / 2, "Click Q to exit")
moves the cursor to row 0 and to the center of the window horizontally, then prints the text "Click Q to exit".mvwprintw(buttonWin, 0, max_x - 18, "Next: Right Arrow")
moves the cursor to row 0 and to the right side of the window, leaving a gap of 18 characters, then prints the text "Next: Right Arrow".refresh()
updates the screen to reflect the changes.wrefresh(buttonWin)
updates the button window to reflect the changes.
Last updated