j178 / leetgo

Best LeetCode friend for geek. :snowboarder:
MIT License
535 stars 32 forks source link

[Bug] wsl can't use `leetgo open` #240

Closed rgbygv closed 12 months ago

rgbygv commented 12 months ago

leetgo debug

Leetgo version info :

dev
commit: HEAD
built at: unknown
goos: linux
goarch: amd64
module version: v1.3.10, checksum: h1:5eCgMs7qTP3NUNmo2cve0tjUnQJJMyYyVDXBwGi9fzs=

Global config dir : /root/.config/leetgo Global config file : /root/.config/leetgo/config.yaml Project root : /root/leetcode Working dir : /root/leetcode Project config file : /root/leetcode/leetgo.yaml Project configuration:

# This is the leetgo project level config, global config is at /root/.config/leetgo/config.yaml
# For more details, please refer to https://github.com/j178/leetgo
language: zh
code:
  lang: python
leetcode:
  site: https://leetcode.cn
editor:
  use: custom
  command: "code"
  args: "{{.CodeFile}} {{.DescriptionFile}}"

Full configuration :

author: 
language: zh
code:
  lang: python
  filename_template: '{{ .Id | padWithZero 4 }}{{ if .SlugIsMeaningful }}.{{ .Slug }}{{ end }}'
  separate_description_file: true
  modifiers:
    - name: removeUselessComments
  go:
    out_dir: go
    modifiers:
      - name: removeUselessComments
      - name: changeReceiverName
      - name: addNamedReturn
      - name: addMod
  python3:
    out_dir: python
    blocks:
      - name: beforeMarker
        template: |2
          from bisect import bisect_left, bisect_right
          from collections import Counter, defaultdict, deque
          from copy import deepcopy
          from functools import cache, cmp_to_key, lru_cache, reduce
          from heapq import heapify, heappop, heappush, heappushpop, heapreplace
          from itertools import accumulate, chain, count, pairwise, zip_longest
          from math import ceil, comb, floor, gcd, inf, isqrt, log2, perm, sqrt
          from operator import xor
          from string import ascii_lowercase
          from typing import List, Optional
      - name: beforeCode
        template: |
          # from sortedcontainers import SortedList
    executable: python3
  cpp:
    out_dir: cpp
    cxx: g++
    cxxflags: -O2 -std=c++17
  rust:
    out_dir: rust
  java:
    out_dir: java
leetcode:
  site: https://leetcode.cn
  credentials:
    from: cookies
    browsers: []
contest:
  out_dir: contest
  filename_template: '{{ .ContestShortSlug }}/{{ .Id }}{{ if .SlugIsMeaningful }}.{{ .Slug }}{{ end }}'
  open_in_browser: true
editor:
  use: custom
  command: code
  args: '{{.CodeFile}} {{.DescriptionFile}}'

Debug log

root@vthe ~/leetcode (main)# export DEBUG=1
root@vthe ~/leetcode (main)# lc open today
2023/11/16 16:01:49 DEBU request method=POST url=https://leetcode.cn/graphql
  body=
  │ {"operationName":"questionOfToday","query":"\n    query questionOfToday {\n        todayRecord {\n            question {\n                titleSlug\n            }\n        }\n    }","variables":{}}

2023/11/16 16:01:49 DEBU response url=https://leetcode.cn/graphql code=200
  headers=
  │ Connection: keep-alive\r
  │ Content-Length: 96\r
  │ Content-Type: application/json\r
 data="{\"data\":{\"todayRecord\":[{\"question\":{\"titleSlug\":\"longest-even-odd-subarray-with-threshold\"}}]}}"
2023/11/16 16:01:49 DEBU request method=POST url=https://leetcode.cn/graphql
  body=
  │ {"operationName":"questionData","query":"\n\tquery questionData($titleSlug: String!) {\n\t\tquestion(titleSlug: $titleSlug) {\n\t\t\tquestionId\n\t\t\tquestionFrontendId\n\t\t\tcategoryTitle\n\t\t\ttitle\n\t\t\ttitleSlug\n\t\t\tcontent\n\t\t\tisPaidOnly\n\t\t\ttranslatedTitle\n\t\t\ttranslatedContent\n\t\t\tdifficulty\n\t\t\tstatus\n\t\t\tstats\n\t\t\thints\n\t\t\tsimilarQuestions\n\t\t\tsampleTestCase\n\t\t\texampleTestcases\n\t\t\texampleTestcaseList\n\t\t\tjsonExampleTestcases\n\t\t\tmetaData\n\t\t\tcodeSnippets {\n\t\t\t\tlang\n\t\t\t\tlangSlug\n\t\t\t\tcode\n\t\t\t}\n\t\t\ttopicTags {\n\t\t\t\tname\n\t\t\t\tslug\n\t\t\t\ttranslatedName\n\t\t\t}\n\t\t\teditorType\n\t\t}\n\t}","variables":{"titleSlug":"longest-even-odd-subarray-with-threshold"}}

