Skip to content

Commit cba4a6f

Browse files
committed
Only disable links in VS code in the list
1 parent 5556d42 commit cba4a6f

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

src/app_state.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::{bail, Context, Error, Result};
22
use std::{
3+
env,
34
fs::{self, File},
45
io::{Read, StdoutLock, Write},
56
path::Path,
@@ -44,6 +45,8 @@ pub struct AppState {
4445
file_buf: Vec<u8>,
4546
official_exercises: bool,
4647
cmd_runner: CmdRunner,
48+
// Running in VS Code.
49+
vs_code: bool,
4750
}
4851

4952
impl AppState {
@@ -131,6 +134,7 @@ impl AppState {
131134
file_buf: Vec::with_capacity(2048),
132135
official_exercises: !Path::new("info.toml").exists(),
133136
cmd_runner,
137+
vs_code: env::var_os("TERM_PROGRAM").is_some_and(|v| v == "vscode"),
134138
};
135139

136140
let state_file_status = slf.update_from_file();
@@ -163,6 +167,11 @@ impl AppState {
163167
&self.cmd_runner
164168
}
165169

170+
#[inline]
171+
pub fn vs_code(&self) -> bool {
172+
self.vs_code
173+
}
174+
166175
// Write the state file.
167176
// The file's format is very simple:
168177
// - The first line is a comment.
@@ -457,6 +466,7 @@ mod tests {
457466
file_buf: Vec::new(),
458467
official_exercises: true,
459468
cmd_runner: CmdRunner::build().unwrap(),
469+
vs_code: false,
460470
};
461471

462472
let mut assert = |done: [bool; 3], expected: [Option<usize>; 3]| {

src/list/state.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,13 @@ impl<'a> ListState<'a> {
163163
writer.write_str(exercise.name)?;
164164
writer.write_ascii(&self.name_col_padding[exercise.name.len()..])?;
165165

166-
terminal_file_link(&mut writer, exercise.path, Color::Blue)?;
166+
// The list links aren't shown correctly in VS Code on Windows.
167+
// But VS Code shows its own links anyway.
168+
if self.app_state.vs_code() {
169+
writer.write_str(exercise.path)?;
170+
} else {
171+
terminal_file_link(&mut writer, exercise.path, Color::Blue)?;
172+
}
167173

168174
next_ln(stdout)?;
169175
stdout.queue(ResetColor)?;

src/term.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{
2-
cell::Cell,
3-
env, fmt, fs,
2+
fmt, fs,
43
io::{self, BufRead, StdoutLock, Write},
54
};
65

@@ -11,10 +10,6 @@ use crossterm::{
1110
Command, QueueableCommand,
1211
};
1312

14-
thread_local! {
15-
static VS_CODE: Cell<bool> = Cell::new(env::var_os("TERM_PROGRAM").is_some_and(|v| v == "vscode"));
16-
}
17-
1813
pub struct MaxLenWriter<'a, 'b> {
1914
pub stdout: &'a mut StdoutLock<'b>,
2015
len: usize,
@@ -161,11 +156,6 @@ pub fn terminal_file_link<'a>(
161156
path: &str,
162157
color: Color,
163158
) -> io::Result<()> {
164-
// VS Code shows its own links. This also avoids some issues, especially on Windows.
165-
if VS_CODE.get() {
166-
return writer.write_str(path);
167-
}
168-
169159
let canonical_path = fs::canonicalize(path).ok();
170160

171161
let Some(canonical_path) = canonical_path.as_deref().and_then(|p| p.to_str()) else {

0 commit comments

Comments
 (0)