Skip to main content

binocular/preview/
protocol.rs

1use crate::search::types::SearchItem;
2use std::borrow::Cow;
3
4#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5pub enum PreviewSource {
6    SearchItem(SearchItem),
7    Diff {
8        left: String,
9        right: String,
10    },
11    GitHistory {
12        commit: String,
13        path: String,
14        line: usize,
15    },
16    GitBranch {
17        branch: String,
18    },
19    GitCommit {
20        commit: String,
21    },
22    LogStream(String),
23}
24
25impl PreviewSource {
26    pub fn title(&self) -> Cow<'_, str> {
27        match self {
28            Self::SearchItem(item) => match item {
29                SearchItem::Path(path) => Cow::Borrowed(path.as_str()),
30                SearchItem::Grep { path, .. } => Cow::Borrowed(path.as_str()),
31                SearchItem::GitHistory {
32                    commit, path, line, ..
33                } => Cow::Owned(format!("{commit}: {path}:{line}")),
34                SearchItem::GitBranch { branch, .. } => Cow::Owned(format!("branch: {branch}")),
35                SearchItem::GitCommit { commit, .. } => Cow::Owned(format!("commit: {commit}")),
36                SearchItem::Stdin(_) => Cow::Borrowed("<stdin>"),
37                SearchItem::Message(_) => Cow::Borrowed("Message"),
38            },
39            Self::Diff { left, right } => Cow::Owned(format!("diff: {left} <> {right}")),
40            Self::GitHistory { commit, path, line } => {
41                Cow::Owned(format!("{commit}: {path}:{line}"))
42            }
43            Self::GitBranch { branch } => Cow::Owned(format!("branch: {branch}")),
44            Self::GitCommit { commit } => Cow::Owned(format!("commit: {commit}")),
45            Self::LogStream(path) => Cow::Borrowed(path.as_str()),
46        }
47    }
48
49    pub fn file_path(&self) -> Option<&str> {
50        match self {
51            Self::SearchItem(item) => item.preview_path(),
52            Self::Diff { .. } => None,
53            Self::GitHistory { path, .. } => Some(path.as_str()),
54            Self::GitBranch { .. } | Self::GitCommit { .. } => None,
55            Self::LogStream(path) => Some(path.as_str()),
56        }
57    }
58}
59
60#[derive(Debug, Clone, PartialEq, Eq, Hash)]
61pub enum PreviewRequest {
62    Path {
63        source: PreviewSource,
64        path: String,
65    },
66    Diff {
67        source: PreviewSource,
68        left: String,
69        right: String,
70    },
71    GitHistory {
72        source: PreviewSource,
73        repo_root: String,
74        commit: String,
75        path: String,
76        line: usize,
77    },
78    GitBranch {
79        source: PreviewSource,
80        repo_root: String,
81        branch: String,
82    },
83    GitCommit {
84        source: PreviewSource,
85        repo_root: String,
86        commit: String,
87    },
88    Grep {
89        source: PreviewSource,
90        path: String,
91        line: usize,
92        text: String,
93    },
94    StdinOrCommand {
95        source: PreviewSource,
96        item: String,
97    },
98    StructuredLog {
99        source: PreviewSource,
100        path: String,
101    },
102}
103
104impl PreviewRequest {
105    pub fn from_source(source: PreviewSource, has_preview_command: bool) -> Self {
106        match source {
107            PreviewSource::LogStream(path) => Self::StructuredLog {
108                source: PreviewSource::LogStream(path.clone()),
109                path,
110            },
111            PreviewSource::Diff { left, right } => Self::Diff {
112                source: PreviewSource::Diff {
113                    left: left.clone(),
114                    right: right.clone(),
115                },
116                left,
117                right,
118            },
119            PreviewSource::GitHistory { commit, path, line } => Self::GitHistory {
120                source: PreviewSource::GitHistory {
121                    commit: commit.clone(),
122                    path: path.clone(),
123                    line,
124                },
125                repo_root: String::new(),
126                commit,
127                path,
128                line,
129            },
130            PreviewSource::GitBranch { branch } => Self::GitBranch {
131                source: PreviewSource::GitBranch {
132                    branch: branch.clone(),
133                },
134                repo_root: String::new(),
135                branch,
136            },
137            PreviewSource::GitCommit { commit } => Self::GitCommit {
138                source: PreviewSource::GitCommit {
139                    commit: commit.clone(),
140                },
141                repo_root: String::new(),
142                commit,
143            },
144            PreviewSource::SearchItem(item) => {
145                if has_preview_command {
146                    return Self::StdinOrCommand {
147                        source: PreviewSource::SearchItem(item.clone()),
148                        item: item.display_text().into_owned(),
149                    };
150                }
151
152                match item {
153                    SearchItem::Path(path) => Self::Path {
154                        source: PreviewSource::SearchItem(SearchItem::Path(path.clone())),
155                        path,
156                    },
157                    SearchItem::Grep { path, line, text } => Self::Grep {
158                        source: PreviewSource::SearchItem(SearchItem::Grep {
159                            path: path.clone(),
160                            line,
161                            text: text.clone(),
162                        }),
163                        path,
164                        line,
165                        text,
166                    },
167                    SearchItem::GitHistory {
168                        commit, path, line, ..
169                    } => Self::GitHistory {
170                        source: PreviewSource::GitHistory {
171                            commit: commit.clone(),
172                            path: path.clone(),
173                            line,
174                        },
175                        repo_root: String::new(),
176                        commit,
177                        path,
178                        line,
179                    },
180                    SearchItem::GitBranch { branch, .. } => Self::GitBranch {
181                        source: PreviewSource::GitBranch {
182                            branch: branch.clone(),
183                        },
184                        repo_root: String::new(),
185                        branch,
186                    },
187                    SearchItem::GitCommit { commit, .. } => Self::GitCommit {
188                        source: PreviewSource::GitCommit {
189                            commit: commit.clone(),
190                        },
191                        repo_root: String::new(),
192                        commit,
193                    },
194                    SearchItem::Stdin(text) => Self::StdinOrCommand {
195                        source: PreviewSource::SearchItem(SearchItem::Stdin(text.clone())),
196                        item: text,
197                    },
198                    SearchItem::Message(text) => Self::StdinOrCommand {
199                        source: PreviewSource::SearchItem(SearchItem::Message(text.clone())),
200                        item: text,
201                    },
202                }
203            }
204        }
205    }
206
207    pub fn source(&self) -> &PreviewSource {
208        match self {
209            Self::Path { source, .. }
210            | Self::Diff { source, .. }
211            | Self::GitHistory { source, .. }
212            | Self::GitBranch { source, .. }
213            | Self::GitCommit { source, .. }
214            | Self::Grep { source, .. }
215            | Self::StdinOrCommand { source, .. }
216            | Self::StructuredLog { source, .. } => source,
217        }
218    }
219
220    pub fn file_path(&self) -> Option<&str> {
221        match self {
222            Self::Path { path, .. }
223            | Self::GitHistory { path, .. }
224            | Self::Grep { path, .. }
225            | Self::StructuredLog { path, .. } => Some(path.as_str()),
226            Self::Diff { .. }
227            | Self::GitBranch { .. }
228            | Self::GitCommit { .. }
229            | Self::StdinOrCommand { .. } => None,
230        }
231    }
232}