summaryrefslogtreecommitdiffstats
path: root/drivers/fpga/dfl-fme-br.c
AgeCommit message (Expand)Author
2018-10-16fpga: bridge: add devm_fpga_bridge_createAlan Tull
2018-07-15fpga: dfl: add fpga bridge platform driver for FMEWu Hao
4' href='#n24'>24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
use chrono::{DateTime, Datelike, TimeZone, Local, Date};
use yansi::{Style};
use itertools::Itertools;
use structopt::StructOpt;

use crate::cursorfile;
use crate::input;
use crate::config::{Config,CalendarConfig};
use crate::khevent::KhEvent;
use crate::khline::KhLine;
use crate::KhResult;

#[derive(Debug, StructOpt)]
pub struct AgendaArgs {
  /// Show agenda view
  #[structopt(name = "args")]
  pub args: Vec<String>,
}

pub fn show_events(config: &Config, args: &[&str]) -> KhResult<()> {
  let mut events = input::selection(args)?;

  let cursor = cursorfile::read_cursorfile().ok();
  show_events_cursor(config, &mut events, cursor.as_ref());

  Ok(())
}

pub fn show_events_cursor(
  config: &Config,
  events: &mut Iterator<Item = KhEvent>,
  cursor: Option<&KhLine>,
) {

  let mut not_over_yet: Vec<(usize, KhEvent, Option<&CalendarConfig>)> = Vec::new();
  let mut cals_iter = events
    .enumerate()
    .map(|(i, event)| {
      let config = event.get_calendar_name().and_then(|name| config.get_config_for_calendar(&name));
      (i, event, config)
    })
    .peekable();

  let start_day = match cals_iter.peek() {
    Some((_, event, _)) => {
      event
        .get_start()
        .map(|dtstart| dtstart.into())
        .unwrap_or_else(|| Local.timestamp(0, 0))
        .date()
    }
    None => return,
  };

  let mut cur_day = start_day.pred();
  let mut last_printed_day = start_day.pred();
  while cals_iter.peek().is_some() || !not_over_yet.is_empty() {
    cur_day = cur_day.succ();

    maybe_print_date_line_header(&config, cur_day, start_day, &mut last_printed_day);

    not_over_yet.retain( |(index, event, cal_config)| {
      let is_cursor = cursor.map(|c| c.matches_khevent(&event)).unwrap_or(false);
      maybe_print_date_line(&config, cur_day, start_day, &mut last_printed_day);
      print_event_line(*cal_config, *index, &event, cur_day, is_cursor);
      event.continues_after(cur_day)
    });

    let relevant_events = cals_iter.peeking_take_while(|(_,event,_)| event.starts_on(cur_day));
    for (i, event, cal_config) in relevant_events {
      let is_cursor = cursor.map(|c| c.matches_khevent(&event)).unwrap_or(false);
      maybe_print_date_line(&config, cur_day, start_day, &mut last_printed_day);
      print_event_line(cal_config, i, &event, cur_day, is_cursor);
      if event.continues_after(cur_day) {
        not_over_yet.push((i, event, cal_config));
      }
    }
  }
}

fn maybe_print_week_separator(config: &Config, date: Date<Local>, start_date: Date<Local>, last_printed_date: Date<Local>) {
  if !config.agenda.print_week_separator {
    return;
  }
  if date != start_date && last_printed_date.iso_week() < date.iso_week() {
    khprintln!();
  }
}

fn maybe_print_date_line_header(config: &Config, date: Date<Local>, start_date: Date<Local>, last_printed_date: &mut Date<Local>) {
  if !config.agenda.print_empty_days {
    return;
  }
  maybe_print_date_line(config, date, start_date, last_printed_date);
}

fn maybe_print_date_line(config: &Config, date: Date<Local>, start_date: Date<Local>, last_printed_date: &mut Date<Local>) {
  if date <= *last_printed_date {
    return;
  }
  maybe_print_week_separator(config, date, start_date, *last_printed_date);
  print_date_line(date);
  *last_printed_date = date;
}

fn print_date_line(date: Date<Local>) {
  let style_heading = Style::default().bold();
  khprintln!("{}, {}", style_heading.paint(date.format("%Y-%m-%d")), date.format("%A"));
}

fn print_event_line(
  config: Option<&CalendarConfig>,
  index: usize,
  event: &KhEvent,
  date: Date<Local>,
  is_cursor: bool
) {
  match event_line(config, &event, date, is_cursor) {
    Ok(line) => khprintln!("{:4}  {}", index, line),
    Err(error) => warn!("{} in {}", error, event.get_uid())
  }
}

pub fn event_line(
  config: Option<&CalendarConfig>,
  event: &KhEvent,
  cur_day: Date<Local>,
  is_cursor: bool
) -> Result<String, String> {
  if !event.relevant_on(cur_day) {
    return Err(format!("event is not relevant for {:?}", cur_day));
  }

  let mut summary = event.get_summary().ok_or("Invalid SUMMARY")?;
  if let Some(config) = config {
    let calendar_style = config.get_style_for_calendar();
    summary = calendar_style.paint(summary).to_string();
  }

  let cursor_icon = if is_cursor { ">" } else { "" };

  if event.is_allday() {
    Ok(format!("{:3}             {}", cursor_icon, summary))
  } else {
    let mut time_sep = " ";
    let dtstart: DateTime<Local> = event.get_start().ok_or("Invalid DTSTART")?.into();
    let start_string = if dtstart.date() != cur_day {
      "".to_string()
    } else {
      time_sep = "-";
      format!("{}", dtstart.format("%H:%M"))
    };

    let dtend: DateTime<Local> = event.get_end().ok_or("Invalid DTEND")?.into();
    let end_string = if dtend.date() != cur_day {
      "".to_string()
    } else {
      time_sep = "-";
      format!("{}", dtend.format("%H:%M"))
    };

    Ok(format!("{:3}{:5}{}{:5}  {}", cursor_icon, start_string, time_sep, end_string, summary))
  }
}

impl KhEvent {
  fn starts_on(&self, date: Date<Local>) -> bool {
    let dtstart: Date<Local> = self.get_start().unwrap().into();
    dtstart == date
  }

  fn relevant_on(&self, date: Date<Local>) -> bool {
    let dtstart: Option<Date<Local>> = self.get_start().map(|date| date.into());
    let last_relevant_date: Option<Date<Local>> = self.get_last_relevant_date().map(|date| date.into());

    dtstart.map(|dtstart| dtstart <= date).unwrap_or(false) &&
    last_relevant_date.map(|enddate| enddate >= date).unwrap_or(false)
  }

  fn continues_after(&self, date: Date<Local>) -> bool {
    let last_relevant_date: Option<Date<Local>> = self.get_last_relevant_date().map(|date| date.into());
    last_relevant_date
      .map(|enddate| enddate > date)
      .unwrap_or(false)
  }
}

#[cfg(test)]
mod integration {
  use super::*;
  use crate::testdata;
  use crate::testutils::*;
  use crate::utils::stdioutils;
  use crate::config::Config;
  use crate::icalwrap::IcalVCalendar;

  use chrono::{Local, TimeZone};

  #[test]
  fn test_starts_on() {
    let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY, None).unwrap();
    let event = cal.get_principal_khevent();

    let first_day =