[Lldb-commits] [lldb] 9ef7de7 - [LLDB][GUI] Add required property to text fields

Greg Clayton via lldb-commits lldb-commits at lists.llvm.org
Wed Jul 21 14:40:55 PDT 2021


Author: Omar Emara
Date: 2021-07-21T14:40:43-07:00
New Revision: 9ef7de7c819d39eaa4176d046083bf585c9274b0

URL: https://github.com/llvm/llvm-project/commit/9ef7de7c819d39eaa4176d046083bf585c9274b0
DIFF: https://github.com/llvm/llvm-project/commit/9ef7de7c819d39eaa4176d046083bf585c9274b0.diff

LOG: [LLDB][GUI] Add required property to text fields

This patch adds a required property to text fields and their
derivatives. Additionally, the Process Name and PID fields in the attach
form were marked as required.

Differential Revision: https://reviews.llvm.org/D106458

Added: 
    

Modified: 
    lldb/source/Core/IOHandlerCursesGUI.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Core/IOHandlerCursesGUI.cpp b/lldb/source/Core/IOHandlerCursesGUI.cpp
index 1779a5b1e882..98ef6bd3d476 100644
--- a/lldb/source/Core/IOHandlerCursesGUI.cpp
+++ b/lldb/source/Core/IOHandlerCursesGUI.cpp
@@ -1075,8 +1075,9 @@ typedef std::unique_ptr<FieldDelegate> FieldDelegateUP;
 
 class TextFieldDelegate : public FieldDelegate {
 public:
-  TextFieldDelegate(const char *label, const char *content)
-      : m_label(label), m_cursor_position(0), m_first_visibile_char(0) {
+  TextFieldDelegate(const char *label, const char *content, bool required)
+      : m_label(label), m_required(required), m_cursor_position(0),
+        m_first_visibile_char(0) {
     if (content)
       m_content = content;
   }
@@ -1238,6 +1239,13 @@ class TextFieldDelegate : public FieldDelegate {
     return eKeyNotHandled;
   }
 
+  void FieldDelegateExitCallback() override {
+    if (!IsSpecified() && m_required)
+      SetError("This field is required!");
+  }
+
+  bool IsSpecified() { return !m_content.empty(); }
+
   bool HasError() { return !m_error.empty(); }
 
   void ClearError() { m_error.clear(); }
@@ -1250,6 +1258,7 @@ class TextFieldDelegate : public FieldDelegate {
 
 protected:
   std::string m_label;
+  bool m_required;
   // The position of the top left corner character of the border.
   std::string m_content;
   // The cursor position in the content string itself. Can be in the range
@@ -1266,8 +1275,8 @@ class TextFieldDelegate : public FieldDelegate {
 
 class IntegerFieldDelegate : public TextFieldDelegate {
 public:
-  IntegerFieldDelegate(const char *label, int content)
-      : TextFieldDelegate(label, std::to_string(content).c_str()) {}
+  IntegerFieldDelegate(const char *label, int content, bool required)
+      : TextFieldDelegate(label, std::to_string(content).c_str(), required) {}
 
   // Only accept digits.
   bool IsAcceptableChar(int key) override { return isdigit(key); }
@@ -1278,13 +1287,16 @@ class IntegerFieldDelegate : public TextFieldDelegate {
 
 class FileFieldDelegate : public TextFieldDelegate {
 public:
-  FileFieldDelegate(const char *label, const char *content,
-                    bool need_to_exist = true)
-      : TextFieldDelegate(label, content), m_need_to_exist(need_to_exist) {}
+  FileFieldDelegate(const char *label, const char *content, bool need_to_exist,
+                    bool required)
+      : TextFieldDelegate(label, content, required),
+        m_need_to_exist(need_to_exist) {}
 
-  // Set appropriate error messages if the file doesn't exists or is, in fact, a
-  // directory.
   void FieldDelegateExitCallback() override {
+    TextFieldDelegate::FieldDelegateExitCallback();
+    if (!IsSpecified())
+      return;
+
     FileSpec file(GetPath());
     if (m_need_to_exist && !FileSystem::Instance().Exists(file)) {
       SetError("File doesn't exist!");
@@ -1306,12 +1318,15 @@ class FileFieldDelegate : public TextFieldDelegate {
 class DirectoryFieldDelegate : public TextFieldDelegate {
 public:
   DirectoryFieldDelegate(const char *label, const char *content,
-                         bool need_to_exist = true)
-      : TextFieldDelegate(label, content), m_need_to_exist(need_to_exist) {}
+                         bool need_to_exist, bool required)
+      : TextFieldDelegate(label, content, required),
+        m_need_to_exist(need_to_exist) {}
 
-  // Set appropriate error messages if the directory doesn't exists or is, in
-  // fact, a file.
   void FieldDelegateExitCallback() override {
+    TextFieldDelegate::FieldDelegateExitCallback();
+    if (!IsSpecified())
+      return;
+
     FileSpec file(GetPath());
     if (m_need_to_exist && !FileSystem::Instance().Exists(file)) {
       SetError("Directory doesn't exist!");
@@ -1879,31 +1894,35 @@ class FormDelegate {
 
   // Factory methods to create and add fields of specific types.
 
-  TextFieldDelegate *AddTextField(const char *label, const char *content) {
-    TextFieldDelegate *delegate = new TextFieldDelegate(label, content);
+  TextFieldDelegate *AddTextField(const char *label, const char *content,
+                                  bool required) {
+    TextFieldDelegate *delegate =
+        new TextFieldDelegate(label, content, required);
     m_fields.push_back(FieldDelegateUP(delegate));
     return delegate;
   }
 
   FileFieldDelegate *AddFileField(const char *label, const char *content,
-                                  bool need_to_exist = true) {
+                                  bool need_to_exist, bool required) {
     FileFieldDelegate *delegate =
-        new FileFieldDelegate(label, content, need_to_exist);
+        new FileFieldDelegate(label, content, need_to_exist, required);
     m_fields.push_back(FieldDelegateUP(delegate));
     return delegate;
   }
 
   DirectoryFieldDelegate *AddDirectoryField(const char *label,
                                             const char *content,
-                                            bool need_to_exist = true) {
+                                            bool need_to_exist, bool required) {
     DirectoryFieldDelegate *delegate =
-        new DirectoryFieldDelegate(label, content, need_to_exist);
+        new DirectoryFieldDelegate(label, content, need_to_exist, required);
     m_fields.push_back(FieldDelegateUP(delegate));
     return delegate;
   }
 
-  IntegerFieldDelegate *AddIntegerField(const char *label, int content) {
-    IntegerFieldDelegate *delegate = new IntegerFieldDelegate(label, content);
+  IntegerFieldDelegate *AddIntegerField(const char *label, int content,
+                                        bool required) {
+    IntegerFieldDelegate *delegate =
+        new IntegerFieldDelegate(label, content, required);
     m_fields.push_back(FieldDelegateUP(delegate));
     return delegate;
   }
@@ -2368,9 +2387,9 @@ class ProcessAttachFormDelegate : public FormDelegate {
     types.push_back(std::string("Name"));
     types.push_back(std::string("PID"));
     m_type_field = AddChoicesField("Attach By", 2, types);
-    m_pid_field = AddIntegerField("PID", 0);
+    m_pid_field = AddIntegerField("PID", 0, true);
     m_name_field =
-        AddTextField("Process Name", GetDefaultProcessName().c_str());
+        AddTextField("Process Name", GetDefaultProcessName().c_str(), true);
     m_continue_field = AddBooleanField("Continue once attached.", false);
     m_wait_for_field = AddBooleanField("Wait for process to launch.", false);
     m_include_existing_field =


        


More information about the lldb-commits mailing list