Skip to content

Commit

Permalink
Create lesskey package
Browse files Browse the repository at this point in the history
  • Loading branch information
bensadeh committed Aug 15, 2022
1 parent 7fdfb88 commit 2dd0cb9
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 62 deletions.
25 changes: 5 additions & 20 deletions bubble/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
_ "embed"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -43,9 +41,6 @@ const (
numberOfCategories = 5
)

//go:embed lesskey
var lesskey string

// Item is an item that appears in the list.
// type Item interface{}

Expand Down Expand Up @@ -504,19 +499,9 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {

commentTree := tree.Print(story, m.config, m.width, lastVisited)

tempLesskeyFile, err := os.CreateTemp("", "lesskey*")
if err != nil {
log.Fatal(err)
}

_, _ = tempLesskeyFile.WriteString(lesskey)

// lesskeyPath, _ := filepath.Abs(filepath.Dir(tempLesskeyFile.Name()))

command := cli.LessWithLesskey(commentTree, tempLesskeyFile.Name())
command := cli.Less(commentTree, m.config.LesskeyPath)

return m, tea.ExecProcess(command, func(err error) tea.Msg {
defer os.Remove(tempLesskeyFile.Name())
return message.EditorFinishedMsg{Err: err}
})

Expand All @@ -536,11 +521,11 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
}

blocks := parser.Parse(article)
header := renderer.CreateHeader(msg.Title, msg.Url, 70)
renderedArticle := renderer.ToString(blocks, 70, m.config.IndentationSymbol)
header := renderer.CreateHeader(msg.Title, msg.Url, m.config.CommentWidth)
renderedArticle := renderer.ToString(blocks, m.config.CommentWidth, m.config.IndentationSymbol)
renderedArticle = postprocessor.Process(header+renderedArticle, msg.Url)

command := cli.WrapLess(renderedArticle)
command := cli.Less(renderedArticle, m.config.LesskeyPath)

return m, tea.ExecProcess(command, func(err error) tea.Msg {
return message.EditorFinishedMsg{Err: err}
Expand Down Expand Up @@ -855,7 +840,7 @@ func (m *Model) handleBrowsing(msg tea.Msg) tea.Cmd {
func (m *Model) showHelpScreen() tea.Cmd {
helpScreen := help.GetHelpScreen(m.config.EnableNerdFonts)

command := cli.WrapLess(helpScreen)
command := cli.Less(helpScreen, m.config.LesskeyPath)

return tea.ExecProcess(command, func(err error) tea.Msg {
return message.EditorFinishedMsg{Err: err}
Expand Down
38 changes: 1 addition & 37 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,7 @@ import (
"clx/constants/unicode"
)

func Less(input string) {
command := exec.Command("less",
"--RAW-CONTROL-CHARS",
"--pattern="+unicode.ZeroWidthSpace,
"--ignore-case",
"--tilde",
"--use-color",
"-P?e"+"\u001B[48;5;232m "+"\u001B[38;5;200m"+"e"+"\u001B[38;5;214m"+"n"+"\u001B[38;5;69m"+"d "+"\033[0m",
"-DSy",
"-DP-")

command.Stdin = strings.NewReader(input)
command.Stdout = os.Stdout

if err := command.Run(); err != nil {
panic(err)
}
}

func WrapLess(input string) *exec.Cmd {
command := exec.Command("less",
"--RAW-CONTROL-CHARS",
"--pattern="+unicode.ZeroWidthSpace,
"--ignore-case",
"--tilde",
"--use-color",
"-P?e"+"\u001B[48;5;232m "+"\u001B[38;5;200m"+"E"+"\u001B[38;5;214m"+"n"+"\u001B[38;5;69m"+"d "+"\033[0m",
"-DSy",
"-DP-")

command.Stdin = strings.NewReader(input)
command.Stdout = os.Stdout

return command
}

func LessWithLesskey(input string, pathToLesskey string) *exec.Cmd {
func Less(input string, pathToLesskey string) *exec.Cmd {
command := exec.Command("less",
"--RAW-CONTROL-CHARS",
"--pattern="+unicode.ZeroWidthSpace,
Expand Down
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"clx/app"
"clx/bubble"
"clx/indent"
"clx/less"
"clx/settings"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -32,7 +33,12 @@ func Root() *cobra.Command {
config := getConfig()
config.IndentationSymbol = indent.GetIndentSymbol(hideIndentSymbol)

lesskey := less.NewLesskey()
config.LesskeyPath = lesskey.GetPath()

bubble.Run(config)

defer lesskey.Remove()
},
}

Expand Down
20 changes: 15 additions & 5 deletions cmd/view.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package cmd

import (
_ "embed"
"strconv"
"time"

"clx/less"

"clx/hn/services/hybrid"

"clx/cli"
hybrid_bubble "clx/hn/services/hybrid"
"clx/screen"
"clx/settings"
"clx/tree"

"github.com/spf13/cobra"
Expand All @@ -24,16 +27,23 @@ func viewCmd() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
id, _ := strconv.Atoi(args[0])

service := new(hybrid_bubble.Service)
service := new(hybrid.Service)

comments := service.FetchComments(id)

config := settings.New()
config := getConfig()

screenWidth := screen.GetTerminalWidth()
commentTree := tree.Print(comments, config, screenWidth, time.Now().Unix())

cli.Less(commentTree)
lesskey := less.NewLesskey()

command := cli.Less(commentTree, lesskey.GetPath())

if err := command.Run(); err != nil {
defer lesskey.Remove()
panic(err)
}
},
}
}
31 changes: 31 additions & 0 deletions less/less.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package less

import (
_ "embed"
"os"
)

//go:embed lesskey
var lesskey string

type Lesskey struct {
tempLesskeyFile *os.File
}

func NewLesskey() *Lesskey {
tempLesskeyFile, _ := os.CreateTemp("", "lesskey*")
_, _ = tempLesskeyFile.WriteString(lesskey)

key := new(Lesskey)
key.tempLesskeyFile = tempLesskeyFile

return key
}

func (key *Lesskey) GetPath() string {
return key.tempLesskeyFile.Name()
}

func (key *Lesskey) Remove() {
_ = os.Remove(key.tempLesskeyFile.Name())
}
File renamed without changes.
1 change: 1 addition & 0 deletions settings/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Config struct {
IndentationSymbol string
DebugMode bool
EnableNerdFonts bool
LesskeyPath string
}

func New() *Config {
Expand Down

0 comments on commit 2dd0cb9

Please sign in to comment.