2023/11/16 16:01:49 DEBU response url=https://leetcode.cn/graphql code=200
  headers=
  │ Connection: keep-alive\r
  │ Content-Length: 10253\r
  │ Content-Type: application/json\r
 data="{\"data\":{\"question\":{\"questionId\":\"2866\",\"questionFrontendId\":\"2760\",\"categoryTitle\":\"Algorithms\",\"title\":\"Longest Even Odd Subarray With Threshold\",\"titleSlug\":\"longest-even-odd-subarray-with-threshold\",\"content\":\"<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>threshold</code>.</p>\n\n<p>Find the length of the <strong>longest subarray</strong> of <code>nums</code> starting at index <code>l</code> and ending at index <code>r</code> <code>(0 &lt;= l &lt;= r &lt; nums.length)</code> that satisfies the following conditions:</p>\n\n<ul>\n\t<li><code>nums[l] % 2 == 0</code></li>\n\t<li>For all indices <code>i</code> in the range <code>[l, r - 1]</code>, <code>nums[i] % 2 != nums[i + 1] % 2</code></li>\n\t<li>For all indices <code>i</code> in the range <code>[l, r]</code>, <code>nums[i] &lt;= threshold</code></li>\n</ul>\n\n<p>Return <em>an integer denoting the length of the longest such subarray.</em></p>\n\n<p><strong>Note:</strong> A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\\"example\\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [3,2,5,4], threshold = 5\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> In this example, we can select the subarray that starts at l = 1 and ends at r = 3 =&gt; [2,5,4]. This subarray satisfies the conditions.\nHence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.</pre>\n\n<p><strong class=\\"example\\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,2], threshold = 2\n<strong>Output:</strong> 1\n<strong>Explanation:</strong> In this example, we can select the subarray that starts at l = 1 and ends at r = 1 =&gt; [2]. \nIt satisfies all the conditions and we can show that 1 is the maximum possible achievable length.\n</pre>\n\n<p><strong class=\\"example\\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [2,3,4,5], threshold = 4\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> In this example, we can select the subarray that starts at l = 0 and ends at r = 2 =&gt; [2,3,4]. \nIt satisfies all the conditions.\nHence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 100 </code></li>\n\t<li><code>1 &lt;= nums[i] &lt;= 100 </code></li>\n\t<li><code>1 &lt;= threshold &lt;= 100</code></li>\n</ul>\n\",\"isPaidOnly\":false,\"translatedTitle\":\"\u6700\u957f\u5947\u5076\u5b50\u6570\u7ec4\",\"translatedContent\":\"<p>\u7ed9\u4f60\u4e00\u4e2a\u4e0b\u6807\u4ece <strong>0</strong> \u5f00\u59cb\u7684\u6574\u6570\u6570\u7ec4 <code>nums</code> \u548c\u4e00\u4e2a\u6574\u6570 <code>threshold</code> \u3002</p>\n\n<p>\u8bf7\u4f60\u4ece <code>nums</code> \u7684\u5b50\u6570\u7ec4\u4e2d\u627e\u51fa\u4ee5\u4e0b\u6807 <code>l</code> \u5f00\u5934\u3001\u4e0b\u6807 <code>r</code> \u7ed3\u5c3e <code>(0 &lt;= l &lt;= r &lt; nums.length)</code> \u4e14\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u7684 <strong>\u6700\u957f\u5b50\u6570\u7ec4</strong> \uff1a</p>\n\n<ul>\n\t<li><code>nums[l] % 2 == 0</code></li>\n\t<li>\u5bf9\u4e8e\u8303\u56f4&nbsp;<code>[l, r - 1]</code> \u5185\u7684\u6240\u6709\u4e0b\u6807 <code>i</code> \uff0c<code>nums[i] % 2 != nums[i + 1] % 2</code></li>\n\t<li>\u5bf9\u4e8e\u8303\u56f4&nbsp;<code>[l, r]</code> \u5185\u7684\u6240\u6709\u4e0b\u6807 <code>i</code> \uff0c<code>nums[i] &lt;= threshold</code></li>\n</ul>\n\n<p>\u4ee5\u6574\u6570\u5f62\u5f0f\u8fd4\u56de\u6ee1\u8db3\u9898\u76ee\u8981\u6c42\u7684\u6700\u957f\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\u3002</p>\n\n<p><strong>\u6ce8\u610f\uff1a\u5b50\u6570\u7ec4</strong> \u662f\u6570\u7ec4\u4e2d\u7684\u4e00\u4e2a\u8fde\u7eed\u975e\u7a7a\u5143\u7d20\u5e8f\u5217\u3002</p>\n\n<p>&nbsp;</p>\n\n<p><strong>\u793a\u4f8b 1\uff1a</strong></p>\n\n<pre><strong>\u8f93\u5165\uff1a</strong>nums = [3,2,5,4], threshold = 5\n<strong>\u8f93\u51fa\uff1a</strong>3\n<strong>\u89e3\u91ca\uff1a</strong>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9009\u62e9\u4ece l = 1 \u5f00\u59cb\u3001\u5230 r = 3 \u7ed3\u675f\u7684\u5b50\u6570\u7ec4 =&gt; [2,5,4] \uff0c\u6ee1\u8db3\u4e0a\u8ff0\u6761\u4ef6\u3002\n\u56e0\u6b64\uff0c\u7b54\u6848\u5c31\u662f\u8fd9\u4e2a\u5b50\u6570\u7ec4\u7684\u957f\u5ea6 3 \u3002\u53ef\u4ee5\u8bc1\u660e 3 \u662f\u6ee1\u8db3\u9898\u76ee\u8981\u6c42\u7684\u6700\u5927\u957f\u5ea6\u3002</pre>\n\n<p><strong>\u793a\u4f8b 2\uff1a</strong></p>\n\n<pre><strong>\u8f93\u5165\uff1a</strong>nums = [1,2], threshold = 2\n<strong>\u8f93\u51fa\uff1a</strong>1\n<strong>\u89e3\u91ca\uff1a</strong>\n\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9009\u62e9\u4ece l = 1 \u5f00\u59cb\u3001\u5230 r = 1 \u7ed3\u675f\u7684\u5b50\u6570\u7ec4 =&gt; [2] \u3002\n\u8be5\u5b50\u6570\u7ec4\u6ee1\u8db3\u4e0a\u8ff0\u5168\u90e8\u6761\u4ef6\u3002\u53ef\u4ee5\u8bc1\u660e 1 \u662f\u6ee1\u8db3\u9898\u76ee\u8981\u6c42\u7684\u6700\u5927\u957f\u5ea6\u3002\n</pre>\n\n<p><strong>\u793a\u4f8b 3\uff1a</strong></p>\n\n<pre><strong>\u8f93\u5165\uff1a</strong>nums = [2,3,4,5], threshold = 4\n<strong>\u8f93\u51fa\uff1a</strong>3\n<strong>\u89e3\u91ca\uff1a</strong>\n\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9009\u62e9\u4ece l = 0 \u5f00\u59cb\u3001\u5230 r = 2 \u7ed3\u675f\u7684\u5b50\u6570\u7ec4 =&gt; [2,3,4] \u3002 \n\u8be5\u5b50\u6570\u7ec4\u6ee1\u8db3\u4e0a\u8ff0\u5168\u90e8\u6761\u4ef6\u3002\n\u56e0\u6b64\uff0c\u7b54\u6848\u5c31\u662f\u8fd9\u4e2a\u5b50\u6570\u7ec4\u7684\u957f\u5ea6 3 \u3002\u53ef\u4ee5\u8bc1\u660e 3 \u662f\u6ee1\u8db3\u9898\u76ee\u8981\u6c42\u7684\u6700\u5927\u957f\u5ea6\u3002</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>\u63d0\u793a\uff1a</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 100 </code></li>\n\t<li><code>1 &lt;= nums[i] &lt;= 100 </code></li>\n\t<li><code>1 &lt;= threshold &lt;= 100</code></li>\n</ul>\n\",\"difficulty\":\"Easy\",\"status\":\"ac\",\"stats\":\"{\\"totalAccepted\\": \\"18.9K\\", \\"totalSubmission\\": \\"42K\\", \\"totalAcceptedRaw\\": 18942, \\"totalSubmissionRaw\\": 42024, \\"acRate\\": \\"45.1%\\"}\",\"hints\":[\"Brute force all the possible subarrays and find the longest that satisfies the conditions.\"],\"similarQuestions\":\"[]\",\"sampleTestCase\":\"[3,2,5,4]\n5\",\"exampleTestcases\":\"[3,2,5,4]\n5\n[1,2]\n2\n[2,3,4,5]\n4\",\"exampleTestcaseList\":[\"[3,2,5,4]\n5\",\"[1,2]\n2\",\"[2,3,4,5]\n4\"],\"jsonExampleTestcases\":\"[\\"[3,2,5,4]\\n5\\", \\"[1,2]\\n2\\", \\"[2,3,4,5]\\n4\\"]\",\"metaData\":\"{\n  \\"name\\": \\"longestAlternatingSubarray\\",\n  \\"params\\": [\n    {\n      \\"name\\": \\"nums\\",\n      \\"type\\": \\"integer[]\\"\n    },\n    {\n      \\"type\\": \\"integer\\",\n      \\"name\\": \\"threshold\\"\n    }\n  ],\n  \\"return\\": {\n    \\"type\\": \\"integer\\"\n  }\n}\",\"codeSnippets\":[{\"lang\":\"C++\",\"langSlug\":\"cpp\",\"code\":\"class Solution {\npublic:\n    int longestAlternatingSubarray(vector<int>& nums, int threshold) {\n        \n    }\n};\"},{\"lang\":\"Java\",\"langSlug\":\"java\",\"code\":\"class Solution {\n    public int longestAlternatingSubarray(int[] nums, int threshold) {\n\n    }\n}\"},{\"lang\":\"Python\",\"langSlug\":\"python\",\"code\":\"class Solution(object):\n    def longestAlternatingSubarray(self, nums, threshold):\n        \\"\\"\\"\n        :type nums: List[int]\n        :type threshold: int\n        :rtype: int\n        \\"\\"\\"\"},{\"lang\":\"Python3\",\"langSlug\":\"python3\",\"code\":\"class Solution:\n    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\"},{\"lang\":\"C\",\"langSlug\":\"c\",\"code\":\"int longestAlternatingSubarray(int* nums, int numsSize, int threshold){\n\n}\"},{\"lang\":\"C#\",\"langSlug\":\"csharp\",\"code\":\"public class Solution {\n    public int LongestAlternatingSubarray(int[] nums, int threshold) {\n\n    }\n}\"},{\"lang\":\"JavaScript\",\"langSlug\":\"javascript\",\"code\":\"/**\n * @param {number[]} nums\n * @param {number} threshold\n * @return {number}\n */\nvar longestAlternatingSubarray = function(nums, threshold) {\n\n};\"},{\"lang\":\"TypeScript\",\"langSlug\":\"typescript\",\"code\":\"function longestAlternatingSubarray(nums: number[], threshold: number): number {\n\n};\"},{\"lang\":\"PHP\",\"langSlug\":\"php\",\"code\":\"class Solution {\n\n    /**\n     * @param Integer[] $nums\n     * @param Integer $threshold\n     * @return Integer\n     */\n    function longestAlternatingSubarray($nums, $threshold) {\n\n    }\n}\"},{\"lang\":\"Swift\",\"langSlug\":\"swift\",\"code\":\"class Solution {\n    func longestAlternatingSubarray(_ nums: [Int], _ threshold: Int) -> Int {\n\n    }\n}\"},{\"lang\":\"Kotlin\",\"langSlug\":\"kotlin\",\"code\":\"class Solution {\n    fun longestAlternatingSubarray(nums: IntArray, threshold: Int): Int {\n\n    }\n}\"},{\"lang\":\"Dart\",\"langSlug\":\"dart\",\"code\":\"class Solution {\n  int longestAlternatingSubarray(List<int> nums, int threshold) {\n\n  }\n}\"},{\"lang\":\"Go\",\"langSlug\":\"golang\",\"code\":\"func longestAlternatingSubarray(nums []int, threshold int) int {\n\n}\"},{\"lang\":\"Ruby\",\"langSlug\":\"ruby\",\"code\":\"# @param {Integer[]} nums\n# @param {Integer} threshold\n# @return {Integer}\ndef longest_alternating_subarray(nums, threshold)\n\nend\"},{\"lang\":\"Scala\",\"langSlug\":\"scala\",\"code\":\"object Solution {\n    def longestAlternatingSubarray(nums: Array[Int], threshold: Int): Int = {\n\n    }\n}\"},{\"lang\":\"Rust\",\"langSlug\":\"rust\",\"code\":\"impl Solution {\n    pub fn longest_alternating_subarray(nums: Vec<i32>, threshold: i32) -> i32 {\n\n    }\n}\"},{\"lang\":\"Racket\",\"langSlug\":\"racket\",\"code\":\"(define/contract (longest-alternating-subarray nums threshold)\n  (-> (listof exact-integer?) exact-integer? exact-integer?)\n\n  )\"},{\"lang\":\"Erlang\",\"langSlug\":\"erlang\",\"code\":\"-spec longest_alternating_subarray(Nums :: [integer()], Threshold :: integer()) -> integer().\nlongest_alternating_subarray(Nums, Threshold) ->\n  .\"},{\"lang\":\"Elixir\",\"langSlug\":\"elixir\",\"code\":\"defmodule Solution do\n  @spec longest_alternating_subarray(nums :: [integer], threshold :: integer) :: integer\n  def longest_alternating_subarray(nums, threshold) do\n\n  end\nend\"}],\"topicTags\":[{\"name\":\"Array\",\"slug\":\"array\",\"translatedName\":\"\u6570\u7ec4\"},{\"name\":\"Sliding Window\",\"slug\":\"sliding-window\",\"translatedName\":\"\u6ed1\u52a8\u7a97\u53e3\"}],\"editorType\":"
