Skip to content

Commit

Permalink
Merge pull request #33 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 3.4.0
  • Loading branch information
andyone authored Jun 9, 2022
2 parents e9d479e + 84670c8 commit f29e385
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 338 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ jobs:
fetch-depth: 2

- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v2
with:
languages: go

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v2
73 changes: 59 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
################################################################################

# This Makefile generated by GoMakeGen 1.5.1 using next command:
# This Makefile generated by GoMakeGen 2.1.0 using next command:
# gomakegen --mod .
#
# More info: https://kaos.sh/gomakegen
Expand All @@ -9,39 +9,84 @@

export GO111MODULE=on

ifdef VERBOSE ## Print verbose information (Flag)
VERBOSE_FLAG = -v
endif

MAKEDIR = $(dir $(realpath $(firstword $(MAKEFILE_LIST))))
GITREV ?= $(shell test -s $(MAKEDIR)/.git && git rev-parse --short HEAD)

################################################################################

.DEFAULT_GOAL := help
.PHONY = fmt vet deps deps-test test mod-init mod-update mod-vendor help
.PHONY = fmt vet deps update test init vendor mod-init mod-update mod-download mod-vendor help

################################################################################

deps: mod-update ## Download dependencies
init: mod-init ## Initialize new module

deps: mod-download ## Download dependencies

deps-test: deps ## Download dependencies for tests
update: mod-update ## Update dependencies to the latest versions

vendor: mod-vendor ## Make vendored copy of dependencies

test: ## Run tests
go test -covermode=count .
ifdef COVERAGE_FILE ## Save coverage data into file (String)
go test $(VERBOSE_FLAG) -covermode=count -coverprofile=$(COVERAGE_FILE) .
else
go test $(VERBOSE_FLAG) -covermode=count .
endif

mod-init: ## Initialize new module
mod-init:
ifdef MODULE_PATH ## Module path for initialization (String)
go mod init $(MODULE_PATH)
else
go mod init
go mod tidy
endif

ifdef COMPAT ## Compatible Go version (String)
go mod tidy $(VERBOSE_FLAG) -compat=$(COMPAT)
else
go mod tidy $(VERBOSE_FLAG)
endif

mod-update:
ifdef UPDATE_ALL ## Update all dependencies (Flag)
go get -u $(VERBOSE_FLAG) all
else
go get -u $(VERBOSE_FLAG) ./...
endif

ifdef COMPAT
go mod tidy $(VERBOSE_FLAG) -compat=$(COMPAT)
else
go mod tidy $(VERBOSE_FLAG)
endif

test -d vendor && go mod vendor $(VERBOSE_FLAG) || :

mod-update: ## Download modules to local cache
mod-download:
go mod download

mod-vendor: ## Make vendored copy of dependencies
go mod vendor
mod-vendor:
go mod vendor $(VERBOSE_FLAG)

fmt: ## Format source code with gofmt
find . -name "*.go" -exec gofmt -s -w {} \;

vet: ## Runs go vet over sources
vet: ## Runs 'go vet' over sources
go vet -composites=false -printfuncs=LPrintf,TLPrintf,TPrintf,log.Debug,log.Info,log.Warn,log.Error,log.Critical,log.Print ./...

help: ## Show this info
@echo -e '\n\033[1mSupported targets:\033[0m\n'
@echo -e '\n\033[1mTargets:\033[0m\n'
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
| awk 'BEGIN {FS = ":.*?## "}; {printf " \033[33m%-12s\033[0m %s\n", $$1, $$2}'
| awk 'BEGIN {FS = ":.*?## "}; {printf " \033[33m%-14s\033[0m %s\n", $$1, $$2}'
@echo -e '\n\033[1mVariables:\033[0m\n'
@grep -E '^ifdef [A-Z_]+ .*?## .*$$' $(abspath $(lastword $(MAKEFILE_LIST))) \
| sed 's/ifdef //' \
| awk 'BEGIN {FS = " .*?## "}; {printf " \033[32m%-14s\033[0m %s\n", $$1, $$2}'
@echo -e ''
@echo -e '\033[90mGenerated by GoMakeGen 1.5.1\033[0m\n'
@echo -e '\033[90mGenerated by GoMakeGen 2.1.0\033[0m\n'

