5
$\begingroup$

WakaTime is a tool for logging one's coding activities, and now have amazing coverage for code editors. Although I write Wolfram in VS Code, I still spend a lot of time tweaking and testing in the notebook interface. Is there a way to link the WakaTime API to Mathematica?

$\endgroup$
1
  • $\begingroup$ I have an attempt in the early stage, and I put it in a GitHub gist. It is far from desirable, But I hope this brings wisdom from the community. $\endgroup$
    – Gravifer
    Commented Feb 23, 2021 at 8:49

1 Answer 1

1
$\begingroup$

I have expanded the initial thoughts into a draft package.

(* ::Package:: *)

(* ::Title:: *)
(*Mathematica WakaTime Plugin*)


(* ::Author:: *)
(*Author: Gravifer*)
(*Date: 2021-02-21*)
(*Version: 0.0.3*)


BeginPackage["WakaTime`"]
ClearAll[Evaluate[Context[] <> "*"]]


Begin["`Private`"]
ClearAll[Evaluate[Context[] <> "*"]]


GetWakaEXE[]:=If[#[["ExitCode"]]==0,Identity[$wakaEXE=StringReplace[#[["StandardOutput"]],{"\\"->"/","\r"->"","\n"->""}]],Print["wakatime.exe is not found"];$Failed]&@RunProcess[{"where.exe", "wakatime"}]
GetWakaEXE[]
$WakaTimePluginVersion="v0.0.3"
WakaTime::unsaved="The current file is not on the disk, and the heartbeat cannot be sent. (This message only issue once for the current file)";
Once[If[ValueQ[$PrePrint],$WakaTimePreReadBackUp=$PreRead]]


UpdateHeartbeat[]:=(Once[$LastHeartbeat=AbsoluteTime[]];
  If[Not[ValueQ[$LastHeartbeat]]&&(AbsoluteTime[]-$LastHeartbeat>0),
  Quiet@Unset@Once[$LastHeartbeat=AbsoluteTime[]];Once[$LastHeartbeat=AbsoluteTime[]];True,
  False])


GetGitFolder[]:=(If[#[["ExitCode"]]==0,
  Set[$gitFolder,Last[FileNameSplit@#[["StandardOutput"]]]],
  Unset[$gitFolder]]&@Evaluate[RunProcess[{"git","rev-parse","--show-toplevel"}]];)


GetCurrentProject[]:=($CurrentProject=If[ValueQ[$gitFolder],
      StringTemplate["\"`gitFolder`\""][<|"gitFolder"->$gitFolder|>],"\"wolfram-wakatime\""])


GetCurrentFile[]:=If[Not[Enclose[StringReplace[Quiet@Confirm@NotebookFileName[],"\\"->"/"],Null&]===Null],
  Set[$CurrentFile,StringReplace[Quiet@NotebookFileName[],"\\"->"/"]],
  Unset[$CurrentFile]];


ManageEvaluationNotebookChange[]:=(#;If[Not[$CurrentNotebook===EvaluationNotebook[]],
  Quiet@Unset@Once[Message[WakaTime::unsaved]];GetGitFolder[];GetCurrentProject[];GetCurrentFile[];Unset@#];#;)&@
  Unevaluated[Once[$CurrentNotebook=Quiet@EvaluationNotebook[]]];


SendHeartbeat[]:=If[ValueQ[$CurrentFile],StartProcess[{$wakaEXE, "--write",
  "--plugin"  ,
StringTemplate["\"Mathematica-wakatime-gravifer-plugin/`PlugInVersion`\""][<|"PlugInVersion"->$WakaTimePluginVersion|>],
  "--entity"  ,
StringTemplate["\"`CurrentFile`\""][<|"CurrentFile"->$CurrentFile|>],
  "--language",
"\"Wolfram\"",
  "--project", 
StringTemplate["\"`CurrentProject`\""][<|"CurrentProject"->$CurrentProject|>]}],
      Once[Message[WakaTime::unsaved]]]


$PreRead=(ManageEvaluationNotebookChange[];If[UpdateHeartbeat[],SendHeartbeat[]];
If[ValueQ[$WakaTimePreReadBackUp],$WakaTimePreReadBackUp[#],#])&;


End[]


EndPackage[]

Downloading the file from GitHub Gist and follow the guide there should help you try this out. Everything is still in the early stage.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.