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

Implement ASCII art only flag #45

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ You can run the `gh skyline` command with the following flags:
- Examples: `gh skyline --year 2020`, `gh skyline --year 2014-2024`
- `-w`, `--web`: Open the GitHub profile for the authenticated or specified user.
- Example: `gh skyline --web`, `gh skyline --user mona --web`
- `--art-only`: Show the ASCII art preview without generating an STL file.

### Examples

Expand Down Expand Up @@ -78,6 +79,12 @@ Generate a skyline from the user's join year to the current year:
gh skyline --full
```

Generate only the ASCII preview for a skyline:

```bash
gh skyline --art-only
```

Enable debug logging:

```bash
Expand Down
12 changes: 7 additions & 5 deletions ascii/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var ErrInvalidGrid = errors.New("invalid contribution grid")
// GenerateASCII creates a 2D ASCII art representation of the contribution data.
// It returns the generated ASCII art as a string and an error if the operation fails.
// When includeHeader is true, the output includes the header template.
func GenerateASCII(contributionGrid [][]types.ContributionDay, username string, year int, includeHeader bool) (string, error) {
func GenerateASCII(contributionGrid [][]types.ContributionDay, username string, year int, includeHeader bool, includeUserInfo bool) (string, error) {
if len(contributionGrid) == 0 {
return "", ErrInvalidGrid
}
Expand Down Expand Up @@ -74,10 +74,12 @@ func GenerateASCII(contributionGrid [][]types.ContributionDay, username string,
buffer.WriteRune('\n')
}

// Add centered user info below
buffer.WriteString("\n")
buffer.WriteString(centerText(username))
buffer.WriteString(centerText(fmt.Sprintf("%d", year)))
if includeUserInfo {
// Add centered user info below
buffer.WriteString("\n")
buffer.WriteString(centerText(username))
buffer.WriteString(centerText(fmt.Sprintf("%d", year)))
}

return buffer.String(), nil
}
Expand Down
26 changes: 24 additions & 2 deletions ascii/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,24 @@ func TestGenerateASCII(t *testing.T) {
includeHeader: false,
wantErr: false,
},
{
name: "no header",
grid: makeTestGrid(3, 7),
user: "testuser",
year: 2023,
includeHeader: false,
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := GenerateASCII(tt.grid, tt.user, tt.year, tt.includeHeader)
result, err := GenerateASCII(tt.grid, tt.user, tt.year, tt.includeHeader, tt.includeHeader)
if (err != nil) != tt.wantErr {
t.Errorf("GenerateASCII() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if !tt.wantErr && tt.includeHeader {
// Existing validation code...
if !strings.Contains(result, "testuser") {
t.Error("Generated ASCII should contain username")
Expand All @@ -52,6 +60,20 @@ func TestGenerateASCII(t *testing.T) {
if !strings.Contains(result, string(EmptyBlock)) {
t.Error("Generated ASCII should contain empty blocks")
}
if !strings.Contains(result, HeaderTemplate) {
t.Error("Generated ASCII should contain header")
}
}
if !tt.wantErr && !tt.includeHeader {
if strings.Contains(result, "testuser") {
t.Error("Generated ASCII should exclude username when requested")
}
if strings.Contains(result, "2023") {
t.Error("Generated ASCII should exclude year when requested")
}
if strings.Contains(result, HeaderTemplate) {
t.Error("Generated ASCII should exclude header when requested")
}
}
})
}
Expand Down
20 changes: 13 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var (
full bool
debug bool
web bool
artOnly bool
output string // new output path flag

rootCmd = &cobra.Command{
Expand Down Expand Up @@ -106,6 +107,7 @@ func init() {
rootCmd.Flags().BoolVarP(&full, "full", "f", false, "Generate contribution graph from join year to current year")
rootCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable debug logging")
rootCmd.Flags().BoolVarP(&web, "web", "w", false, "Open GitHub profile (authenticated or specified user).")
rootCmd.Flags().BoolVar(&artOnly, "art-only", false, "Generate only ASCII preview")
rootCmd.Flags().StringVarP(&output, "output", "o", "", "Output file path (optional)")
}

Expand Down Expand Up @@ -176,7 +178,7 @@ func generateSkyline(startYear, endYear int, targetUser string, full bool) error
allContributions = append(allContributions, contributions)

// Generate ASCII art for each year
asciiArt, err := ascii.GenerateASCII(contributions, targetUser, year, year == startYear)
asciiArt, err := ascii.GenerateASCII(contributions, targetUser, year, (year == startYear) && !artOnly, !artOnly)
if err != nil {
if warnErr := log.Warning("Failed to generate ASCII preview: %v", err); warnErr != nil {
return warnErr
Expand All @@ -202,14 +204,18 @@ func generateSkyline(startYear, endYear int, targetUser string, full bool) error
}
}

// Generate filename
outputPath := generateOutputFilename(targetUser, startYear, endYear)
if !artOnly {
// Generate filename
outputPath := generateOutputFilename(targetUser, startYear, endYear)

// Generate the STL file
if len(allContributions) == 1 {
return stl.GenerateSTL(allContributions[0], outputPath, targetUser, startYear)
// Generate the STL file
if len(allContributions) == 1 {
return stl.GenerateSTL(allContributions[0], outputPath, targetUser, startYear)
}
return stl.GenerateSTLRange(allContributions, outputPath, targetUser, startYear, endYear)
}
return stl.GenerateSTLRange(allContributions, outputPath, targetUser, startYear, endYear)

return nil
}

// Variable for client initialization - allows for testing
Expand Down