Skip to content

Commit

Permalink
feat: nowModify template func
Browse files Browse the repository at this point in the history
  • Loading branch information
dlvhdr committed Mar 7, 2025
1 parent e945e1f commit 66f84ed
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 70 deletions.
4 changes: 2 additions & 2 deletions .gh-dash.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# yaml-language-server: $schema=https://dlvhdr.github.io/gh-dash/configuration/gh-dash/schema.json
prSections:
- title: Mine
filters: is:open author:@me repo:dlvhdr/gh-dash
filters: is:open author:@me repo:dlvhdr/gh-dash updated:>={{ nowModify "-3w" }} sort:updated-desc
layout:
author:
hidden: true
- title: Review
filters: repo:dlvhdr/gh-dash -author:@me is:open
filters: repo:dlvhdr/gh-dash -author:@me is:open updated:>={{ nowModify "-2.5w" }}
- title: All
filters: repo:dlvhdr/gh-dash
issuesSections:
Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/dlvhdr/gh-dash/v4/git"
"github.com/dlvhdr/gh-dash/v4/ui"
"github.com/dlvhdr/gh-dash/v4/ui/markdown"
"github.com/dlvhdr/gh-dash/v4/utils"
)

var (
Expand Down Expand Up @@ -154,6 +155,8 @@ func init() {
defer logger.Close()
}

utils.InitTemplateHandler()

p := tea.NewProgram(
model,
tea.WithAltScreen(),
Expand Down
88 changes: 88 additions & 0 deletions docs/content/configuration/searching.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
title: Searching
linkTitle: >-
![icon:search](lucide) Searching
summary: >-
How to search and filter issues and prs
weight: 3
---

# Searching

Searching for prs and issues is done by defining sections and their filters.
Search filters are defined using [GitHub search filters][01].

For example, this section, shows open PRs authored by anyone but me, which were updated in the last 2 weeks.

```yaml
prsSections:
- title: Review
filter: >-
is:open
-author:@me
updated>={{ nowModify "-2w" }}
```
Note: don't specify `is:pr` for this setting. The dashboard always adds that filter for PR
sections.

You can define any combination of search filters. To make it easier to read and maintain
your filters, we recommend using the `>-` syntax after the `filter` key and writing one
filter per line.

For more information about writing filters for searching GitHub, see [Searching issues and pull requests][02].

## Search Templates

In addition to GitHub's filters, gh-dash adds templating functions.

### `nowModify`

The `nowModify` function helps you calculate relative dates in the ISO-8601 format (which is what GitHub expects).

Given the date today is 2025-02-02, a search filter of `updated>={{ nowModify "-1mo" }}` will output `updated>=2025-01-02`.

A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as `1w`, `-8h` or `1mo2w`.

The available units are:

- [Go's builtin durations](https://pkg.go.dev/time#ParseDuration) - `ns`, `us` (or `µs`), `ms`, `s`, `m`, `h`
- Additionally:
- `d`/`D` for days
- `w`/`W` for weeks
- `M`/`mo` for months
- `y`/`Y` for years

## Smart Filtering

By default, if the directory you launch `gh-dash` from is a clone of a remote GitHub repo (or if you
have the `GH_REPO` environment variable set to a particular remote), then for any of your PR
sections and issue sections with `filters` values in your [configuration](/configuration) that don’t
have an explicit `repo:` field, `gh-dash` adds a `repo:<RepoName>` field to the search-bar value for
them (where _`<RepoName>`_ is the name of the remote repo).

That is, `gh-dash` further filters those sections down to only the PRs/issues for the GitHub
repo name specified in your `GH_REPO` environment variable — or else the repo name of the remote
tracked by the clone directory from which `gh-dash` launched.

For that, `gh-dash` first checks and uses the repo name in the `GH_REPO` environment variable (if
you have that set). If `gh-dash` doesn’t find that, then it next checks for the value of the remote
repo name tracked by the clone directory from which you launched `gh-dash` — by looking through all
GitHub remotes configured for that clone in the following order:

1. `upstream`
2. `github`
3. `origin`

…and, otherwise, if `gh-dash` finds no remotes with any of those names, then it uses the repo name
for the first remote in the output that `git remote` shows.

To disable Smart Filtering at launch, set [`smartFilteringAtLaunch`](/configuration/gh-dash/#smartfilteringatlaunch)
to `false` in your [configuration](/configuration).

To toggle Smart Filtering on or off for the current section you’re currently viewing, either use the
`t` key — or else use whatever custom keybinding you have set for the `togglesearch` builtin in the
`keybindings` section of your [configuration](/configuration).

[01]: https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests
[02]: https://docs.github.com/en/search-github/getting-started-with-searching-on-github/understanding-the-search-syntax
37 changes: 0 additions & 37 deletions docs/content/getting-started/smartfiltering.md

This file was deleted.

10 changes: 4 additions & 6 deletions docs/data/schemas/issue-section.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,12 @@ properties:
is:open
involves:@me
-author:@me
updated>={{ nowModify "-2w" }}
```
For more information about writing filters for searching GitHub, see
[Understanding the search syntax][02] and [Searching issues and pull requests][01] in
GitHub's documentation.
For more information about writing filters for searching GitHub, see [Searching].
[01]: https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests
[02]: https://docs.github.com/en/search-github/getting-started-with-searching-on-github/understanding-the-search-syntax
[Searching]: /configuration/searching
layout:
$ref: ./layout/issue.yaml
schematize:
Expand All @@ -79,7 +77,7 @@ properties:
weight: 4
details: |
This setting defines how many issues the dashboard should fetch for the section when:
- The dashboard first loads.
- The [sref:fetch interval] elapses.
- You navigate to the next issue in a table without another fetched issue to display.
Expand Down
10 changes: 4 additions & 6 deletions docs/data/schemas/pr-section.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,12 @@ properties:
is:open
involves:@me
-author:@me
updated>={{ nowModify "-2w" }}
```
For more information about writing filters for searching GitHub, see
[Understanding the search syntax][02] and [Searching issues and pull requests][01] in
GitHub's documentation.
For more information about writing filters for searching GitHub, see [Searching].
[01]: https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests
[02]: https://docs.github.com/en/search-github/getting-started-with-searching-on-github/understanding-the-search-syntax
[Searching]: /configuration/searching
layout:
$ref: ./layout/pr.yaml
schematize:
Expand All @@ -79,7 +77,7 @@ properties:
weight: 4
details: |
This setting defines how many PRs the dashboard should fetch for the section when:
- The dashboard first loads.
- The [sref:fetch interval] elapses.
- You navigate to the next issue in a table without another fetched issue to display.
Expand Down
18 changes: 10 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/dlvhdr/gh-dash/v4

go 1.21
go 1.23.3

toolchain go1.21.4
toolchain go1.23.4

require (
github.com/atotto/clipboard v0.1.4
Expand All @@ -15,10 +15,11 @@ require (
github.com/cli/shurcooL-graphql v0.0.4
github.com/gen2brain/beeep v0.0.0-20240112042604-c7bb2cd88fea
github.com/go-playground/validator/v10 v10.18.0
github.com/go-sprout/sprout v1.0.0
github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a
github.com/shurcooL/githubv4 v0.0.0-20240120211514-18a1ae0e79dc
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.10.0
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -27,6 +28,7 @@ require (
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75 // indirect
github.com/spf13/cast v1.7.1 // indirect
)

require (
Expand Down Expand Up @@ -67,13 +69,13 @@ require (
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e // indirect
github.com/yuin/goldmark v1.7.4 // indirect
github.com/yuin/goldmark-emoji v1.0.3 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/term v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.18.0 h1:BvolUXjp4zuvkZ5YN5t7ebzbhlUtPsPm2S9NAZ5nl9U=
github.com/go-playground/validator/v10 v10.18.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-sprout/sprout v1.0.0 h1:4uxG1fZbUxfXB2OsjwzyIOK4lZQEhsksib17vVAZqOs=
github.com/go-sprout/sprout v1.0.0/go.mod h1:I6ifgKakZI/NOkFyZueoS5N0qENvI5ZWcWT7oM34vsE=
github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 h1:qZNfIGkIANxGv/OqtnntR4DfOY2+BgwR60cAcu/i3SE=
github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4/go.mod h1:kW3HQ4UdaAyrUCSSDR4xUzBKW6O2iA4uHhk7AtyYp10=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
Expand Down Expand Up @@ -119,6 +121,8 @@ github.com/shurcooL/githubv4 v0.0.0-20240120211514-18a1ae0e79dc h1:vH0NQbIDk+mJL
github.com/shurcooL/githubv4 v0.0.0-20240120211514-18a1ae0e79dc/go.mod h1:zqMwyHmnN/eDOZOdiTohqIUKUrTFX62PNlu7IJdu0q8=
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 h1:17JxqqJY66GmZVHkmAsGEkcIu0oCe3AM420QDgGwZx0=
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466/go.mod h1:9dIRpgIY7hVhoqfe0/FcYp0bpInZaT7dc3BYOprrIUE=
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand All @@ -131,6 +135,7 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af h1:6yITBqGTE2lEeTPG04SN9W+iWHCRyHqlVYILiSXziwk=
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af/go.mod h1:4F09kP5F+am0jAwlQLddpoMDM+iewkxxt6nxUQ5nq5o=
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e h1:BuzhfgfWQbX0dWzYzT1zsORLnHRv3bcRcsaUk0VmXA8=
Expand All @@ -142,6 +147,8 @@ github.com/yuin/goldmark-emoji v1.0.3 h1:aLRkLHOuBR2czCY4R8olwMjID+tENfhyFDMCRhb
github.com/yuin/goldmark-emoji v1.0.3/go.mod h1:tTkZEbwu5wkPmgTcitqddVxY9osFZiavD+r4AzQrh1U=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a h1:HinSgX1tJRX3KsL//Gxynpw5CTOAIPhgL4W8PNiIpVE=
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
Expand All @@ -151,15 +158,23 @@ golang.org/x/oauth2 v0.15.0/go.mod h1:q48ptWNTY5XWf+JNten23lcvHpLJ0ZSxF5ttTHKVCA
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
golang.org/x/term v0.28.0 h1:/Ts8HFuMR2E6IP/jlo7QVLZHggjKQbhu/7H0LJFr3Gg=
golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
Expand Down
1 change: 1 addition & 0 deletions ui/components/prssection/prssection.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ func (m *Model) FetchNextPageSectionRows() []tea.Cmd {
if limit == nil {
limit = &m.Ctx.Config.Defaults.PrsLimit
}

res, err := data.FetchPullRequests(m.GetFilters(), *limit, m.PageInfo)
if err != nil {
return constants.TaskFinishedMsg{
Expand Down
7 changes: 4 additions & 3 deletions ui/components/section/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,7 @@ func (m *BaseModel) HasRepoNameInConfiguredFilter() bool {
}

func (m *BaseModel) GetSearchValue() string {
log.Debug("GetSearchValue", "before", m.SearchValue)
searchValue := m.enrichSearchWithTemplateVars()
log.Debug("GetSearchValue", "searchValue", searchValue)
repo, err := repository.Current()
if err != nil {
return searchValue
Expand All @@ -228,7 +226,8 @@ func (m *BaseModel) enrichSearchWithTemplateVars() string {
searchVars := struct{ Now time.Time }{
Now: time.Now(),
}
tmpl, err := template.New("search").Parse(searchValue)
handler := utils.TemplateHandler()
tmpl, err := template.New("search").Funcs(handler.Build()).Parse(searchValue)
if err != nil {
return searchValue
}
Expand All @@ -238,6 +237,8 @@ func (m *BaseModel) enrichSearchWithTemplateVars() string {
return searchValue
}

log.Debug("template", "err", err, "buf", buf.String())

return buf.String()
}

Expand Down
1 change: 0 additions & 1 deletion ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmd = m.onViewedRowChanged()

case key.Matches(msg, m.keys.TogglePreview):
log.Debug("======================= TogglePreview")
m.sidebar.IsOpen = !m.sidebar.IsOpen
m.syncMainContentWidth()

Expand Down
Loading

0 comments on commit 66f84ed

Please sign in to comment.