Exemple #1
0
func TestRender(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{"simple string", "asd\nsdfghi\njk", "asd\nsdfghi\njk\n"},
		{"simple utf8 string", "Hello, 世界\n\ni like chips", "Hello, 世界\n\ni like chips\n"},
		{"headings", "# Hello, 世界\n\ni like chips", "\033[44m\033[1m\033[36mHello, 世界\033[0m\n\ni like chips\n"},
		{"quotation", "Title\n> Raja", "Title\n  \033[37mRaja\033[0m\n"},
		{"inline list", "Title\n- Raja\n- Pranav", "Title\n\033[32m- Raja\033[0m\n\033[32m- Pranav\033[0m\n"},
		{"code", "Title\n`go build`\n`go test`", "Title\n  \033[40m\033[37mgo build\033[0m\n  \033[40m\033[37mgo test\033[0m\n"},
	}
	for _, test := range tests {
		rendered := tldr.Render(strings.NewReader(test.input))
		if rendered != test.expected {
			t.Errorf("Incorrect render of %s: got '%s', want '%s'", test.name, rendered, test.expected)
		}
	}
}
Exemple #2
0
func run() error {
	if len(os.Args) <= 1 {
		return errors.New("Usage: tldr <command>")
	}

	usr, err := user.Current()
	if err != nil {
		return err
	}
	if usr.HomeDir == "" {
		return errors.New("Can't load user's home folder path")
	}

	var repository entity.Repository
	repository = remote.NewRemoteRepository(remoteAddress)
	repository = cache.NewFileSystemCacheRepository(repository, path.Join(usr.HomeDir, ".tldr"), time.Hour*24)
	repository = tldr.NewIndexCheckerRepository(repository)

	cmd := os.Args[1]
	platform := runtime.GOOS
	switch platform {
	case "darwin":
		platform = "osx"
	}

	var page entity.Page
	for _, platform := range []string{platform, "common"} {
		page, err = repository.Page(cmd, platform)
		if err != nil {
			continue
		}
		defer page.Close()
		fmt.Println(tldr.Render(page.Reader()))
		return nil
	}
	return err
}