src/main.rs
245 lines · 6259 bytes
1use crossterm::{
2 event::{self, DisableMouseCapture, EnableMouseCapture},
3 execute,
4 terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
5};
6use std::{error::Error, fs, io, sync::mpsc::channel};
7use tui::{
8 backend::{Backend, CrosstermBackend},
9 Terminal,
10};
11use youtube_tui::{
12 app::{app::App, config::Action},
13 structs::{Row, RowItem, State},
14};
15
16fn main() -> Result<(), Box<dyn Error>> {
17 init()?;
18
19 let app = App::default();
20
21 enable_raw_mode()?;
22 let mut stdout = io::stdout();
23 execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
24 let backend = CrosstermBackend::new(stdout);
25 let mut terminal = Terminal::new(backend)?;
26
27 let res = run_app(&mut terminal, app);
28
29 execute!(
30 terminal.backend_mut(),
31 LeaveAlternateScreen,
32 DisableMouseCapture
33 )?;
34 disable_raw_mode()?;
35
36 res?;
37 exit()?;
38
39 Ok(())
40}
41
42fn run_app<B: Backend>(mut terminal: &mut Terminal<B>, mut app: App) -> Result<(), Box<dyn Error>> {
43 loop {
44 if app.render {
45 let hold = ui(&mut terminal, app);
46 app = hold.0;
47 hold.1?;
48 }
49
50 if app.load {
51 app.message = Some(app.page.message(&app.config));
52 let mut watch_history = app.watch_history.clone();
53 terminal.clear()?;
54 let hold = ui(&mut terminal, app);
55 app = hold.0;
56 hold.1?;
57 let mut new_state = Vec::new();
58 for row in app.state.0.iter() {
59 let mut row_vec = Vec::new();
60 for row_item in row.items.iter() {
61 match row_item.item.load_item(&app, &mut watch_history) {
62 Ok(new) => {
63 row_vec.push(RowItem {
64 item: new,
65 ..*row_item
66 });
67 }
68 Err(e) => {
69 row_vec.push(RowItem {
70 item: row_item.item.clone(),
71 ..*row_item
72 });
73 app.message = Some(e.to_string());
74 }
75 }
76 }
77
78 new_state.push(Row {
79 items: row_vec,
80 ..*row
81 });
82 }
83
84 app.watch_history = watch_history;
85 app.state = State(new_state);
86
87 app.load = false;
88 terminal.clear()?;
89 let hold = ui(&mut terminal, app);
90 app = hold.0;
91 } else {
92 match event::read()? {
93 event::Event::Key(key) => {
94 if app.selected.is_none() {
95 let action = match app.config.keybindings.0.get(&key) {
96 Some(action) => *action,
97 None => continue,
98 };
99 match action {
100 Action::Exit => {
101 return Ok(());
102 }
103 Action::Back => {
104 let holder = app.pop();
105 app = holder.0;
106 if holder.1 {
107 terminal.clear()?;
108 }
109 app.render = true;
110 continue;
111 }
112 Action::ClearHistory => {
113 app.history = Vec::new();
114 continue;
115 }
116 Action::Home => {
117 terminal.clear()?;
118 app.home();
119 continue;
120 }
121 _ => {}
122 }
123 }
124
125 app = app.key_input(key);
126 app.render = true;
127 }
128 event::Event::Resize(_, _) => {
129 app.render = true;
130 }
131 _ => {}
132 }
133 }
134 }
135}
136
137fn ui<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> (App, Result<(), Box<dyn Error>>) {
138 let (send, recv) = channel();
139 let res = terminal.draw(|mut frame| {
140 let app = app.render(&mut frame);
141 send.send(app).unwrap();
142 });
143
144 app = recv.recv().unwrap();
145
146 app.message = None;
147 app.render = false;
148
149 match res {
150 Ok(_) => (app, Ok(())),
151 Err(e) => (app, Err(Box::new(e))),
152 }
153}
154
155fn init() -> Result<(), Box<dyn Error>> {
156 let home_dir = home::home_dir().expect("Cannot get your home directory");
157
158 let mut dir = home_dir.clone();
159
160 dir.push(".cache");
161 if dir.exists() {
162 fs::remove_dir_all(&dir)?;
163 }
164 fs::create_dir(&dir)?;
165
166 dir.push("youtube-tui");
167 if !dir.exists() {
168 fs::create_dir(&dir)?;
169 }
170
171 dir.push("thumbnails");
172
173 if !dir.exists() {
174 fs::create_dir(&dir)?;
175 }
176
177 dir = home_dir.clone();
178
179 dir.push(".config");
180
181 if !dir.exists() {
182 fs::create_dir(&dir)?;
183 }
184
185 dir.push("youtube-tui");
186
187 if !dir.exists() {
188 fs::create_dir(&dir)?;
189 }
190
191 dir = home_dir;
192
193 dir.push(".local");
194
195 if !dir.exists() {
196 fs::create_dir(&dir)?;
197 }
198
199 dir.push("share");
200
201 if !dir.exists() {
202 fs::create_dir(&dir)?;
203 }
204
205 dir.push("youtube-tui");
206
207 if !dir.exists() {
208 fs::create_dir(&dir)?;
209 }
210
211 dir.push("watch_history");
212
213 if !dir.exists() {
214 fs::create_dir(&dir)?;
215 }
216
217 dir.push("thumbnails");
218
219 if !dir.exists() {
220 fs::create_dir(&dir)?;
221 }
222
223 dir.pop();
224
225 dir.push("info");
226
227 if !dir.exists() {
228 fs::create_dir(&dir)?;
229 }
230
231 Ok(())
232}
233
234fn exit() -> Result<(), Box<dyn Error>> {
235 let mut dir = home::home_dir().expect("Cannot get your home directory");
236
237 dir.push(".cache");
238 dir.push("youtube-tui");
239
240 if dir.exists() {
241 fs::remove_dir_all(&dir)?;
242 }
243
244 Ok(())
245}