feat: fossil segment

resolves #2373
This commit is contained in:
Jan De Dobbeleer 2022-06-05 16:30:39 +02:00 committed by Jan De Dobbeleer
parent a88bea85c2
commit d1501a6211
6 changed files with 218 additions and 0 deletions

View file

@ -103,6 +103,8 @@ const (
EXIT SegmentType = "exit" EXIT SegmentType = "exit"
// FLUTTER writes the flutter version // FLUTTER writes the flutter version
FLUTTER SegmentType = "flutter" FLUTTER SegmentType = "flutter"
// FOSSIL writes the fossil status
FOSSIL SegmentType = "fossil"
// GIT represents the git status and information // GIT represents the git status and information
GIT SegmentType = "git" GIT SegmentType = "git"
// GOLANG writes which go version is currently active // GOLANG writes which go version is currently active
@ -268,6 +270,7 @@ func (segment *Segment) mapSegmentWithWriter(env environment.Environment) error
EXECUTIONTIME: &segments.Executiontime{}, EXECUTIONTIME: &segments.Executiontime{},
EXIT: &segments.Exit{}, EXIT: &segments.Exit{},
FLUTTER: &segments.Flutter{}, FLUTTER: &segments.Flutter{},
FOSSIL: &segments.Fossil{},
GIT: &segments.Git{}, GIT: &segments.Git{},
GOLANG: &segments.Golang{}, GOLANG: &segments.Golang{},
HASKELL: &segments.Haskell{}, HASKELL: &segments.Haskell{},

68
src/segments/fossil.go Normal file
View file

@ -0,0 +1,68 @@
package segments
import "strings"
// FossilStatus represents part of the status of a Svn repository
type FossilStatus struct {
ScmStatus
}
func (s *FossilStatus) add(code string) {
switch code {
case "CONFLICT":
s.Conflicted++
case "DELETED":
s.Deleted++
case "ADDED":
s.Added++
case "EDITED", "UPDATED", "CHANGED":
s.Modified++
case "RENAMED":
s.Moved++
}
}
const (
FOSSILCOMMAND = "fossil"
)
type Fossil struct {
scm
Status *FossilStatus
Branch string
}
func (f *Fossil) Template() string {
return " \ufb2b {{.Branch}} {{.Status.String}} "
}
func (f *Fossil) Enabled() bool {
command := f.getCommand(FOSSILCOMMAND)
if !f.env.HasCommand(command) {
return false
}
// run fossil command
output, err := f.env.RunCommand(command, "status")
if err != nil {
return false
}
f.Status = &FossilStatus{}
lines := strings.Split(output, "\n")
for _, line := range lines {
if len(line) == 0 {
continue
}
context := strings.SplitN(line, " ", 2)
if len(context) < 2 {
continue
}
switch context[0] {
case "tags:":
f.Branch = strings.TrimSpace(context[1])
default:
f.Status.add(context[0])
}
}
return true
}

View file

@ -0,0 +1,73 @@
package segments
import (
"fmt"
"oh-my-posh/mock"
"oh-my-posh/properties"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFossilStatus(t *testing.T) {
cases := []struct {
Case string
Output string
OutputError error
HasCommand bool
ExpectedStatus string
ExpectedBranch string
ExpectedDisabled bool
}{
{
Case: "not installed",
HasCommand: false,
ExpectedDisabled: true,
},
{
Case: "command error",
HasCommand: true,
OutputError: fmt.Errorf("error"),
ExpectedDisabled: true,
},
{
Case: "default status",
HasCommand: true,
Output: `
repository: /Users/jan/Downloads/myclone.fossil
local-root: /Users/jan/Projects/fossil/
config-db: /Users/jan/.config/fossil.db
checkout: 0fabc4f3566c7e7d9e528b17253de42e14dd5c7b 2022-06-05 04:06:17 UTC
parent: e8a051e6a943a26c9c33a30df8ceda069c06c174 2022-06-04 23:09:02 UTC
tags: trunk
comment: In the /setup_skin page, add a mention of/link to /skins, per request in the forum. (user: stephan)
EDITED auto.def
EDITED configure
ADDED test.tst
`,
ExpectedBranch: "trunk",
ExpectedStatus: "+1 ~2",
},
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("GOOS").Return("unix")
env.On("IsWsl").Return(false)
env.On("HasCommand", FOSSILCOMMAND).Return(tc.HasCommand)
env.On("RunCommand", FOSSILCOMMAND, []string{"status"}).Return(strings.ReplaceAll(tc.Output, "\t", ""), tc.OutputError)
f := &Fossil{
scm: scm{
env: env,
props: properties.Map{},
},
}
got := f.Enabled()
assert.Equal(t, !tc.ExpectedDisabled, got, tc.Case)
if tc.ExpectedDisabled {
continue
}
assert.Equal(t, tc.ExpectedStatus, f.Status.String(), tc.Case)
assert.Equal(t, tc.ExpectedBranch, f.Branch, tc.Case)
}
}

View file

@ -195,6 +195,7 @@
"dart", "dart",
"exit", "exit",
"executiontime", "executiontime",
"fossil",
"git", "git",
"go", "go",
"haskell", "haskell",
@ -565,6 +566,19 @@
} }
} }
}, },
{
"if": {
"properties": {
"type": {
"const": "fossil"
}
}
},
"then": {
"title": "Fossil Segment",
"description": "https://ohmyposh.dev/docs/segments/fossil"
}
},
{ {
"if": { "if": {
"properties": { "properties": {

View file

@ -0,0 +1,59 @@
---
id: fossil
title: Fossil
sidebar_label: Fossil
---
## What
Display [fossil][fossil] information when in a fossil repository.
Local changes can also be displayed which uses the following syntax:
- `+` added
- `!` conflicted
- `-` deleted
- `~` modified
- `>` moved
## Sample Configuration
```json
{
"type": "fossil",
"style": "powerline",
"powerline_symbol": "\uE0B0",
"foreground": "#193549",
"background": "#ffeb3b"
}
```
## Template ([info][templates])
:::note default template
``` template
\ufb2b {{.Branch}} {{.Status.String}}
```
:::
### Properties
- `.Status`: `FossilStatus` - changes in the worktree (see below)
- `.Branch`: `string` - current branch
### FossilStatus
- `.Modified`: `int` - number of edited, updated and changed files
- `.Deleted`: `int` - number of deleted files
- `.Added`: `int` - number of added files
- `.Moved`: `int` - number of renamed files
- `.Conflicted`: `int` - number of conflicting files
- `.Changed`: `boolean` - if the status contains changes or not
- `.HasConflicts`: `boolean` - if the status contains conflicts or not
- `.String`: `string` - a string representation of the changes above
[fossil]: https://fossil-scm.org
[templates]: /docs/config-templates
[hyperlinks]: /docs/config-templates#helper-functions

View file

@ -67,6 +67,7 @@ module.exports = {
"segments/executiontime", "segments/executiontime",
"segments/exit", "segments/exit",
"segments/flutter", "segments/flutter",
"segments/fossil",
"segments/git", "segments/git",
"segments/poshgit", "segments/poshgit",
"segments/golang", "segments/golang",