summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-08-28 01:19:53 +0200
committermo8it <mo8it@proton.me>2024-08-28 01:19:53 +0200
commitcba4a6f9c8f3b76ccfbf8c4c2aab6adda649df64 (patch)
tree03abc8181e57cda7e6f537baab672a3880a42b0a
parent5556d42b46e3bfe281343d69da588378c728c089 (diff)
Only disable links in VS code in the list
-rw-r--r--src/app_state.rs10
-rw-r--r--src/list/state.rs8
-rw-r--r--src/term.rs12
3 files changed, 18 insertions, 12 deletions
diff --git a/src/app_state.rs b/src/app_state.rs
index 1000047..b88c125 100644
--- a/src/app_state.rs
+++ b/src/app_state.rs
@@ -1,5 +1,6 @@
use anyhow::{bail, Context, Error, Result};
use std::{
+ env,
fs::{self, File},
io::{Read, StdoutLock, Write},
path::Path,
@@ -44,6 +45,8 @@ pub struct AppState {
file_buf: Vec<u8>,
official_exercises: bool,
cmd_runner: CmdRunner,
+ // Running in VS Code.
+ vs_code: bool,
}
impl AppState {
@@ -131,6 +134,7 @@ impl AppState {
file_buf: Vec::with_capacity(2048),
official_exercises: !Path::new("info.toml").exists(),
cmd_runner,
+ vs_code: env::var_os("TERM_PROGRAM").is_some_and(|v| v == "vscode"),
};
let state_file_status = slf.update_from_file();
@@ -163,6 +167,11 @@ impl AppState {
&self.cmd_runner
}
+ #[inline]
+ pub fn vs_code(&self) -> bool {
+ self.vs_code
+ }
+
// Write the state file.
// The file's format is very simple:
// - The first line is a comment.
@@ -457,6 +466,7 @@ mod tests {
file_buf: Vec::new(),
official_exercises: true,
cmd_runner: CmdRunner::build().unwrap(),
+ vs_code: false,
};
let mut assert = |done: [bool; 3], expected: [Option<usize>; 3]| {
diff --git a/src/list/state.rs b/src/list/state.rs
index 51e4cfa..5f0cda3 100644
--- a/src/list/state.rs
+++ b/src/list/state.rs
@@ -163,7 +163,13 @@ impl<'a> ListState<'a> {
writer.write_str(exercise.name)?;
writer.write_ascii(&self.name_col_padding[exercise.name.len()..])?;
- terminal_file_link(&mut writer, exercise.path, Color::Blue)?;
+ // The list links aren't shown correctly in VS Code on Windows.
+ // But VS Code shows its own links anyway.
+ if self.app_state.vs_code() {
+ writer.write_str(exercise.path)?;
+ } else {
+ terminal_file_link(&mut writer, exercise.path, Color::Blue)?;
+ }
next_ln(stdout)?;
stdout.queue(ResetColor)?;
diff --git a/src/term.rs b/src/term.rs
index 0416c30..ee8dbf8 100644
--- a/src/term.rs
+++ b/src/term.rs
@@ -1,6 +1,5 @@
use std::{
- cell::Cell,
- env, fmt, fs,
+ fmt, fs,
io::{self, BufRead, StdoutLock, Write},
};
@@ -11,10 +10,6 @@ use crossterm::{
Command, QueueableCommand,
};
-thread_local! {
- static VS_CODE: Cell<bool> = Cell::new(env::var_os("TERM_PROGRAM").is_some_and(|v| v == "vscode"));
-}
-
pub struct MaxLenWriter<'a, 'b> {
pub stdout: &'a mut StdoutLock<'b>,
len: usize,
@@ -161,11 +156,6 @@ pub fn terminal_file_link<'a>(
path: &str,
color: Color,
) -> io::Result<()> {
- // VS Code shows its own links. This also avoids some issues, especially on Windows.
- if VS_CODE.get() {
- return writer.write_str(path);
- }
-
let canonical_path = fs::canonicalize(path).ok();
let Some(canonical_path) = canonical_path.as_deref().and_then(|p| p.to_str()) else {