diff hgext3rd/topic/common.py @ 6877:f8ea46c21b56

topic: make formatfqbn() able to produce FQBN of 3 different lengths
author Anton Shestakov <av6@dwimlabs.net>
date Thu, 26 Sep 2024 17:09:11 +0400
parents 963471ebe26a
children
line wrap: on
line diff
--- a/hgext3rd/topic/common.py	Thu Sep 19 16:50:21 2024 +0400
+++ b/hgext3rd/topic/common.py	Thu Sep 26 17:09:11 2024 +0400
@@ -42,7 +42,29 @@
         tns, topic = b'none', tns
     return branch, tns, topic
 
-def formatfqbn(branch=b'', namespace=b'', topic=b'', short=True):
+# Fully qualified branch name length constants for formatfqbn() etc
+#
+# Selecting different length only matters when at least one FQBN component is
+# empty/default. The general idea is: short length will skip all empty
+# components (except for branch if there's nothing else to show), normal length
+# will only skip default topic namespace and empty topic, full length will not
+# skip anything and will show everything. For example:
+#
+# - formatfqbn(topic=foo, length=FQBN_SHORT) gives:
+#   //foo
+#
+# - formatfqbn(topic=foo, length=FQBN_NORMAL) gives:
+#   default//foo
+#
+# - formatfqbn(topic=foo, length=FQBN_FULL) gives:
+#   default//none/foo
+#
+# See also: tests for debug-format-fqbn command.
+FQBN_SHORT = 1
+FQBN_NORMAL = 2
+FQBN_FULL = 3
+
+def formatfqbn(branch=b'', namespace=b'', topic=b'', length=FQBN_NORMAL):
     """format branch, namespace and topic into branch//namespace/topic string
 
     >>> formatfqbn(branch=b'branch', topic=b'topic')
@@ -69,9 +91,9 @@
     'http://example.com/branch//namespace/topic'
     """
     result = b''
-    showbranch = True # branch and not (short and branch == b'default')
-    shownamespace = namespace and not (short and namespace == b'none')
-    if short and not showbranch and not shownamespace and not topic:
+    showbranch = branch and not (length == FQBN_SHORT and branch == b'default')
+    shownamespace = namespace and not (length != FQBN_FULL and namespace == b'none')
+    if not shownamespace and not topic:
         # if there's nothing to show, show at least branch
         showbranch = True
     if showbranch: