Tuesday, 16 July 2024

Crafting an Interactive Real-Time Countdown with User Input in Bash


 In this post, we explore a practical application of Bash scripting for creating an interactive real-time countdown that also incorporates user input. The idea is to present a series of questions from a file with a countdown for each, prompting the user to respond within a specified time limit. This script can be useful for quiz applications or timed tests.

Problem Statement

The task is to display a countdown timer alongside questions from a text file (QuestionBank.txt). The user has a fixed amount of time to answer each question before the script automatically proceeds to the next one. The challenge lies in managing the timer and user input simultaneously without cluttering the terminal output.

Solution Overview

We will use Bash’s built-in commands and terminal manipulation utilities (tput) to create a clear and user-friendly interface. The script will handle:

  • Displaying a countdown for each question.
  • Reading a user input within the time limit.
  • Preserving the terminal’s previous output while updating the timer.

The Script

Below is an enhanced version of the Bash script which addresses the requirements and improves upon the provided examples:

#!/bin/bash

# Load questions from the file
exec 3<QuestionBank.txt

# Function to display questions and countdown
ask_question() {
    local timer=10  # Set the countdown time for each question
    while IFS= read -r question && [[ $question ]]; do
        echo "$question"
    done <&3

    # Countdown loop
    for ((t=$timer; t>=0; t--)); do
        # Save cursor position and clear the line
        tput sc
        echo -ne "\rTime remaining: $t seconds"

        # Try to read a single character as answer
        read -p ' Your answer: ' -t 1 -n 1 answer && break

        # Restore cursor position
        tput rc
    done
    echo -e "\n"
}

# Main loop to process each set of 6 lines as a question
while true; do
    tput clear  # Clear the screen for each new question
    if ! ask_question; then
        break  # Exit if no more questions
    fi
    echo "Processing answer: $answer"
    # Add logic here to process the answer
done

# Clean up file descriptor
exec 3<&-

# Reset terminal settings
tput rmcup
echo "Quiz completed."

Explanation

  1. File Handling: We open QuestionBank.txt and assign it to file descriptor 3 for better control over the file data flow.

  2. Question Display: The function ask_question reads and displays questions from the file. Each question is allowed a 10-second timeframe for the user to respond.

  3. Countdown and Input: Inside the countdown loop, the script saves the cursor’s position before displaying the timer, then attempts to read an answer. If the user provides an answer before the timeout, the loop breaks early.

  4. Terminal Management: tput sc and tput rc are used to save and restore the cursor position, making the output flicker-free and cleaner. tput clear ensures each question appears at the top of the terminal window.

  5. Ending the Quiz: The script loops until all questions are processed. File descriptor 3 is closed, and terminal settings are reset after the quiz completion.

This script effectively manages simultaneous output and input in Bash, providing a robust foundation for interactive timed quizzes or tests. By leveraging tput and careful management of terminal behavior, we can create sophisticated shell applications that are both practical and user-friendly.

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home