diff --git a/.gitignore b/.gitignore index 03683e8..f184910 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Binaries amfora +amfora.exe amfora-* build dist diff --git a/.travis.yml b/.travis.yml index 7935a21..6af09e9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,16 +7,24 @@ go: - "1.14" - "1.15" +os: + - linux + - osx + - windows + +before_install: + - if [ "$TRAVIS_OS_NAME" = "windows" ]; then choco install make; fi + script: - - go test ./... - - go build + - go test -race ./... + - make env: GO111MODULE=on cache: directories: - - $HOME/.cache/go-build + - $GOCACHE - $GOPATH/pkg/mod # TODO: GitHub Releases deploy diff --git a/CHANGELOG.md b/CHANGELOG.md index fd1c3ea..695431d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- `ansi` config setting, to disable ANSI colors in pages (#79, #86) + +### Changed +- Disabling the `color` config setting also disables ANSI colors in pages (#79, #86) ## [v1.5.0] - 2020-09-01 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..09c159f --- /dev/null +++ b/Makefile @@ -0,0 +1,54 @@ +GIT_EXISTS := $(shell git status > /dev/null 2>&1 ; echo $$?) + +ifeq ($(GIT_EXISTS), 0) + ifeq ($(shell git tag --points-at HEAD),) + # Not currently on a tag + VERSION := $(shell git describe --tags | sed 's/-.*/-next/') # v1.2.3-next + else + # On a tag + VERSION := $(shell git tag --points-at HEAD) + endif + + COMMIT := $(shell git rev-parse --verify HEAD) +endif + +INSTALL := install -o root -g 0 +INSTALL_DIR := /usr/local/bin +DESKTOP_DIR := /usr/share/applications + +.PHONY: all build install desktop clean uninstall fmt + +all: build + +build: +ifneq ($(GIT_EXISTS), 0) + # No Git repo + $(error No Git repo was found, which is needed to compile the commit and version) +endif + @echo "Downloading dependencies" + @go env -w GO111MODULE=on ; go mod download + @echo "Building binary" + @go env -w GO111MODULE=on CGO_ENABLED=0 ; go build -ldflags="-s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.builtBy=Makefile" + +install: + @echo "Installing Amfora to $(INSTALL_DIR)" + @$(INSTALL) -m 755 amfora $(INSTALL_DIR) + +desktop: + @echo "Setting up desktop file" + @$(INSTALL) -m 644 amfora.desktop $(DESKTOP_DIR) + @update-desktop-database $(DESKTOP_DIR) + +clean: + @echo "Removing Amfora binary in local directory" + @$(RM) amfora + +uninstall: + @echo "Removing Amfora from $(INSTALL_DIR)" + @$(RM) $(INSTALL_DIR)/amfora + @echo "Removing desktop file" + -@$(RM) $(DESKTOP_DIR)/amfora.desktop + -@update-desktop-database $(DESKTOP_DIR) + +fmt: + go fmt ./... diff --git a/README.md b/README.md index fe448f7..6ade436 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ It fully passes Sean Conman's client torture test, including the new Unicode tes ### Binary -Download a binary from the [releases](https://github.com/makeworld-the-better-one/amfora/releases) page. On Unix-based systems you might have to make the file executable with `chmod +x `. You should also move the binary to `/usr/local/bin/`. +Download a binary from the [releases](https://github.com/makeworld-the-better-one/amfora/releases) page. On Unix-based systems you might have to make the file executable with `chmod +x `. You can rename the file to just `amfora` for easy access, and move it to `/usr/local/bin/`. On Windows, make sure you click "Advanced > Run anyway" after double-clicking, or something like that. @@ -59,17 +59,20 @@ brew upgrade amfora ### From Source This section is for programmers who want to install from source. Make sure you're using Go 1.13 at least, as earlier versions will fail to build. -Install latest release: -``` -go env -w GO111MODULE=on -go get github.com/makeworld-the-better-one/amfora +The recommended way of installing Amfora fom source is using the Makefile. Note that this requires GNU Make, so use the `gmake` (not `make`) command on macOS. You may have to install it with `brew install make`. On Windows, you can install [Chocolatey](https://chocolatey.org/install) and run `choco install make`. + +Note that the Makefile currently assumes that git is available, and that it is inside a git repository. + +```shell +git clone https://github.com/makeworld-the-better-one/amfora +cd amfora +# git checkout v1.2.3 # Optionally pin to a specific version instead of the latest commit +make +sudo make install # Not for Windows! +sudo make desktop # Optional, installs .desktop file. Not for Windows or macOS ``` -Install latest commit: -``` -go env -w GO111MODULE=on -go get github.com/makeworld-the-better-one/amfora@master -``` +Because you installed with the Makefile, running `amfora -v` will tell you exactly what commit the binary was built from. ## Usage diff --git a/config/config.go b/config/config.go index 9785480..128affc 100644 --- a/config/config.go +++ b/config/config.go @@ -236,6 +236,7 @@ func Init() error { viper.SetDefault("a-general.http", "default") viper.SetDefault("a-general.search", "gus.guru/search") viper.SetDefault("a-general.color", true) + viper.SetDefault("a-general.ansi", true) viper.SetDefault("a-general.bullets", true) viper.SetDefault("a-general.left_margin", 0.15) viper.SetDefault("a-general.max_width", 100) diff --git a/config/default.go b/config/default.go index f2cbda4..11cc1ef 100644 --- a/config/default.go +++ b/config/default.go @@ -33,6 +33,9 @@ search = "gemini://gus.guru/search" # Whether colors will be used in the terminal color = true +# Whether ANSI codes from the page content should be rendered +ansi = true + # Whether to replace list asterisks with unicode bullets bullets = true diff --git a/default-config.toml b/default-config.toml index 421f3e7..4242bb1 100644 --- a/default-config.toml +++ b/default-config.toml @@ -30,6 +30,9 @@ search = "gemini://gus.guru/search" # Whether colors will be used in the terminal color = true +# Whether ANSI codes from the page content should be rendered +ansi = true + # Whether to replace list asterisks with unicode bullets bullets = true diff --git a/renderer/renderer.go b/renderer/renderer.go index 7087fc2..3b1e6eb 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -7,6 +7,7 @@ package renderer import ( "fmt" urlPkg "net/url" + "regexp" "strconv" "strings" @@ -15,12 +16,17 @@ import ( "gitlab.com/tslocum/cview" ) +// Regex for identifying ANSI color codes +var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`) + // RenderANSI renders plain text pages containing ANSI codes. // Practically, it is used for the text/x-ansi. func RenderANSI(s string, leftMargin int) string { s = cview.Escape(s) - if viper.GetBool("a-general.color") { + if viper.GetBool("a-general.color") && viper.GetBool("a-general.ansi") { s = cview.TranslateANSI(s) + } else { + s = ansiRegex.ReplaceAllString(s, "") } var shifted string lines := strings.Split(s, "\n") @@ -277,9 +283,12 @@ func convertRegularGemini(s string, numLinks, width int, proxied bool) (string, // If it's not a gemini:// page, set this to true. func RenderGemini(s string, width, leftMargin int, proxied bool) (string, []string) { s = cview.Escape(s) - if viper.GetBool("a-general.color") { + if viper.GetBool("a-general.color") && viper.GetBool("a-general.ansi") { s = cview.TranslateANSI(s) + } else { + s = ansiRegex.ReplaceAllString(s, "") } + lines := strings.Split(s, "\n") links := make([]string, 0)