about summary refs log tree commit diff stats
path: root/pkgs/by-name/ts/tskm/src/interface/open/handle.rs
blob: 5738d2329492bec998e1511f6763ab31ecfc43db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
use std::process;

use anyhow::{bail, Context, Result};
use log::{error, info};

use crate::{cli::OpenCommand, rofi, task};

pub fn handle(command: OpenCommand) -> Result<()> {
    match command {
        OpenCommand::Review => {
            for project in task::Project::all()? {
                if project.is_touched() {
                    info!("Reviewing project: '{}'", project.to_project_display());
                    open_in_browser(project).with_context(|| {
                        format!(
                            "Failed to open project ('{}') in Firefox",
                            project.to_project_display()
                        )
                    })?;
                    project.untouch().with_context(|| {
                        format!(
                            "Failed to untouch project ('{}')",
                            project.to_project_display()
                        )
                    })?;
                }
            }
        }
        OpenCommand::Project { project } => {
            let project = if let Some(p) = project {
                p
            } else if let Some(p) = task::Project::get_current()? {
                p
            } else {
                bail!("You need to either supply a project or have a project active!");
            };

            project.touch().context("Failed to touch project")?;
            open_in_browser(&project).context("Failed to open project")?;
        }
        OpenCommand::Select => {
            let selected_project: task::Project = task::Project::from_project_string(
                &rofi::select(
                    task::Project::all()
                        .context("Failed to get all registered projects")?
                        .iter()
                        .map(task::Project::to_project_display)
                        .collect::<Vec<_>>()
                        .as_slice(),
                )
                .context("Failed to get selected project")?,
            )
            .expect("This should work, as we send only projects in");

            selected_project
                .touch()
                .context("Failed to touch project")?;

            open_in_browser(&selected_project).context("Failed to open project")?;
        }
    }
    Ok(())
}

fn open_in_browser(selected_project: &task::Project) -> Result<()> {
    let old_project: Option<task::Project> =
        task::Project::get_current().context("Failed to get currently active project")?;
    // We have ensured that only one task may be active
    let old_task: Option<task::Id> =
        task::Id::get_current().context("Failed to get currently active task")?;

    selected_project.activate().with_context(|| {
        format!(
            "Failed to active project: '{}'",
            selected_project.to_project_display()
        )
    })?;

    let tracking_task = {
        let all_tasks = selected_project.get_tasks().with_context(|| {
            format!(
                "Failed to get assoctiated tasks for project: '{}'",
                selected_project.to_project_display()
            )
        })?;

        let tracking_task = all_tasks.into_iter().find(|t| {
            let maybe_desc = t.description();
            if let Ok(desc) = maybe_desc {
                desc == "tracking"
            } else {
                error!(
                    "Getting task description returned error: {}",
                    maybe_desc.expect_err("We already check for Ok")
                );
                false
            }
        });

        if let Some(task) = tracking_task {
            info!(
                "Starting task {} -> tracking",
                selected_project.to_project_display()
            );
            task.start()?;
        }
        tracking_task
    };

    let status = process::Command::new("firefox")
        .args([
            "-P",
            selected_project.to_project_display().as_str(),
            "about:newtab",
        ])
        .status()
        .context("Failed to start firefox")?;

    if !status.success() {
        error!("Firefox run exited with error.");
    }

    if let Some(task) = tracking_task {
        task.stop()?;
    }
    if let Some(task) = old_task {
        task.start()?;
    }

    if let Some(project) = old_project {
        project.activate()?;
    } else {
        task::Project::clear()?;
    }

    Ok(())
}