Skip to content

Commit

Permalink
Added -notheme, -blockcursor and -bold.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemnos committed Jul 27, 2021
1 parent d6f2c29 commit bf56358
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ bin/
*.swp
*.swn
*.swo
tags
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.4.2:
Added -notheme, -blockcursor and -bold.

# 0.4.0:
Too numerous to list (see the man page)

Expand Down
12 changes: 12 additions & 0 deletions man.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ usage: tt \[OPTION\]... \[FILE\]

: The theme to use.

-notheme

: Attempt to use the default terminal theme. This may produce odd results depending on the theme colours.

-blockcursor

: Use the default cursor style.

-bold

: Embolden typed text.

-w

: The maximum line length in characters. This option is ignored if -raw is present.
Expand Down
41 changes: 36 additions & 5 deletions src/tt.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,20 @@ func showReport(scr tcell.Screen, cpm, wpm int, accuracy float64, attribution st
}
}

func createTyper(scr tcell.Screen, themeName string) *typer {
func createDefaultTyper(scr tcell.Screen) *typer {
return NewTyper(scr, true, tcell.ColorDefault,
tcell.ColorDefault,
tcell.ColorWhite,
tcell.ColorGreen,
tcell.ColorGreen,
tcell.ColorMaroon)
}

func createTyper(scr tcell.Screen, bold bool, themeName string) *typer {
var theme map[string]string

if b := readResource("themes", themeName); b == nil {
die("%s does not appear to be a valid theme, try '-list themes' for a list of built in themes.", themeName)
die("%s does not appear to be a valid theme, try '-list theme' for a list of built in theme.", themeName)
} else {
theme = parseConfig(b)
}
Expand All @@ -149,7 +158,7 @@ func createTyper(scr tcell.Screen, themeName string) *typer {
die("errcol is not defined and/or a valid hex colour.")
}

return NewTyper(scr, fgcol, bgcol, hicol, hicol2, hicol3, errcol)
return NewTyper(scr, bold, fgcol, bgcol, hicol, hicol2, hicol3, errcol)
}

var usage = `usage: tt [options] [file]
Expand All @@ -174,6 +183,12 @@ Aesthetics
-showwpm Display WPM whilst typing.
-theme THEMEFILE The theme to use.
-w The maximum line length in characters. This option is
-notheme Attempt to use the default terminal theme.
This may produce odd results depending
on the theme colours.
-blockcursor Use the default cursor style.
-bold Embolden typed text.
ignored if -raw is present.
Test Parameters
-t SECONDS Terminate the test after the given number of seconds.
Expand Down Expand Up @@ -226,6 +241,8 @@ func main() {
var noSkip bool
var noBackspace bool
var noReport bool
var noTheme bool
var normalCursor bool
var timeout int
var startParagraph int

Expand All @@ -237,6 +254,7 @@ func main() {
var showWpm bool
var multiMode bool
var versionFlag bool
var boldFlag bool

var err error
var testFn func() []segment
Expand All @@ -255,12 +273,15 @@ func main() {

flag.BoolVar(&showWpm, "showwpm", false, "")
flag.BoolVar(&noSkip, "noskip", false, "")
flag.BoolVar(&normalCursor, "blockcursor", false, "")
flag.BoolVar(&noBackspace, "nobackspace", false, "")
flag.BoolVar(&noTheme, "notheme", false, "")
flag.BoolVar(&oneShotMode, "oneshot", false, "")
flag.BoolVar(&noHighlight, "nohighlight", false, "")
flag.BoolVar(&noHighlightCurrent, "highlight2", false, "")
flag.BoolVar(&noHighlightNext, "highlight1", false, "")
flag.BoolVar(&noReport, "noreport", false, "")
flag.BoolVar(&boldFlag, "bold", false, "")
flag.BoolVar(&csvMode, "csv", false, "")
flag.BoolVar(&jsonMode, "json", false, "")
flag.BoolVar(&rawMode, "raw", false, "")
Expand Down Expand Up @@ -294,10 +315,14 @@ func main() {
}

if versionFlag {
fmt.Fprintf(os.Stderr, "tt version 0.4.0\n")
fmt.Fprintf(os.Stderr, "tt version 0.4.2\n")
os.Exit(1)
}

if noTheme {
os.Setenv("TCELL_TRUECOLOR", "disable")
}

reflow := func(s string) string {
sw, _ := scr.Size()

Expand Down Expand Up @@ -347,7 +372,12 @@ func main() {
}
}()

typer := createTyper(scr, themeName)
var typer *typer
if noTheme {
typer = createDefaultTyper(scr)
} else {
typer = createTyper(scr, boldFlag, themeName)
}

if noHighlightNext || noHighlight {
typer.currentWordStyle = typer.nextWordStyle
Expand All @@ -360,6 +390,7 @@ func main() {

typer.SkipWord = !noSkip
typer.DisableBackspace = noBackspace
typer.BlockCursor = normalCursor
typer.ShowWpm = showWpm

if timeout != -1 {
Expand Down
20 changes: 14 additions & 6 deletions src/typer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type typer struct {
SkipWord bool
ShowWpm bool
DisableBackspace bool
BlockCursor bool
tty io.Writer

currentWordStyle tcell.Style
Expand All @@ -47,7 +48,7 @@ type typer struct {
defaultStyle tcell.Style
}

func NewTyper(scr tcell.Screen, fgcol, bgcol, hicol, hicol2, hicol3, errcol tcell.Color) *typer {
func NewTyper(scr tcell.Screen, emboldenTypedText bool, fgcol, bgcol, hicol, hicol2, hicol3, errcol tcell.Color) *typer {
var tty io.Writer
def := tcell.StyleDefault.
Foreground(fgcol).
Expand All @@ -59,13 +60,18 @@ func NewTyper(scr tcell.Screen, fgcol, bgcol, hicol, hicol2, hicol3, errcol tcel
tty = ioutil.Discard
}

correctStyle := def.Foreground(hicol)
if emboldenTypedText {
correctStyle = correctStyle.Bold(true)
}

return &typer{
Scr: scr,
SkipWord: true,
tty: tty,

defaultStyle: def,
correctStyle: def.Foreground(hicol),
correctStyle: correctStyle,
currentWordStyle: def.Foreground(hicol2),
nextWordStyle: def.Foreground(hicol3),
incorrectStyle: def.Foreground(errcol),
Expand Down Expand Up @@ -159,11 +165,13 @@ func (t *typer) start(s string, timeLimit time.Duration, startImmediately bool,
x := (sw - nc) / 2
y := (sh - nr) / 2

t.tty.Write([]byte("\033[5 q"))
if !t.BlockCursor {
t.tty.Write([]byte("\033[5 q"))

//Assumes original cursor shape was a block (the one true cursor shape), there doesn't appear to be a
//good way to save/restore the shape if the user has changed it from the otcs.
defer t.tty.Write([]byte("\033[2 q"))
//Assumes original cursor shape was a block (the one true cursor shape), there doesn't appear to be a
//good way to save/restore the shape if the user has changed it from the otcs.
defer t.tty.Write([]byte("\033[2 q"))
}

t.Scr.SetStyle(t.defaultStyle)
idx := 0
Expand Down
Binary file modified tt.1.gz
Binary file not shown.

0 comments on commit bf56358

Please sign in to comment.