summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/renderer.rs31
-rw-r--r--src/util/language.rs33
-rw-r--r--src/util/mod.rs1
3 files changed, 39 insertions, 26 deletions
diff --git a/src/renderer.rs b/src/renderer.rs
index ad5201f..f492f1e 100644
--- a/src/renderer.rs
+++ b/src/renderer.rs
@@ -4,7 +4,11 @@ use std::{
path::Path,
};
-use crate::{logger::Logger, normalizer::NormalizedPath, util::path_display::display_path};
+use crate::{
+ logger::Logger,
+ normalizer::NormalizedPath,
+ util::{language::detect_language, path_display::display_path},
+};
const DEFAULT_MAX_FILE_SIZE: u64 = 1_000_000;
@@ -142,31 +146,6 @@ fn human_readable_size(bytes: u64) -> String {
}
}
-fn detect_language(filename: &Path, contents: &str) -> &'static str {
- let ext = filename
- .extension()
- .and_then(|e| e.to_str())
- .map(|e| e.to_ascii_lowercase());
- let ext_str = ext.as_deref();
- match ext_str {
- Some("rs") => "rust",
- Some("py") => "python",
- Some("json") => "json",
- _ => {
- if let Some(first_line) = contents.lines().next() {
- if first_line.contains("python")
- {
- return "python";
- }
- if first_line.contains("bash") {
- return "bash";
- }
- }
- ""
- }
- }
-}
-
#[cfg(test)]
mod tests {
use std::{
diff --git a/src/util/language.rs b/src/util/language.rs
new file mode 100644
index 0000000..fae7bf9
--- /dev/null
+++ b/src/util/language.rs
@@ -0,0 +1,33 @@
+use std::path::Path;
+
+pub fn detect_language(filename: &Path, contents: &str) -> &'static str {
+ detect_from_extension(filename)
+ .or_else(|| detect_from_shebang(contents))
+ .unwrap_or("")
+}
+
+fn detect_from_extension(filename: &Path) -> Option<&'static str> {
+ let ext = filename
+ .extension()
+ .and_then(|e| e.to_str())
+ .map(|e| e.to_ascii_lowercase());
+ let ext_str = ext.as_deref();
+ match ext_str {
+ Some("rs") => Some("rust"),
+ Some("py") => Some("python"),
+ Some("json") => Some("json"),
+ _ => None,
+ }
+}
+
+fn detect_from_shebang(contents: &str) -> Option<&'static str> {
+ if let Some(first_line) = contents.lines().next() {
+ if first_line.contains("python") {
+ return Some("python");
+ }
+ if first_line.contains("bash") {
+ return Some("bash");
+ }
+ }
+ None
+}
diff --git a/src/util/mod.rs b/src/util/mod.rs
index 1ba817e..5595a45 100644
--- a/src/util/mod.rs
+++ b/src/util/mod.rs
@@ -1 +1,2 @@
+pub mod language;
pub mod path_display;