Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clickable links in opam show #4568

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/client/opamListCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,18 @@ let info st ~fields ~raw ~where ?normalise ?(show_empty=false)
List.fold_left (fun acc item ->
let contents = detail_printer ?normalise ~sort st nv item in
if show_empty || contents <> "" then
[ OpamConsole.colorise `blue (string_of_field ~raw item); contents ]
let name = string_of_field ~raw item in
let name_url =
try
let basic_contents = detail_printer ~prettify:true ?normalise ~sort st nv item in
let contents_url = OpamUrl.parse basic_contents in
if OpamUrl.(contents_url.transport = "http" || contents_url.transport = "https") then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A reason to not use the backend field ?

OpamConsole.url ~ref:(OpamUrl.(to_string ({contents_url with backend = `http}))) ~label:name
else
name
with OpamUrl.Parse_error _ -> name
in
[ OpamConsole.colorise `blue name_url; contents ]
:: acc
else acc)
[] (List.rev fields)
Expand Down
6 changes: 6 additions & 0 deletions src/core/opamConsole.ml
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,12 @@ let carriage_delete =
else
carriage_delete_unix

let url ~ref ~label =
if not (color ()) || Sys.win32 && get_win32_console_shim `stdout Mode = Shim then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth a comment linking to the handy https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda which describes the escape codes for Linux

label
else
Printf.sprintf "\027]8;;%s\027\\%s\027]8;;\027\\" ref label

let displaying_status = ref false

let clear_status_unix () =
Expand Down
1 change: 1 addition & 0 deletions src/core/opamConsole.mli
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ val colorise : text_style -> string -> string
val colorise' : text_style list -> string -> string
val acolor : text_style -> unit -> string -> string
val acolor_w : int -> text_style -> Format.formatter -> string -> unit
val url : ref:string -> label:string -> string

module Symbols : sig
val rightwards_arrow : OpamCompat.Uchar.t
Expand Down
26 changes: 19 additions & 7 deletions src/core/opamStd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1175,13 +1175,25 @@ module OpamFormat = struct
| '\xc2'..'\xdf' -> aux (acc - min 1 (len - i)) (i + 2)
| '\xe0'..'\xef' -> aux (acc - min 2 (len - i)) (i + 3)
| '\xf0'..'\xf4' -> aux (acc - min 3 (len - i)) (i + 4)
| '\027' ->
(try
let j = String.index_from s (ofs+i+1) 'm' - ofs in
if j > len then acc - (len - i) else
aux (acc - (j - i + 1)) (j + 1)
with Not_found | Invalid_argument _ ->
acc - (len - i))
| '\027' when i < len - 1 ->
begin match s.[ofs + i + 1] with
| '[' ->
(try
let j = String.index_from s (ofs+i+1) 'm' - ofs in
if j > len then acc - (len - i) else
aux (acc - (j - i + 1)) (j + 1)
with Not_found | Invalid_argument _ ->
acc - (len - i))
| ']' ->
(try
let esc_middle = String.index_from s (ofs+i+1) '\027' in
let esc_end = String.index_from s (esc_middle+1) '\027' in
if esc_middle > len || esc_end > len then acc - (len - i) else
aux (acc - (esc_middle - i + 2) - 7) (esc_end + 7)
with Not_found | Invalid_argument _ ->
acc - (len - i))
| _ -> acc - (len - i)
end
| _ -> aux acc (i + 1)
in
aux len 0
Expand Down