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

custom variables don't work on windows: they aren't being evaluated #128

Closed
yhoebeke opened this issue Apr 19, 2021 · 11 comments
Closed

custom variables don't work on windows: they aren't being evaluated #128

yhoebeke opened this issue Apr 19, 2021 · 11 comments
Assignees
Labels

Comments

@yhoebeke
Copy link

@yhoebeke yhoebeke commented Apr 19, 2021

Plugin informations (please complete the following information):

  • OS: Windows 10
  • Templater version: 1.5.4
  • Obsidian version: 0.11.13
  • Templater settings: "assets/templates", Timeout: "5"

Describe the bug
It looks like the variables aren’t getting evaluated when a user function is called.

Expected behavior
user-defined variables in custom function calls should be evaluated accordingly.
The code that made me find this error can be found here : Adding your google calendar agenda to your daily journal - Share & showcase - Obsidian Forum
I wasn't able to make the code work as the variables $from $to weren't replaced by the correct values.

Screenshots
This user function:
image
image
Returns this
image

@luckman212
Copy link

@luckman212 luckman212 commented Apr 19, 2021

@yhoebeke I'm guessing this is because on Windows, the default shell is PowerShell, not bash. You should try to access the variables using PowerShell syntax, e.g. $Env:from etc. Take a look at https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables

@yhoebeke
Copy link
Author

@yhoebeke yhoebeke commented Apr 19, 2021

I am really sorry, but this may go beyond my basic coding knowledge.
I tried using "$Env:from" but I still get the same results.

I also found this : https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_variables?view=powershell-7.1
It seems $variable is a correct syntax for powershell variables.

The error seems to come from before the command is sent to the command line.
image

@luckman212
Copy link

@luckman212 luckman212 commented Apr 19, 2021

@yhoebeke See #129 ... on Windows it seems that Electron's default shell for child_process is actually cmd.exe

So, your user template would need to reference the variables in "batch file" format

echo %from%

Or, you can use PowerShell via:

powershell -command "& { Write-Host -NoNewLine ('from variable = ' + $Env:from) }"

@yhoebeke
Copy link
Author

@yhoebeke yhoebeke commented Apr 20, 2021

Thank you @luckman212 for the explanations!

echo %from% worked.

Now I tried to combine it with the agenda code (without any variables first)
Before, this code worked fine:

C:/Users/adminuser/AppData/Local/Programs/Python/Python37-32/Scripts/gcalcli --calendar "01 - ME meetings and events" --calendar "02 - ME - time-blocking"  --calendar "03 - WORK - Meetings and events" --calendar "04 - WORK - Time-blocking for activities work-related" --nocolor agenda "12:00" "2021/04/22 23:00"

and <% tp.user.gcalcli() %>
So from my understanding, this was using cmd.exe
Now however, this user function leads to this result : "undefined"
And in the console log I get this error :

Error with User Template echo Error: Command failed: powershell -command "C:/Users/adminuser/AppData/Local/Programs/Python/Python37-32/Scripts/gcalcli agenda 2021/04/20 2021/04/21"

Traceback (most recent call last):
  File "c:\users\adminuser\appdata\local\programs\python\python37-32\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\adminuser\appdata\local\programs\python\python37-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\adminuser\AppData\Local\Programs\Python\Python37-32\Scripts\gcalcli.exe\__main__.py", line 7, in <module>
  File "c:\users\adminuser\appdata\local\programs\python\python37-32\lib\site-packages\gcalcli\cli.py", line 152, in main
    gcal.AgendaQuery(start=parsed_args.start, end=parsed_args.end)
  File "c:\users\adminuser\appdata\local\programs\python\python37-32\lib\site-packages\gcalcli\gcal.py", line 1238, in AgendaQuery
    return self._display_queried_events(start, end)
  File "c:\users\adminuser\appdata\local\programs\python\python37-32\lib\site-packages\gcalcli\gcal.py", line 1187, in _display_queried_events
    return self._iterate_events(start, event_list, year_date=year_date)
  File "c:\users\adminuser\appdata\local\programs\python\python37-32\lib\site-packages\gcalcli\gcal.py", line 1056, in _iterate_events
    self._PrintEvent(event, prefix)
  File "c:\users\adminuser\appdata\local\programs\python\python37-32\lib\site-packages\gcalcli\gcal.py", line 710, in _PrintEvent
    event_color
  File "c:\users\adminuser\appdata\local\programs\python\python37-32\lib\site-packages\gcalcli\printer.py", line 89, in msg
    file.write(msg)
  File "c:\users\adminuser\appdata\local\programs\python\python37-32\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f4b0' in position 28: character maps to <undefined>

    at ChildProcess.exithandler (child_process.js:312)
    at ChildProcess.emit (events.js:310)
    at maybeClose (internal/child_process.js:1021)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:286)

