|
|
@@ -0,0 +1,69 @@
|
|
|
+#pragma once
|
|
|
+
|
|
|
+#include <string>
|
|
|
+#include <cstddef>
|
|
|
+
|
|
|
+namespace lumacs {
|
|
|
+
|
|
|
+/// @brief Shared message formatting utilities for both GTK and TUI frontends.
|
|
|
+///
|
|
|
+/// This utility provides consistent message formatting including:
|
|
|
+/// - Multi-line message collapsing (show first line + count)
|
|
|
+/// - Message truncation with help hint
|
|
|
+class MessageFormatter {
|
|
|
+public:
|
|
|
+ /// Truncation suffix shown when message is too long
|
|
|
+ static constexpr const char* TRUNCATION_SUFFIX = "... [C-h e]";
|
|
|
+
|
|
|
+ /// @brief Collapse a multi-line message to show only the first line with a count indicator.
|
|
|
+ /// @param message The original message (may contain newlines)
|
|
|
+ /// @return The collapsed message with line count indicator if multi-line, otherwise unchanged
|
|
|
+ static std::string collapse_multiline(const std::string& message) {
|
|
|
+ size_t newline_pos = message.find('\n');
|
|
|
+ if (newline_pos == std::string::npos) {
|
|
|
+ return message; // Single line, return as-is
|
|
|
+ }
|
|
|
+
|
|
|
+ // Count additional lines
|
|
|
+ size_t line_count = 1;
|
|
|
+ size_t pos = 0;
|
|
|
+ while ((pos = message.find('\n', pos)) != std::string::npos) {
|
|
|
+ ++line_count;
|
|
|
+ ++pos;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Return first line with indicator
|
|
|
+ return message.substr(0, newline_pos) +
|
|
|
+ " [+" + std::to_string(line_count - 1) + " lines, C-h e]";
|
|
|
+ }
|
|
|
+
|
|
|
+ /// @brief Truncate a message to fit within a maximum character width.
|
|
|
+ /// @param message The message to truncate
|
|
|
+ /// @param max_width Maximum number of characters allowed
|
|
|
+ /// @return The truncated message with suffix if it was too long, otherwise unchanged
|
|
|
+ static std::string truncate_to_width(const std::string& message, size_t max_width) {
|
|
|
+ if (message.length() <= max_width) {
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ const std::string suffix = TRUNCATION_SUFFIX;
|
|
|
+ if (max_width <= suffix.length()) {
|
|
|
+ return suffix.substr(0, max_width); // Edge case: very narrow width
|
|
|
+ }
|
|
|
+
|
|
|
+ size_t max_chars = max_width - suffix.length();
|
|
|
+ return message.substr(0, max_chars) + suffix;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// @brief Format a message for display in the echo area.
|
|
|
+ /// Combines multi-line collapsing and width truncation.
|
|
|
+ /// @param message The original message
|
|
|
+ /// @param max_width Maximum display width in characters
|
|
|
+ /// @return The formatted message ready for display
|
|
|
+ static std::string format_for_echo_area(const std::string& message, size_t max_width) {
|
|
|
+ std::string result = collapse_multiline(message);
|
|
|
+ return truncate_to_width(result, max_width);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+} // namespace lumacs
|