Skip to content

Instantly share code, notes, and snippets.

@smeech
Last active February 18, 2025 15:07
Show Gist options
  • Select an option

  • Save smeech/632a50aece7c75eb48cb0eaa488532b9 to your computer and use it in GitHub Desktop.

Select an option

Save smeech/632a50aece7c75eb48cb0eaa488532b9 to your computer and use it in GitHub Desktop.
[Temperature C/°F conversion] Espanso temperature C/°F conversion. From conversation at: https://discord.com/channels/884163483409731584/1013914627886817372/1253000300730515508. Usage: ::10C → 10C / 50.00°F or ::10F → 10°F / -12.22C #espanso #python #regex
# Espanso temperature C/°F conversion
# From conversation at: https://discord.com/channels/884163483409731584/1013914627886817372/1253000300730515508
# Usage: ::10C → 10C / 50.00°F, or ::10F → 10°F / -12.22C
matches:
- regex: ::(?P<value>\d+(?:\.\d+)?)(?P<unit>[CF])
replace: "{{output}}"
vars:
- name: output
type: script
params:
args:
- python
- -c
- |
import sys
sys.stdout.reconfigure(encoding='utf-8') # May be necessary if "°" doesn't render
if "{{unit}}" == "C":
print(f"{{value}}C / {{{value}} * 9 / 5 + 32:.2f}°F")
else:
conv_value = ({{value}} - 32) * 5 / 9
print(f"{{{value}}}°F / {({{value}} - 32) * 5 / 9:.2f}C")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment