Fix: improve notification display formatting and add global exception handling

- add truncateString utility for long notification titles
- improve notification table alignment
- remove unnecessary heading spacing in notification preferences
- add application-level exception handling in UserInterface::run()

Fixes #1777
This commit is contained in:
2026-05-27 12:19:34 +05:30
parent a9c8ec93b7
commit 83e2bed432
3 changed files with 63 additions and 22 deletions
@@ -21,4 +21,28 @@ namespace util
{ {
std::cout << "\x1B[2J\x1B[H" << std::flush; std::cout << "\x1B[2J\x1B[H" << std::flush;
} }
/*
Function: truncateString
Description:
Truncates a string if its length exceeds the given maximum length.
The truncated string ends with "..." to indicate omitted characters.
Parameters:
- text: const std::string&, input string to truncate
- maxLength: size_t, maximum allowed length of the returned string
Returns:
- std::string: Original string if within limit, otherwise truncated string with "..."
*/
inline std::string truncateString(const std::string& text, size_t maxLength)
{
if (text.length() <= maxLength)
{
return text;
}
if (maxLength <= 3)
{
return std::string(maxLength, '.');
}
return text.substr(0, maxLength - 3) + "...";
}
} }
@@ -553,9 +553,9 @@ inline const Notification* selectNotification(const util::Vector<const Notificat
{ {
util::Map<int, const Notification*> indexedNotifications; util::Map<int, const Notification*> indexedNotifications;
std::cout << std::left std::cout << std::left
<< std::setw(6) << "Index" << std::setw(10) << "Index"
<< std::setw(15) << "ID" << std::setw(15) << "ID"
<< std::setw(30) << "Title" << std::setw(35) << "Title"
<< std::setw(25) << "Timestamp" << std::setw(25) << "Timestamp"
<< std::endl; << std::endl;
int currentIndex = 1; int currentIndex = 1;
@@ -565,9 +565,9 @@ inline const Notification* selectNotification(const util::Vector<const Notificat
if (currentNotification) if (currentNotification)
{ {
std::cout << std::left std::cout << std::left
<< std::setw(6) << currentIndex << std::setw(10) << currentIndex
<< std::setw(15) << currentNotification->getId() << std::setw(15) << currentNotification->getId()
<< std::setw(35) << currentNotification->getTitle() << std::setw(35) << util::truncateString(currentNotification->getTitle(), 30)
<< std::setw(25) << currentNotification->getCreatedAt().toString() << std::setw(25) << currentNotification->getCreatedAt().toString()
<< std::endl; << std::endl;
indexedNotifications.insert(currentIndex, currentNotification); indexedNotifications.insert(currentIndex, currentNotification);
@@ -886,7 +886,7 @@ inline bool getNotificationPreference(const std::string& serviceName)
while (true) while (true)
{ {
util::clear(); util::clear();
std::cout << " Configure Notification Preferences\n"; std::cout << "Configure Notification Preferences\n";
std::cout << "\n" << serviceName << " Notifications\n"; std::cout << "\n" << serviceName << " Notifications\n";
std::cout << "1. Enable Notifications\n"; std::cout << "1. Enable Notifications\n";
std::cout << "2. Disable Notifications\n"; std::cout << "2. Disable Notifications\n";
@@ -7,6 +7,8 @@ Author: Trenser
Date:19-May-2026 Date:19-May-2026
*/ */
#include <iostream>
#include <stdexcept>
#include "Enums.h" #include "Enums.h"
#include "InputHelper.h" #include "InputHelper.h"
#include "OutputHelper.h" #include "OutputHelper.h"
@@ -23,29 +25,44 @@ Return type: void
*/ */
void UserInterface::run() void UserInterface::run()
{ {
m_controller.loadSystemData(); try
m_controller.runSystemChecks();
bool isMenuActive = true;
while (isMenuActive)
{ {
try m_controller.loadSystemData();
m_controller.runSystemChecks();
bool isMenuActive = true;
while (isMenuActive)
{ {
int choice; try
util::clear();
std::cout << "Vehicle Service System\n1. Login\n2. Register Customer\n3. Exit\nEnter your Choice: ";
util::read(choice);
if (!handleOperation(choice))
{ {
isMenuActive = false; int choice;
util::clear();
std::cout << "Vehicle Service System\n1. Login\n2. Register Customer\n3. Exit\nEnter your Choice: ";
util::read(choice);
if (!handleOperation(choice))
{
isMenuActive = false;
}
}
catch (const std::exception& e)
{
std::cout << "Exception: " << e.what() << std::endl;
util::pressEnter();
} }
} }
catch (const std::exception& e) m_controller.saveSystemData();
{ }
std::cout << "Exception: " << e.what() << std::endl; catch (const std::invalid_argument& exception)
util::pressEnter(); {
} std::cout << "Exception: Invalid Argument: " << exception.what() << std::endl;
}
catch (const std::exception& exception)
{
std::cout << "Exception: " << exception.what() << std::endl;
}
catch (...)
{
std::cout << "Unknown error occurred." << std::endl;
} }
m_controller.saveSystemData();
} }
/* /*