wslpath: https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/: No such file or directory

cmdlet Start-Process at command pipeline position 1
Supply values for the following parameters:
FilePath:
Start-Process : Cannot process command because of one or more missing mandat
ory parameters: FilePath.
At line:1 char:1
+ start
+ ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], Paramet
   erBindingException
    + FullyQualifiedErrorId : MissingMandatoryParameter,Microsoft.PowerShel
   l.Commands.StartProcessCommand

2023/11/16 16:01:50 FATA exit status 4

Description

root@vthe ~# leetgo open today
● leetgo.yaml not found, use global config only file=/root/.config/leetgo/config.yaml
× exec: "xdg-open,x-www-browser,www-browser": executable file not found in $PATH

after install xdg-utils, and link browser

root@vthe ~# ln -s /mnt/d/CentBrowser/chrome.exe /usr/local/bin/chrome
root@vthe ~# ln -s /usr/bin/chrome /usr/bin/x-www-browser
root@vthe ~# ln -s /usr/bin/chrome /usr/bin/www-browser

leetgo open today get weird output

root@vthe ~/leetcode (main)# leetgo open today
wslpath: https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/: No such file or directory
cmdlet Start-Process at command pipeline position 1
Supply values for the following parameters:
FilePath:
Start-Process : Cannot process command because of one or more missing mandat
ory parameters: FilePath.
At line:1 char:1
+ start
+ ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], Paramet
   erBindingException
    + FullyQualifiedErrorId : MissingMandatoryParameter,Microsoft.PowerShel
   l.Commands.StartProcessCommand