So I don't understand why the code that worked before, simply using cmd.exe, doesn't work anymore.

Next, I tried using the powershell without variables.
This command and user function

<% tp.user.agenda() %>
powershell  (C:/Users/adminuser/AppData/Local/Programs/Python/Python37-32/Scripts/gcalcli agenda --nocolor)

Leads to this result
image

So instead of giving me the results for the whole week, it seems the result are cut off.
image

Next, I tried using the powershell with variables.
and long story short, it seems to be working, but the results from templater seem to be cut off

<% tp.user.agenda({from: tp.date.now("YYYY-MM-DD", 2, tp.file.title, "YYYY/MM/DD"),to: tp.date.now("YYYY-MM-DD", 5, tp.file.title, "YYYY/MM/DD")}) %>
powershell  (C:/Users/adminuser/AppData/Local/Programs/Python/Python37-32/Scripts/gcalcli --nocolor agenda %from% %to%)

result:
image

@SilentVoid13
Copy link
Owner

@SilentVoid13 SilentVoid13 commented Apr 20, 2021

Hey @yhoebeke as @luckman212 said, if you're using cmd you must use %from%, but you already figured that out.
Concerning the text "crop" from Templater, it's weird, I never saw that.
When you're executing the command in powershell (replacing %from% and %to% with the correct output) what is the result ?

@luckman212
Copy link

@luckman212 luckman212 commented Apr 20, 2021

We shouldn't mix the syntax. How about skipping cmd.exe completely, and invoking PowerShell via ComSpec (as per #129 (comment))

edit: so far it doesn't quite work (yet) - awaiting answer on #129 about overriding ComSpec

Template:

<% tp.user.TestFunc({ComSpec: 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', from: '2021-04-19', to: '2021-04-20'}) %>

user function TestFunc:

Start-Process -FilePath 'C:/Users/adminuser/AppData/Local/Programs/Python/Python37-32/Scripts/gcalcli' -ArgumentList '--nocolor', 'agenda', $Env:from, $Env:to

refs:

@luckman212
Copy link

@luckman212 luckman212 commented Apr 20, 2021

@yhoebeke SV confirms that setting ComSpec doesn't work (yet) but he may add a switch for that. For now, I found a temporary solution, you may want to try this:

Template (example)

<% tp.user.MyFunc({from: tp.date.yesterday(), to: tp.date.now()}) %>

User Function (MyFunc)

powershell.exe -command "& { Start-Process -FilePath 'C:/Users/adminuser/AppData/Local/Programs/Python/Python37-32/Scripts/gcalcli' -NoNewWindow -ArgumentList '--nocolor', 'agenda', $Env:from, $Env:to }"

@yhoebeke
Copy link
Author

@yhoebeke yhoebeke commented Apr 21, 2021

Is it just me or did my last message here disappear?

@luckman212
Copy link

@luckman212 luckman212 commented Apr 21, 2021

I think it's just you, what was the message about?

@yhoebeke
Copy link
Author

@yhoebeke yhoebeke commented Apr 21, 2021

I had written a long message last evening explaining some debugging I had done with quite some screenshots (I guess there might have been a bug when submitting my comment).
Both your user function and this user function powershell (C:/Users/adminuser/AppData/Local/Programs/Python/Python37-32/Scripts/gcalcli --nocolor agenda --details all --tsv --nodeclined %from% %to%) work to fetch the calendar.

The bottom-line of my message was that I found two (I think related) issues:

  • special characters like ° were displayed incorrectly.
  • what was causing the calendar to be "cut off" in Obsidian were Emojies in the name of the events. In the powershell, I was able to get my calendar for several days, but in Obsidian the information would be cut off just before an event that starts with an emoji. Removing all the emojies from my calendar solved this problem.

@SilentVoid13 So in the end, perhaps there is a problem in how powershell returns/encodes certain characters and/or how templater processes certain characters and emojies ?

To replicate, try asking for your agenda with this command powershell (yourpathto/gcalcli --nocolor agenda) in Templater in Obsidian vs yourpathto/gcalcli --nocolor agenda in powershell. Then add emojies in the beginning of your events and repeat the process. You should get the same behavior as I experienced.

@SilentVoid13
Copy link
Owner

@SilentVoid13 SilentVoid13 commented Apr 21, 2021

Hey @yhoebeke, this is a know problem with powershell, that does not come from Templater (re #9 #15).
I have yet to find a solution for this, I did not looked deeply into it.
Anyway glad it's working now, you will be able to specify powershell as the default shell in the next release in the settings.
And thanks @luckman212 for the help as always!
Closing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants