Commit 42153b6c4f
Unsigned
Layout: unified · split
src/commands/topic.rs +102 −20
| @@ -8,6 +8,13 @@ use weechat::{ | ||
| 8 | 8 | use super::parse_and_run; |
| 9 | 9 | use crate::Servers; |
| 10 | 10 | |
| 11 | #[derive(Debug, Eq, PartialEq)] | |
| 12 | enum TopicAction { | |
| 13 | Show, | |
| 14 | Clear, | |
| 15 | Set(String), | |
| 16 | } | |
| 17 | ||
| 11 | 18 | pub struct TopicCommand { |
| 12 | 19 | servers: Servers, |
| 13 | 20 | } |
| @@ -15,10 +22,11 @@ pub struct TopicCommand { | ||
| 15 | 22 | impl TopicCommand { |
| 16 | 23 | pub fn create(servers: &Servers) -> Result<Command, ()> { |
| 17 | 24 | let settings = CommandSettings::new("topic") |
| 18 | .description("Set or clear the current Matrix room topic.") | |
| 19 | .add_argument("[topic]") | |
| 25 | .description("Show, set, or clear the current Matrix room topic.") | |
| 26 | .add_argument("[--clear] [topic]") | |
| 20 | 27 | .arguments_description( |
| 21 | "topic: The new topic for the current room. Omit it to clear the topic.", | |
| 28 | "topic: The new topic for the current room. Omit it to show the current topic.\n\ | |
| 29 | --clear: Clear the topic.", | |
| 22 | 30 | ); |
| 23 | 31 | |
| 24 | 32 | Command::new( |
| @@ -29,6 +37,54 @@ impl TopicCommand { | ||
| 29 | 37 | ) |
| 30 | 38 | } |
| 31 | 39 | |
| 40 | fn parser() -> Argparse<'static, 'static> { | |
| 41 | Argparse::new("topic") | |
| 42 | .about("Show, set, or clear the current Matrix room topic.") | |
| 43 | .settings(&[ | |
| 44 | ArgParseSettings::DisableHelpFlags, | |
| 45 | ArgParseSettings::DisableVersion, | |
| 46 | ]) | |
| 47 | .arg( | |
| 48 | Arg::with_name("clear") | |
| 49 | .long("clear") | |
| 50 | .help("Clear the current room topic.") | |
| 51 | .conflicts_with("topic"), | |
| 52 | ) | |
| 53 | .arg( | |
| 54 | Arg::with_name("topic") | |
| 55 | .multiple(true) | |
| 56 | .allow_hyphen_values(true), | |
| 57 | ) | |
| 58 | } | |
| 59 | ||
| 60 | fn action(matches: &clap::ArgMatches) -> TopicAction { | |
| 61 | if matches.is_present("clear") { | |
| 62 | TopicAction::Clear | |
| 63 | } else { | |
| 64 | matches | |
| 65 | .values_of("topic") | |
| 66 | .map(|t| TopicAction::Set(t.collect::<Vec<_>>().join(" "))) | |
| 67 | .unwrap_or(TopicAction::Show) | |
| 68 | } | |
| 69 | } | |
| 70 | ||
| 71 | fn show_topic(&self, buffer: &Buffer) { | |
| 72 | if let Some(room) = self.servers.find_room(buffer) { | |
| 73 | match room.room().topic() { | |
| 74 | Some(topic) if !topic.is_empty() => { | |
| 75 | buffer.print(&format!("Topic: {}", topic)); | |
| 76 | } | |
| 77 | _ => { | |
| 78 | buffer.print("No topic is set."); | |
| 79 | } | |
| 80 | } | |
| 81 | } else { | |
| 82 | Weechat::print( | |
| 83 | "The /topic command needs to be run in a Matrix room buffer.", | |
| 84 | ); | |
| 85 | } | |
| 86 | } | |
| 87 | ||
| 32 | 88 | fn set_topic(&self, buffer: &Buffer, topic: String) { |
| 33 | 89 | if let Some(room) = self.servers.find_room(buffer) { |
| 34 | 90 | let room = room.room().clone(); |
| @@ -53,24 +109,50 @@ impl TopicCommand { | ||
| 53 | 109 | |
| 54 | 110 | impl CommandCallback for TopicCommand { |
| 55 | 111 | fn callback(&mut self, _: &Weechat, buffer: &Buffer, arguments: Args) { |
| 56 | let parser = Argparse::new("topic") | |
| 57 | .about("Set or clear the current Matrix room topic.") | |
| 58 | .settings(&[ | |
| 59 | ArgParseSettings::DisableHelpFlags, | |
| 60 | ArgParseSettings::DisableVersion, | |
| 61 | ]) | |
| 62 | .arg( | |
| 63 | Arg::with_name("topic") | |
| 64 | .multiple(true) | |
| 65 | .allow_hyphen_values(true), | |
| 66 | ); | |
| 112 | parse_and_run(Self::parser(), arguments, |matches| match Self::action( | |
| 113 | matches, | |
| 114 | ) { | |
| 115 | TopicAction::Show => self.show_topic(buffer), | |
| 116 | TopicAction::Clear => self.set_topic(buffer, String::new()), | |
| 117 | TopicAction::Set(topic) => self.set_topic(buffer, topic), | |
| 118 | }); | |
| 119 | } | |
| 120 | } | |
| 67 | 121 | |
| 68 | parse_and_run(parser, arguments, |matches| { | |
| 69 | let topic = matches | |
| 70 | .values_of("topic") | |
| 71 | .map(|t| t.collect::<Vec<_>>().join(" ")); | |
| 122 | #[cfg(test)] | |
| 123 | mod tests { | |
| 124 | use super::{TopicAction, TopicCommand}; | |
| 72 | 125 | |
| 73 | self.set_topic(buffer, topic.unwrap_or_default()); | |
| 74 | }); | |
| 126 | fn parse(args: &[&str]) -> TopicAction { | |
| 127 | let matches = TopicCommand::parser() | |
| 128 | .get_matches_from_safe(args) | |
| 129 | .expect("topic arguments should parse"); | |
| 130 | ||
| 131 | TopicCommand::action(&matches) | |
| 132 | } | |
| 133 | ||
| 134 | #[test] | |
| 135 | fn omitted_topic_shows_current_topic() { | |
| 136 | assert_eq!(TopicAction::Show, parse(&["topic"])); | |
| 137 | } | |
| 138 | ||
| 139 | #[test] | |
| 140 | fn clear_flag_explicitly_clears_topic() { | |
| 141 | assert_eq!(TopicAction::Clear, parse(&["topic", "--clear"])); | |
| 142 | } | |
| 143 | ||
| 144 | #[test] | |
| 145 | fn clear_flag_rejects_topic_text() { | |
| 146 | assert!(TopicCommand::parser() | |
| 147 | .get_matches_from_safe(&["topic", "--clear", "not empty"]) | |
| 148 | .is_err()); | |
| 149 | } | |
| 150 | ||
| 151 | #[test] | |
| 152 | fn non_empty_topic_selects_set_action() { | |
| 153 | assert_eq!( | |
| 154 | TopicAction::Set("new room topic".to_owned()), | |
| 155 | parse(&["topic", "new", "room", "topic"]) | |
| 156 | ); | |
| 75 | 157 | } |
| 76 | 158 | } |