文盘Rust——子命令提示,提高用户体验 | 京东云技术团队
上次我们聊到 CLI 的领域交互模式。在领域交互模式中,可能存在多层次的子命令。在使用过程中如果全评记忆的话,命令少还好,多了真心记不住。频繁 --help 也是个很麻烦的事情。如果每次按 'tab' 键就可以提示或补齐命令是不是很方便呢。这一节我们就来说说 'autocommplete' 如何实现。我们还是以interactcli-rs中的实现来解说实现过程 实现过程 其实,rustyline 已经为我们提供了基本的helper功能框架,其中包括了completer。我们来看代码,文件位置src/interact/cli.rs #[derive(Helper)] structMyHelper{ completer:CommandCompleter, highlighter:MatchingBracketHighlighter, validator:MatchingBracketValidator, hinter:HistoryHinter, colored_prompt:String, } pubfnrun(){ letconfig=Config::builder() .history_ignore_space(true) .completion_type(CompletionType::List) .output_stream(OutputStreamType::Stdout) .build(); leth=MyHelper{ completer:get_command_completer(), highlighter:MatchingBracketHighlighter::new(), hinter:HistoryHinter{}, colored_prompt:"".to_owned(), validator:MatchingBracketValidator::new(), }; letmutrl=Editor::with_config(config); //letmutrl=Editor::<()>::new(); rl.set_helper(Some(h)); ...... } 首先定义 MyHelper 结构体, 需要实现 Completer + Hinter + Highlighter + Validator trait。然后通过rustyline的set_helper函数加载我们定义好的helper。在MyHelper 结构体中,需要我们自己来实现completer的逻辑。 Sub command autocompleter实现详解 SubCmd 结构体 #[derive(Debug,Clone)] pubstructSubCmd{ publevel:usize, pubcommand_name:String, pubsubcommands:Vec<String>, } SubCmd 结构体包含:命令级别,命令名称,以及该命令包含的子命令信息,以便在实现实现 autocomplete 时定位命令和子命令的范围 在程序启动时遍历所有的command,src/cmd/rootcmd.rs 中的all_subcommand函数负责收集所有命令并转换为Vec<SubCmd> pubfnall_subcommand(app:&clap_Command,beginlevel:usize,input:&mutVec<SubCmd>){ letnextlevel=beginlevel+1; letmutsubcmds=vec![]; foriterminapp.get_subcommands(){ subcmds.push(iterm.get_name().to_string()); ifiterm.has_subcommands(){ all_subcommand(iterm,nextlevel,input); }else{ ifbeginlevel==0{ all_subcommand(iterm,nextlevel,input); } } } letsubcommand=SubCmd{ level:beginlevel, command_name:app.get_name().to_string(), subcommands:subcmds, }; input.push(subcommand); } CommandCompleter 子命令自动补充功能的核心部分 #[derive(Debug,Clone)] pubstructCommandCompleter{ subcommands:Vec<SubCmd>, } implCommandCompleter{ pubfnnew(subcmds:Vec<SubCmd>)->Self{ Self{ subcommands:subcmds, } } //获取level下所有可能的子命令 pubfnlevel_possible_cmd(&self,level:usize)->Vec<String>{ letmutsubcmds=vec![]; letcmds=self.subcommands.clone(); foritermincmds{ ifiterm.level==level{ subcmds.push(iterm.command_name.clone()); } } returnsubcmds; } //获取level下某字符串开头的子命令 pubfnlevel_prefix_possible_cmd(&self,level:usize,prefix:&str)->Vec<String>{ letmutsubcmds=vec![]; letcmds=self.subcommands.clone(); foritermincmds{ ifiterm.level==level&&iterm.command_name.starts_with(prefix){ subcmds.push(iterm.command_name); } } returnsubcmds; } //获取某level下某subcommand的所有子命令 pubfnlevel_cmd_possible_sub_cmd(&self,level:usize,cmd:String)->Vec<String>{ letmutsubcmds=vec![]; letcmds=self.subcommands.clone(); foritermincmds{ ifiterm.level==level&&iterm.command_name==cmd{ subcmds=iterm.subcommands.clone(); } } returnsubcmds; } //获取某level下某subcommand的所有prefix子命令 pubfnlevel_cmd_possible_prefix_sub_cmd( &self, level:usize, cmd:String, prefix:&str, )->Vec<String>{ letmutsubcmds=vec![]; letcmds=self.subcommands.clone(); foritermincmds{ ifiterm.level==level&&iterm.command_name==cmd{ foriiniterm.subcommands{ ifi.starts_with(prefix){ subcmds.push(i); } } } } returnsubcmds; } pubfncomplete_cmd(&self,line:&str,pos:usize)->Result<(usize,Vec<Pair>)>{ letmutentries:Vec<Pair>=Vec::new(); letd:Vec<_>=line.split('').collect(); ifd.len()==1{ ifd.last()==Some(&""){ forstrinself.level_possible_cmd(1){ letmutreplace=str.clone(); replace.push_str(""); entries.push(Pair{ display:str.clone(), replacement:replace, }); } returnOk((pos,entries)); } ifletSome(last)=d.last(){ forstrinself.level_prefix_possible_cmd(1,*last){ letmutreplace=str.clone(); replace.push_str(""); entries.push(Pair{ display:str.clone(), replacement:replace, }); } returnOk((pos-last.len(),entries)); } } ifd.last()==Some(&""){ forstrinself .level_cmd_possible_sub_cmd(d.len()-1,d.get(d.len()-2).unwrap().to_string()) { letmutreplace=str.clone(); replace.push_str(""); entries.push(Pair{ display:str.clone(), replacement:replace, }); } returnOk((pos,entries)); } ifletSome(last)=d.last(){ forstrinself.level_cmd_possible_prefix_sub_cmd( d.len()-1, d.get(d.len()-2).unwrap().to_string(), *last, ){ letmutreplace=str.clone(); replace.push_str(""); entries.push(Pair{ display:str.clone(), replacement:replace, }); } returnOk((pos-last.len(),entries)); } Ok((pos,entries)) } } implCompleterforCommandCompleter{ typeCandidate=Pair; fncomplete(&self,line:&str,pos:usize,_ctx:&Context<'_>)->Result<(usize,Vec<Pair>)>{ self.complete_cmd(line,pos) } } CommandCompleter 的实现部分比较多,大致包括两个部分,前一部分包括:获取某一级别下所有可能的子命令、获取某级别下某字符串开头的子命令、获取某级别下某个命令的所有子命令,等基本功能。这部分代码中有注释就不一一累述。 函数complete_cmd用来计算行中的位置以及在该位置的替换内容。 输入项是命令行的内容以及光标所在位置,输出项为在该位置需要替换的内容。比如,我们在提示符下输入 "root cm" root 下包含 cmd1、cmd2 两个子命令,此时如果按 'tab'键,complete_cmd 函数就会返回 (7,[cmd1,cmd2])。 作者:京东科技贾世闻 来源:京东云开发者社区 转载请注明来源