Browse Source

klish: navigation command 'replace'

Serj Kalichev 1 year ago
parent
commit
bd4e6159a7
2 changed files with 30 additions and 4 deletions
  1. 6 4
      examples/simple/example.xml
  2. 24 0
      plugins/klish/nav.c

+ 6 - 4
examples/simple/example.xml

@@ -18,10 +18,12 @@
 
 <ENTRY name="main" mode="switch" container="true">
 
+<ENTRY name="prompt" purpose="prompt" value="&gt; "/>
+
 <ENTRY name="exit" help="Exit view">
 	<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
 	<ACTION sym="nav">pop</ACTION>
-	<ACTION sym="print">Exiting klish session</ACTION>
+	<ACTION sym="printl">Exiting klish session</ACTION>
 </ENTRY>
 
 <ENTRY name="cmd" help="Clear settings" mode="sequence">
@@ -29,17 +31,17 @@
 	<ENTRY name="first" help="Clear settings">
 		<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
 		</ENTRY>
-	<ACTION sym="print">test</ACTION>
+	<ACTION sym="printl">test</ACTION>
 </ENTRY>
 
 <ENTRY name="cmd2" help="Clear settings" mode="sequence">
 	<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
-	<ACTION sym="print">test cmd2</ACTION>
+	<ACTION sym="printl">test cmd2</ACTION>
 </ENTRY>
 
 <ENTRY name="comm" value="command" help="Clear settings" mode="sequence">
 	<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
-	<ACTION sym="print">test2</ACTION>
+	<ACTION sym="printl">test2</ACTION>
 </ENTRY>
 
 </ENTRY>

+ 24 - 0
plugins/klish/nav.c

@@ -13,6 +13,7 @@
  * * pop [num] - Pop up path levels. Optional "num" argument specifies number
  *     of levels to pop. Default is 1.
  * * top - Pop up to first path level.
+ * * replace <view_name> - Replace current view by the specified one.
  * * exit - Exit klish.
  */
 
@@ -132,6 +133,29 @@ int klish_nav(kcontext_t *context)
 				return -1;
 			}
 
+		// replace <view_name>
+		} else if (faux_str_casecmp(nav_cmd, "replace") == 0) {
+			const char *view_name = faux_argv_index(argv, 1);
+			kentry_t *new_view = NULL;
+			if (!view_name) {
+				faux_argv_free(argv);
+				return -1;
+			}
+			new_view = kscheme_find_entry_by_path(
+				ksession_scheme(session), view_name);
+			if (!new_view) {
+				faux_argv_free(argv);
+				return -1;
+			}
+			if (!kpath_pop(path)) {
+				faux_argv_free(argv);
+				return -1;
+			}
+			if (!kpath_push(path, klevel_new(new_view))) {
+				faux_argv_free(argv);
+				return -1;
+			}
+
 		// Unknown command
 		} else {
 			faux_argv_free(argv);