| 1 |
package app |
| 2 |
|
| 3 |
import ( |
| 4 |
"git.sr.ht/~rjarry/aerc/config" |
| 5 |
"git.sr.ht/~rjarry/aerc/lib/authres" |
| 6 |
"git.sr.ht/~rjarry/aerc/lib/ui" |
| 7 |
"github.com/mattn/go-runewidth" |
| 8 |
"go.rockorager.dev/vaxis" |
| 9 |
) |
| 10 |
|
| 11 |
type AuthInfo struct { |
| 12 |
authdetails *authres.Details |
| 13 |
showInfo bool |
| 14 |
uiConfig *config.UIConfig |
| 15 |
} |
| 16 |
|
| 17 |
func NewAuthInfo(auth *authres.Details, showInfo bool, uiConfig *config.UIConfig) *AuthInfo { |
| 18 |
return &AuthInfo{authdetails: auth, showInfo: showInfo, uiConfig: uiConfig} |
| 19 |
} |
| 20 |
|
| 21 |
func (a *AuthInfo) Draw(ctx *ui.Context) { |
| 22 |
defaultStyle := a.uiConfig.GetStyle(config.STYLE_DEFAULT) |
| 23 |
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', defaultStyle) |
| 24 |
var text string |
| 25 |
switch { |
| 26 |
case a.authdetails == nil: |
| 27 |
text = "(no header)" |
| 28 |
ctx.Printf(0, 0, defaultStyle, "%s", text) |
| 29 |
case a.authdetails.Err != nil: |
| 30 |
style := a.uiConfig.GetStyle(config.STYLE_ERROR) |
| 31 |
text = a.authdetails.Err.Error() |
| 32 |
ctx.Printf(0, 0, style, "%s", text) |
| 33 |
default: |
| 34 |
checkBounds := func(x int) bool { |
| 35 |
return x < ctx.Width() |
| 36 |
} |
| 37 |
setResult := func(result authres.Result) (string, vaxis.Style) { |
| 38 |
switch result { |
| 39 |
case authres.ResultNone: |
| 40 |
return "none", defaultStyle |
| 41 |
case authres.ResultNeutral: |
| 42 |
return "neutral", a.uiConfig.GetStyle(config.STYLE_WARNING) |
| 43 |
case authres.ResultPolicy: |
| 44 |
return "policy", a.uiConfig.GetStyle(config.STYLE_WARNING) |
| 45 |
case authres.ResultPass: |
| 46 |
return "✓", a.uiConfig.GetStyle(config.STYLE_SUCCESS) |
| 47 |
case authres.ResultFail: |
| 48 |
return "✗", a.uiConfig.GetStyle(config.STYLE_ERROR) |
| 49 |
default: |
| 50 |
return string(result), a.uiConfig.GetStyle(config.STYLE_ERROR) |
| 51 |
} |
| 52 |
} |
| 53 |
x := 1 |
| 54 |
for i := 0; i < len(a.authdetails.Results); i++ { |
| 55 |
if checkBounds(x) { |
| 56 |
text, style := setResult(a.authdetails.Results[i]) |
| 57 |
if i > 0 { |
| 58 |
text = " " + text |
| 59 |
} |
| 60 |
x += ctx.Printf(x, 0, style, "%s", text) |
| 61 |
} |
| 62 |
} |
| 63 |
if a.showInfo { |
| 64 |
infoText := "" |
| 65 |
for i := 0; i < len(a.authdetails.Infos); i++ { |
| 66 |
if i > 0 { |
| 67 |
infoText += "," |
| 68 |
} |
| 69 |
infoText += a.authdetails.Infos[i] |
| 70 |
if reason := a.authdetails.Reasons[i]; reason != "" { |
| 71 |
infoText += reason |
| 72 |
} |
| 73 |
} |
| 74 |
if checkBounds(x) && infoText != "" { |
| 75 |
if trunc := ctx.Width() - x - 3; trunc > 0 { |
| 76 |
text = runewidth.Truncate(infoText, trunc, "…") |
| 77 |
ctx.Printf(x, 0, defaultStyle, " (%s)", text) |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func (a *AuthInfo) Invalidate() { |
| 85 |
ui.Invalidate() |
| 86 |
} |