aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2024-07-14 19:31:42 +0100
committerAkshay <[email protected]>2024-07-14 19:31:42 +0100
commit80ac5c569114e9f99a37119d97a6050b438f573a (patch)
tree9b389374e53ce304f31df44b2ebbd2eeb6b363b8 /src/main.rs
init
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..ce7c84c
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,102 @@
1mod app;
2mod config;
3
4use std::{
5 env, fs,
6 path::Path,
7 sync::{mpsc, Arc, RwLock},
8 thread,
9 time::Duration,
10};
11
12use app::App;
13use console::{Key, Term};
14use notify::{Event as WatchEvent, EventKind as WatchEventKind, RecursiveMode, Watcher};
15
16fn main() {
17 let mut args = env::args();
18 let _ = args.next();
19
20 let language = match args.next().as_ref().map(|s| s.as_str()) {
21 Some("rust") => tree_sitter_rust::language(),
22 Some("tsx") | Some("typescript") => tree_sitter_typescript::language_tsx(),
23 Some("javascript") => tree_sitter_javascript::language(),
24 Some("python") => tree_sitter_python::language(),
25 Some("ruby") => tree_sitter_ruby::language(),
26 Some("markdown") => tree_sitter_md::language(),
27 Some(s) => panic!("invalid language passed: {s}"),
28 None => panic!("no language passed"),
29 };
30 let path = args.next().expect("no arg passed");
31 let query_path = args.next();
32 let src = fs::read_to_string(&path).expect("unable to read file");
33
34 let app = Arc::new(RwLock::new(App::new(
35 src.as_bytes(),
36 &path,
37 query_path.as_ref(),
38 language,
39 )));
40
41 let watch_fn = |watcher_app: Arc<RwLock<App>>| {
42 move |ev| {
43 if let Ok(WatchEvent {
44 kind: WatchEventKind::Modify(..),
45 ..
46 }) = ev
47 {
48 if let Ok(mut locked) = watcher_app.try_write() {
49 locked.reload();
50 locked.draw();
51 };
52 }
53 }
54 };
55
56 let mut watcher1 = notify::recommended_watcher(watch_fn(Arc::clone(&app))).unwrap();
57 watcher1
58 .watch(Path::new(&path), RecursiveMode::NonRecursive)
59 .unwrap();
60
61 let mut watcher2 = notify::recommended_watcher(watch_fn(Arc::clone(&app))).unwrap();
62 if let Some(query_path) = query_path {
63 watcher2
64 .watch(Path::new(&query_path), RecursiveMode::NonRecursive)
65 .unwrap();
66 }
67
68 let (tx, rx) = mpsc::channel();
69 let tx0 = tx.clone();
70 thread::spawn(move || {
71 let term = Term::stdout();
72 loop {
73 if let Ok(Key::Char(ev)) = term.read_key() {
74 tx0.send(ev).unwrap();
75 }
76 }
77 });
78
79 if let Ok(locked) = app.try_read() {
80 locked.draw();
81 }
82
83 loop {
84 match rx.try_recv() {
85 Ok(ev) => {
86 if let Ok(mut locked) = app.try_write() {
87 match ev {
88 '>' => locked.increase_indent(),
89 '<' => locked.decrease_indent(),
90 'n' => locked.toggle_ranges(),
91 's' => locked.toggle_source(),
92 'r' => locked.reload(),
93 _ => (),
94 }
95 locked.draw();
96 }
97 }
98 _ => (),
99 }
100 thread::sleep(Duration::from_millis(10));
101 }
102}