Skip to content

Commit

Permalink
Only disable links in VS code in the list
Browse files Browse the repository at this point in the history
  • Loading branch information
mo8it committed Aug 27, 2024
1 parent 5556d42 commit cba4a6f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/app_state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{bail, Context, Error, Result};
use std::{
env,
fs::{self, File},
io::{Read, StdoutLock, Write},
path::Path,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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]| {
Expand Down
8 changes: 7 additions & 1 deletion src/list/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
12 changes: 1 addition & 11 deletions src/term.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{
cell::Cell,
env, fmt, fs,
fmt, fs,
io::{self, BufRead, StdoutLock, Write},
};

Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit cba4a6f

Please sign in to comment.