× exit status 4
j178 commented 12 months ago

What will happen if you run xdg-open 'https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/'?

rgbygv commented 12 months ago
xdg-open 'https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/'
wslpath: https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/: No such file or directory

位于命令管道位置 1 的 cmdlet Start-Process
请为以下参数提供值:
FilePath:

this will ask me to input a FilePath

if i input FilePath with "D:\CentBrowser\chrome.exe" it will open chrome, but don't open url

root@vthe ~ [4]# xdg-open 'https://leetcode.cn/problems/longest-even-odd-suba
rray-with-threshold/'
wslpath: https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/: No such file or directory

位于命令管道位置 1 的 cmdlet Start-Process
请为以下参数提供值:
FilePath: "D:\CentBrowser\chrome.exe"
j178 commented 12 months ago

Apparently xdg-open has some issues working under WSL.

Some helpful links:

rgbygv commented 12 months ago

Thanks for your help. Yesterday I wanted to try leetgo open today in wsl, and tried it for an hour, so reported it as a bug.

j178 commented 12 months ago

Thanks for your report.

I'll switch from github.com/pkg/browser to github.com/cli/browser, which supports wslview command to open a URL from WSL in Windows.

After #241 merged, could you try the following steps?

  1. Remove xdg-utils and all your browser links
  2. Install wslu package:
    sudo add-apt-repository ppa:wslutilities/wslu
    sudo apt update
    sudo apt install wslu
  3. Try wslview https://baidu.com
  4. Run the master version leetgo:
    go install github.com/j178/leetgo@master
    leetgo open today
rgbygv commented 12 months ago

Thanks, it's work now!