util.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::{
  19. env,
  20. fs::{self, File},
  21. io::Write,
  22. process::Command,
  23. };
  24. use chrono::{Datelike, Local, NaiveDate};
  25. use log::error;
  26. use darkfi::{util::time::Timestamp, Result};
  27. use crate::{
  28. primitives::TaskInfo,
  29. view::{comments_table, events_table, taskinfo_table},
  30. };
  31. /// Parse due date (e.g. "1503" for 15 March) as i64 timestamp.
  32. pub fn due_as_timestamp(due: &str) -> Option<u64> {
  33. if due.len() != 4 || due.parse::<u32>().is_err() {
  34. error!("Due date must be digits of length 4 (e.g. \"1503\" for 15 March)");
  35. return None
  36. }
  37. let (day, month) = (due[..2].parse::<u32>().unwrap(), due[2..].parse::<u32>().unwrap());
  38. if day > 31 || month > 12 {
  39. error!("Invalid or out-of-range date");
  40. return None
  41. }
  42. let mut year = Local::now().year();
  43. // Ensure the due date is in future
  44. if month < Local::now().month() {
  45. year += 1;
  46. }
  47. if month == Local::now().month() && day < Local::now().day() {
  48. year += 1;
  49. }
  50. let dt = NaiveDate::from_ymd_opt(year, month, day).unwrap().and_hms_opt(12, 0, 0).unwrap();
  51. dt.timestamp().try_into().ok()
  52. }
  53. /// Start up the preferred editor to edit a task's description.
  54. pub fn prompt_text(task_info: TaskInfo, what: &str) -> Result<Option<String>> {
  55. // Create a temporary file with some comments inside.
  56. let mut file_path = env::temp_dir();
  57. let file_name = format!("tau-{}", Timestamp::current_time().0);
  58. file_path.push(file_name);
  59. let mut file = File::create(&file_path)?;
  60. writeln!(file, "\n# Write your task {what} above this line.")?;
  61. writeln!(file, "# Lines starting with \"#\" will be removed.")?;
  62. writeln!(file, "# An empty {what} aborts the operation.")?;
  63. writeln!(file, "\n# ------------------------ >8 ------------------------")?;
  64. writeln!(file, "# Do not modify or remove the line above.")?;
  65. writeln!(file, "# Everything below it will be ignored.")?;
  66. writeln!(file, "\n{}", taskinfo_table(task_info.clone())?)?;
  67. writeln!(file, "{}", events_table(task_info.clone())?)?;
  68. writeln!(file, "{}", comments_table(task_info)?)?;
  69. // Try $EDITOR, and if not, fallback to xdg-open.
  70. let editor_argv0 = match env::var("EDITOR") {
  71. Ok(v) => v,
  72. Err(_) => "xdg-open".into(),
  73. };
  74. if let Err(e) = Command::new(editor_argv0).arg(&file_path).status() {
  75. error!("Running $EDITOR failed, neither env or xdg-open are available");
  76. return Err(e.into())
  77. }
  78. // Whatever has been written in the temp file will be read here.
  79. let content = fs::read_to_string(&file_path)?;
  80. fs::remove_file(&file_path)?;
  81. let mut lines = vec![];
  82. for i in content.lines() {
  83. if !i.starts_with('#') {
  84. lines.push(i.to_string())
  85. }
  86. if i == "# ------------------------ >8 ------------------------" {
  87. break
  88. }
  89. }
  90. Ok(Some(lines.join("\n")))
  91. }