Skip to content

Commit 29b13fd

Browse files
committed
Actually toggle projects
1 parent 7b3abc8 commit 29b13fd

6 files changed

Lines changed: 78 additions & 24 deletions

File tree

cmd/root.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"gitte/config"
6+
"gitte/internal"
67
"os"
78

89
"github.com/spf13/cobra"
@@ -55,6 +56,14 @@ Gitte also contain other sub commands for utilities managing such a setup.
5556
return fmt.Errorf("error loading gitte config: %w", err)
5657
}
5758

59+
// if attribute filter-toggles is not set to "false", we filter the toggles based on the toggle file
60+
if val, ok := cmd.Annotations["filter-toggles"]; !ok || val != "false" {
61+
toggledProjects, err := internal.ReadToggledProjects(cwd)
62+
if err != nil {
63+
return fmt.Errorf("error reading toggled projects: %w", err)
64+
}
65+
gitteConfig.FilterToggles(toggledProjects)
66+
}
5867
cmd.SetContext(config.ContextWithConfig(ctx, gitteConfig))
5968

6069
return nil

cmd/toggle.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111
var toggleCmd = &cobra.Command{
1212
Use: "toggle",
1313
Annotations: map[string]string{
14-
"need-config": "true",
14+
"need-config": "true",
15+
"filter-toggles": "false",
1516
},
1617
Short: "Toggle projects on/off with an interactive TUI. Navigate with arrow keys and toggle with space.",
1718
Long: `Opens an interactive terminal UI where you can:

config/config.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package config
22

3-
import "context"
3+
import (
4+
"context"
5+
)
46

57
type GitteConfig struct {
68
StartupChecks StartupCheckMap `yaml:"startup,omitempty"`
@@ -59,3 +61,27 @@ func CwdFromContext(ctx context.Context) string {
5961
func ContextWithCwd(ctx context.Context, cwd string) context.Context {
6062
return context.WithValue(ctx, cwdContextKey, cwd)
6163
}
64+
65+
// ToggledProjects contain a map of project keys that have been explicitly toggled.
66+
type ToggledProjects = map[string]bool
67+
68+
// FilterToggles filters the projects in the GitteConfig based on the provided toggled projects.
69+
// It removes projects that are default disabled and not toggled on,
70+
// as well as projects that are default enabled and toggled off.
71+
func (cfg *GitteConfig) FilterToggles(toggledProjects ToggledProjects) {
72+
filteredProjects := make(map[string]ProjectConfig)
73+
for key, project := range cfg.Projects {
74+
enabled, isToggled := toggledProjects[key]
75+
if project.DefaultDisabled {
76+
if isToggled && enabled {
77+
filteredProjects[key] = project
78+
}
79+
} else {
80+
if !isToggled || enabled {
81+
filteredProjects[key] = project
82+
}
83+
}
84+
}
85+
86+
cfg.Projects = filteredProjects
87+
}

internal/actions-tui.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package internal
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"gitte/executor"
7+
)
8+
9+
type actionOutputHandler struct {
10+
}
11+
12+
func NewActionOutputHandler() *actionOutputHandler {
13+
return &actionOutputHandler{}
14+
}
15+
16+
func (oh *actionOutputHandler) HandleOutput(ctx context.Context, output executor.Output) error {
17+
prefix := fmt.Sprintf("[%s][%s]: ", output.CmdName, output.Stream)
18+
fmt.Printf("%s%s", prefix, string(output.Output))
19+
return nil
20+
}

internal/toggle.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package internal
22

33
import (
4+
"gitte/config"
45
"os"
56
"path/filepath"
67
"strings"
78
)
89

9-
// ToggledProjects contain a map of project keys that have been explicitly toggled.
10-
type ToggledProjects = map[string]bool
11-
1210
const toggleFileName = ".gitte-projects-toggled"
1311

14-
func SaveToggledProjects(cwd string, toggledProjects ToggledProjects) error {
12+
func SaveToggledProjects(cwd string, toggledProjects config.ToggledProjects) error {
1513
var lines []string
1614
for project, toggled := range toggledProjects {
1715
line := project + ":"
@@ -31,27 +29,27 @@ func getToggleFilePath(cwd string) string {
3129
return filepath.Join(cwd, toggleFileName)
3230
}
3331

34-
func ReadToggledProjects(cwd string) (ToggledProjects, error) {
32+
func ReadToggledProjects(cwd string) (config.ToggledProjects, error) {
3533

3634
filePath := getToggleFilePath(cwd)
3735
return parseToggleFile(filePath)
3836
}
3937

40-
func parseToggleFile(filePath string) (ToggledProjects, error) {
38+
func parseToggleFile(filePath string) (config.ToggledProjects, error) {
4139
content, err := os.ReadFile(filePath)
4240

4341
if err != nil {
4442
if os.IsNotExist(err) {
45-
return ToggledProjects{}, nil
43+
return config.ToggledProjects{}, nil
4644
}
4745
return nil, err
4846
}
4947

5048
return parseToggleFileContent(content)
5149
}
5250

53-
func parseToggleFileContent(content []byte) (ToggledProjects, error) {
54-
toggledProjects := make(ToggledProjects)
51+
func parseToggleFileContent(content []byte) (config.ToggledProjects, error) {
52+
toggledProjects := make(config.ToggledProjects)
5553
// The file is parsed a each newline being project:(true|false)
5654
// for example my-project:true
5755
lines := strings.Split(string(content), "\n")

internal/toggle_tui.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,37 @@ type toggleModel struct {
2323
cursor int
2424
viewportOffset int // For scrolling large lists
2525
cwd string
26-
toggledProjects ToggledProjects
26+
toggledProjects config.ToggledProjects
2727
cfg *config.GitteConfig
2828
width int
2929
height int
3030
}
3131

3232
var (
3333
titleStyle = lipgloss.NewStyle().
34-
Bold(true).
35-
Foreground(lipgloss.Color("170")).
36-
MarginBottom(1)
34+
Bold(true).
35+
Foreground(lipgloss.Color("170")).
36+
MarginBottom(1)
3737

3838
selectedStyle = lipgloss.NewStyle().
39-
Foreground(lipgloss.Color("170")).
40-
Bold(true)
39+
Foreground(lipgloss.Color("170")).
40+
Bold(true)
4141

4242
cursorStyle = lipgloss.NewStyle().
43-
Foreground(lipgloss.Color("170"))
43+
Foreground(lipgloss.Color("170"))
4444

4545
enabledStyle = lipgloss.NewStyle().
46-
Foreground(lipgloss.Color("42"))
46+
Foreground(lipgloss.Color("42"))
4747

4848
disabledStyle = lipgloss.NewStyle().
49-
Foreground(lipgloss.Color("196"))
49+
Foreground(lipgloss.Color("196"))
5050

5151
customStyle = lipgloss.NewStyle().
52-
Foreground(lipgloss.Color("226"))
52+
Foreground(lipgloss.Color("226"))
5353

5454
helpStyle = lipgloss.NewStyle().
55-
Foreground(lipgloss.Color("241")).
56-
MarginTop(1)
55+
Foreground(lipgloss.Color("241")).
56+
MarginTop(1)
5757
)
5858

5959
func newToggleModel(cfg *config.GitteConfig, cwd string) (*toggleModel, error) {
@@ -342,7 +342,7 @@ func (m *toggleModel) resetAllProjects() {
342342
proj.CurrentState = proj.DefaultState
343343
proj.IsCustom = false
344344
}
345-
m.toggledProjects = make(ToggledProjects)
345+
m.toggledProjects = make(config.ToggledProjects)
346346
}
347347

348348
// updateViewport adjusts the viewport offset to keep the cursor visible

0 commit comments

Comments
 (0)