Skip to content

Commit

Permalink
Add doc on detemining whether the machine is a laptop or desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Sep 10, 2021
1 parent 1ae0955 commit 0b2692a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions docs/TEMPLATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* [Using `.chezmoitemplates`](#using-chezmoitemplates)
* [Using `.chezmoitemplates` for creating similar files](#using-chezmoitemplates-for-creating-similar-files)
* [Passing multiple arguments](#passing-multiple-arguments)
* [Useful templates](#useful-templates)
* [Determine whether the current machine is a laptop or desktop](#determine-whether-the-current-machine-is-a-laptop-or-desktop)

---

Expand Down Expand Up @@ -472,3 +474,29 @@ it with a dictionary, for example `.local/share/chezmoi/small-font.yml.tmpl`:
```

---

## Useful templates

---

### Determine whether the current machine is a laptop or desktop

The following template sets the `$chassisType` variable to `"desktop"` or
`"laptop"` on macOS, Linux, and Windows.

```
{{- $chassisType := "desktop" }}
{{- if (eq .chezmoi.os "darwin") }}
{{- if contains "MacBook" (output "sysctl" "-n" "hw.model") }}
{{- $chassisType = "laptop" }}
{{- else }}
{{- $chassisType = "desktop" }}
{{- end }}
{{- else if (eq .chezmoi.os "linux") }}
{{- $chassisType = (output "hostnamectl" "--json=short" | mustFromJson).Chassis }}
{{- else if (eq .chezmoi.os "windows") }}
{{- $chassisType = (output "powershell.exe" "-noprofile" "-command" "if (Get-WmiObject -Class win32_battery -ComputerName localhost) { echo laptop } else { echo desktop }") }}
{{- end }}
```

---

0 comments on commit 0b2692a

Please sign in to comment.