/crates/maple_cli/src/app.rs

https://github.com/liuchengxu/vim-clap · Rust · 112 lines · 83 code · 8 blank · 21 comment · 4 complexity · 1432eac1ec06e95fdb71c927cf792df1 MD5 · raw file

  1. use anyhow::Result;
  2. use icon::IconPainter;
  3. use structopt::clap::AppSettings;
  4. use structopt::StructOpt;
  5. #[derive(StructOpt, Debug)]
  6. pub enum Cmd {
  7. /// Display the current version
  8. #[structopt(name = "version")]
  9. Version,
  10. /// Start the stdio-based service, currently there is only filer support.
  11. #[structopt(name = "rpc")]
  12. RPC,
  13. /// Execute the grep command to avoid the escape issue
  14. #[structopt(name = "grep")]
  15. Grep(crate::cmd::grep::Grep),
  16. /// Execute the shell command.
  17. #[structopt(name = "exec")]
  18. Exec(crate::cmd::exec::Exec),
  19. /// Generate the project-wide tags using ctags.
  20. #[structopt(name = "tags")]
  21. Tags(crate::cmd::tags::Tags),
  22. /// Interact with the cache info.
  23. #[structopt(name = "cache")]
  24. Cache(crate::cmd::cache::Cache),
  25. /// Fuzzy filter the input.
  26. #[structopt(name = "filter")]
  27. Filter(crate::cmd::filter::Filter),
  28. /// Filter against current Vim buffer.
  29. #[structopt(name = "blines")]
  30. Blines(crate::cmd::blines::Blines),
  31. /// Generate vim help tags.
  32. #[structopt(name = "helptags")]
  33. Helptags(crate::cmd::helptags::Helptags),
  34. /// Start the forerunner job of grep.
  35. #[structopt(name = "ripgrep-forerunner")]
  36. RipGrepForerunner(crate::cmd::grep::RipGrepForerunner),
  37. /// Retrive the latest remote release info.
  38. #[structopt(name = "upgrade")]
  39. Upgrade(upgrade::Upgrade),
  40. }
  41. #[derive(StructOpt, Debug)]
  42. #[structopt(
  43. name = "maple",
  44. no_version,
  45. global_settings = &[AppSettings::DisableVersion]
  46. )]
  47. pub struct Maple {
  48. /// Print the top NUM of filtered items.
  49. ///
  50. /// The returned JSON has three fields:
  51. /// - total: total number of initial filtered result set.
  52. /// - lines: text lines used for displaying directly.
  53. /// - indices: the indices of matched elements per line, used for the highlight purpose.
  54. #[structopt(short = "n", long = "number", name = "NUM")]
  55. pub number: Option<usize>,
  56. /// Width of clap window.
  57. #[structopt(short = "w", long = "winwidth")]
  58. pub winwidth: Option<usize>,
  59. /// Prepend an icon for item of files and grep provider, valid only when --number is used.
  60. #[structopt(short, long, possible_values = &IconPainter::variants(), case_insensitive = true)]
  61. pub icon_painter: Option<IconPainter>,
  62. /// Do not use the cached file for exec subcommand.
  63. #[structopt(long = "no-cache")]
  64. pub no_cache: bool,
  65. /// Enable the logging system.
  66. #[structopt(long = "log", parse(from_os_str))]
  67. pub log: Option<std::path::PathBuf>,
  68. #[structopt(subcommand)]
  69. pub command: Cmd,
  70. }
  71. impl Maple {
  72. pub fn run(self) -> Result<()> {
  73. if let Some(ref log_path) = self.log {
  74. crate::logger::init(log_path)?;
  75. } else if let Ok(log_path) = std::env::var("VIM_CLAP_LOG_PATH") {
  76. crate::logger::init(log_path)?;
  77. }
  78. match self.command {
  79. Cmd::Version | Cmd::Upgrade(_) => unreachable!(),
  80. Cmd::Helptags(helptags) => helptags.run()?,
  81. Cmd::Tags(tags) => tags.run(self.no_cache, self.icon_painter)?,
  82. Cmd::RPC => {
  83. stdio_server::run_forever(std::io::BufReader::new(std::io::stdin()));
  84. }
  85. Cmd::Blines(blines) => {
  86. blines.run(self.number, self.winwidth)?;
  87. }
  88. Cmd::RipGrepForerunner(rip_grep_forerunner) => {
  89. rip_grep_forerunner.run(self.number, self.icon_painter, self.no_cache)?
  90. }
  91. Cmd::Cache(cache) => cache.run()?,
  92. Cmd::Filter(filter) => {
  93. filter.run(self.number, self.winwidth, self.icon_painter)?;
  94. }
  95. Cmd::Exec(exec) => {
  96. exec.run(self.number, self.icon_painter, self.no_cache)?;
  97. }
  98. Cmd::Grep(grep) => {
  99. grep.run(self.number, self.winwidth, self.icon_painter, self.no_cache)?;
  100. }
  101. }
  102. Ok(())
  103. }
  104. }