Skip to content

Commit

Permalink
feat: process priority from new task input
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-pitblado committed Oct 27, 2024
1 parent e30d1bf commit 652da4e
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 8 deletions.
39 changes: 39 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
regex = "1"
toml = "0.5"
dirs = "4.0"
tokio = { version = "1", features = ["full"] }
Expand Down
17 changes: 12 additions & 5 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::models::{Task, TaskDetail};
use reqwest::Client;
use std::collections::HashMap;
use serde_json::json;
use std::error::Error;

pub async fn fetch_tasks(
instance_url: &str,
Expand Down Expand Up @@ -48,17 +49,23 @@ pub async fn create_new_task(
instance_url: &str,
api_key: &str,
task_title: &str,
) -> Result<(), Box<dyn std::error::Error>> {
priority: Option<u8>,
) -> Result<(), Box<dyn Error>> {
let client = Client::new();
let url = format!("{}/api/v1/projects/1/tasks", instance_url);

let mut map = HashMap::new();
map.insert("title", task_title);
let mut task_data = json!({
"title": task_title
});

if let Some(priority_value) = priority {
task_data["priority"] = json!(priority_value);
}

let res = client
.put(&url)
.header("Authorization", format!("Bearer {}", api_key))
.json(&map)
.json(&task_data)
.send()
.await?;

Expand Down
15 changes: 12 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::api::{create_new_task, fetch_task_detail, fetch_tasks};
use crate::models::{Task, TaskDetail};
use crate::parser::parse_task_input;
use crossterm::event::KeyCode;
use ratatui::widgets::ListState;
use std::io;
Expand Down Expand Up @@ -149,10 +150,18 @@ impl App {
InputMode::Editing => {
match key.code {
KeyCode::Enter => {
// Handle task submission
if !self.new_task_title.trim().is_empty() {
if let Err(err) =
create_new_task(instance_url, api_key, &self.new_task_title).await
// Use the parser to extract the task title, priority, and label titles
let parsed_task = parse_task_input(&self.new_task_title);

// Create the new task with the parsed title, priority, and labels
if let Err(err) = create_new_task(
instance_url,
api_key,
&parsed_task.title,
parsed_task.priority,
)
.await
{
eprintln!("Error creating task: {}", err);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod api;
mod app;
mod models;
mod parser;
mod ui;

use crate::api::fetch_tasks;
Expand Down
29 changes: 29 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use regex::Regex;

pub struct ParsedTask {
pub title: String,
pub priority: Option<u8>,
}

pub fn parse_task_input(input: &str) -> ParsedTask {
// Regex for priority pattern, e.g. !1 to !5
let priority_re = Regex::new(r"!(\d)").unwrap();

let mut priority = None;
let mut title = input.to_string();

// Capture priority if it exists
if let Some(caps) = priority_re.captures(input) {
if let Some(priority_match) = caps.get(1) {
if let Ok(p) = priority_match.as_str().parse::<u8>() {
if (1..=5).contains(&p) {
priority = Some(p);
}
}
}
// Remove the priority pattern from the title
title = priority_re.replace(input, "").trim().to_string();
}

ParsedTask { title, priority }
}

0 comments on commit 652da4e

Please sign in to comment.