################################################################################
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ module github.com/essentialkaos/go-linenoise/v3

go 1.17

require github.com/essentialkaos/check v1.2.1
require github.com/essentialkaos/check v1.3.0

require (
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.6.1 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
)
8 changes: 5 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/essentialkaos/check v1.2.1 h1:avvyFy/1acUNwfxwuOLsHeCjfXtMygtbu0lVDr3nxFs=
github.com/essentialkaos/check v1.2.1/go.mod h1:PhxzfJWlf5L/skuyhzBLIvjMB5Xu9TIyDIsqpY5MvB8=
github.com/essentialkaos/check v1.3.0 h1:ria+8o22RCLdt2D/1SHQsEH5Mmy5S+iWHaGHrrbPUc0=
github.com/essentialkaos/check v1.3.0/go.mod h1:PhxzfJWlf5L/skuyhzBLIvjMB5Xu9TIyDIsqpY5MvB8=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
45 changes: 34 additions & 11 deletions linenoise.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "linenoise.h"

#define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100
Expand All @@ -125,6 +126,7 @@ static linenoiseHintsCallback *hintsCallback = NULL;
static linenoiseFreeHintsCallback *freeHintsCallback = NULL;

static struct termios orig_termios; /* In order to restore at exit.*/
static int maskmode = 0; /* Show "***" instead of input. For passwords. */
static int rawmode = 0; /* For atexit() function to check if restore is needed*/
static int mlmode = 0; /* Multi line mode. Default is single line. */
static int atexit_registered = 0; /* Register atexit just 1 time. */
Expand Down Expand Up @@ -277,6 +279,19 @@ static size_t columnPosForMultiLine(const char *buf, size_t buf_len, size_t pos,

/* ======================= Low level terminal handling ====================== */

/* Enable "mask mode". When it is enabled, instead of the input that
* the user is typing, the terminal will just display a corresponding
* number of asterisks, like "****". This is useful for passwords and other
* secrets that should not be displayed. */
void linenoiseMaskModeEnable(void) {
maskmode = 1;
}

/* Disable mask mode. */
void linenoiseMaskModeDisable(void) {
maskmode = 0;
}

/* Set if to use or not the multi line mode. */
void linenoiseSetMultiLine(int ml) {
mlmode = ml;
Expand Down Expand Up @@ -643,7 +658,11 @@ static void refreshSingleLine(struct linenoiseState *l) {
abAppend(&ab,seq,strlen(seq));
/* Write the prompt and the current buffer content */
abAppend(&ab,l->prompt,strlen(l->prompt));
abAppend(&ab,buf,len);
if (maskmode == 1) {
while (len--) abAppend(&ab,"*",1);
} else {
abAppend(&ab,buf,len);
}
/* Show hits if any. */
refreshShowHints(&ab,l,pcollen);
/* Erase to right */
Expand Down Expand Up @@ -699,7 +718,12 @@ static void refreshMultiLine(struct linenoiseState *l) {

/* Write the prompt and the current buffer content */
abAppend(&ab,l->prompt,strlen(l->prompt));
abAppend(&ab,l->buf,l->len);
if (maskmode == 1) {
unsigned int i;
for (i = 0; i < l->len; i++) abAppend(&ab,"*",1);
} else {
abAppend(&ab,l->buf,l->len);
}

/* Show hits if any. */
refreshShowHints(&ab,l,pcollen);
Expand Down Expand Up @@ -770,7 +794,12 @@ int linenoiseEditInsert(struct linenoiseState *l, const char *cbuf, int clen) {
if ((!mlmode && promptTextColumnLen(l->prompt,l->plen)+columnPos(l->buf,l->len,l->len) < l->cols && !hintsCallback)) {
/* Avoid a full update of the line in the
* trivial case. */
if (write(l->ofd,cbuf,clen) == -1) return -1;
if (maskmode == 1) {
static const char d = '*';
if (write(l->ofd,&d,1) == -1) return -1;
} else {
if (write(l->ofd,cbuf,clen) == -1) return -1;
}
} else {
refreshLine(l);
}
Expand Down Expand Up @@ -1008,12 +1037,6 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen,
case '3': /* Delete key. */
linenoiseEditDelete(&l);
break;
case '1': /* Home */
linenoiseEditMoveHome(&l);
break;
case '4': /* End */
linenoiseEditMoveEnd(&l);
break;
}
}
} else {
Expand Down Expand Up @@ -1302,7 +1325,7 @@ int linenoiseHistorySave(const char *filename) {
fp = fopen(filename,"w");
umask(old_umask);
if (fp == NULL) return -1;
(void)chmod(filename,S_IRUSR|S_IWUSR);
chmod(filename,S_IRUSR|S_IWUSR);
for (j = 0; j < history_len; j++)
fprintf(fp,"%s\n",history[j]);
fclose(fp);
Expand Down
28 changes: 22 additions & 6 deletions linenoise.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ package linenoise
// #include "hooks.h"
import "C"

// ///////////////////////////////////////////////////////////////////////////////// //

import (
"errors"
"unsafe"
)

// ///////////////////////////////////////////////////////////////////////////////// //

// ErrKillSignal is returned returned by Line() when a user quits from prompt.
// This occurs when the user enters ctrl+C or ctrl+D.
var ErrKillSignal = errors.New("Prompt was quited with a kill signal")

// CompletionHandler provides possible completions for given input
type CompletionHandler func(input string) []string

Expand All @@ -40,6 +38,12 @@ type HintHandler func(input string) string

// ///////////////////////////////////////////////////////////////////////////////// //

// ErrKillSignal is returned returned by Line() when a user quits from prompt.
// This occurs when the user enters ctrl+C or ctrl+D.
var ErrKillSignal = errors.New("Prompt was quited with a kill signal")

// ///////////////////////////////////////////////////////////////////////////////// //

// complHandler is completion handler function
var complHandler = func(input string) []string {
return nil
Expand Down Expand Up @@ -138,14 +142,26 @@ func Clear() {

// SetMultiline sets linenoise to multiline or single line.
// In multiline mode the user input will be wrapped to a new line when the length exceeds the amount of available rows in the terminal.
func SetMultiline(ml bool) {
if ml {
func SetMultiline(enable bool) {
if enable {
C.linenoiseSetMultiLine(1)
} else {
C.linenoiseSetMultiLine(0)
}
}

// SetMaskMode sets mask mode. When it is enabled, instead of the input that the user
// is typing, the terminal will just display a corresponding number of asterisks,
// like "****". This is useful for passwords and other secrets that should not
// be displayed.
func SetMaskMode(enable bool) {
if enable {
C.linenoiseMaskModeEnable()
} else {
C.linenoiseMaskModeDisable()
}
}

// SetCompletionHandler sets the CompletionHandler to be used for completion
func SetCompletionHandler(h CompletionHandler) {
complHandler = h
Expand Down
2 changes: 2 additions & 0 deletions linenoise.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ int linenoiseHistoryLoad(const char *filename);
void linenoiseClearScreen(void);
void linenoiseSetMultiLine(int ml);
void linenoisePrintKeyCodes(void);
void linenoiseMaskModeEnable(void);
void linenoiseMaskModeDisable(void);

typedef size_t (linenoisePrevCharLen)(const char *buf, size_t buf_len, size_t pos, size_t *col_len);
typedef size_t (linenoiseNextCharLen)(const char *buf, size_t buf_len, size_t pos, size_t *col_len);
Expand Down
Loading

0 comments on commit f29e385

Please sign in to comment.