diff --git a/core_service.go b/core_service.go index 7e28920..78a4b26 100644 --- a/core_service.go +++ b/core_service.go @@ -347,6 +347,11 @@ func (s *CoreService) CurrentState() ServiceState { return s.State() } +// String returns a concise snapshot of the wrapped service state. +func (s *CoreService) String() string { + return s.State().String() +} + // AddHandler appends handlers to the wrapped service's chain. func (s *CoreService) AddHandler(handlers ...KeyHandler) { if svc := s.wrapped(); svc != nil { diff --git a/core_service_test.go b/core_service_test.go index 049929f..d929578 100644 --- a/core_service_test.go +++ b/core_service_test.go @@ -35,6 +35,7 @@ func TestCoreServiceNilSafe(t *testing.T) { assert.Equal(t, core.Result{Value: "hello", OK: false}, svc.Translate("hello")) assert.Equal(t, defaultServiceStateSnapshot(), svc.State()) assert.Equal(t, defaultServiceStateSnapshot(), svc.CurrentState()) + assert.Equal(t, defaultServiceStateSnapshot().String(), svc.String()) }) assert.Nil(t, defaultService.Load()) diff --git a/service_test.go b/service_test.go index a7ecb43..9e85c74 100644 --- a/service_test.go +++ b/service_test.go @@ -280,6 +280,9 @@ func TestServiceNilReceiverIsSafe(t *testing.T) { if got, want := svc.State(), defaultServiceStateSnapshot(); got.Language != want.Language || got.Mode != want.Mode || got.Fallback != want.Fallback || got.Formality != want.Formality || got.Location != want.Location || got.Direction != want.Direction || got.IsRTL != want.IsRTL || got.Debug != want.Debug || len(got.AvailableLanguages) != len(want.AvailableLanguages) || len(got.Handlers) != len(want.Handlers) { t.Fatalf("nil Service.State() = %+v, want %+v", got, want) } + if got, want := svc.String(), defaultServiceStateSnapshot().String(); got != want { + t.Fatalf("nil Service.String() = %q, want %q", got, want) + } if got, want := svc.T("prompt.yes"), "prompt.yes"; got != want { t.Fatalf("nil Service.T(prompt.yes) = %q, want %q", got, want) } diff --git a/state.go b/state.go index fdd060e..f9e99ac 100644 --- a/state.go +++ b/state.go @@ -160,6 +160,11 @@ func (s *Service) State() ServiceState { ) } +// String returns a concise snapshot of the service state. +func (s *Service) String() string { + return s.State().String() +} + // CurrentState is a more explicit alias for State. func (s *Service) CurrentState() ServiceState { return s.State()