Skip to content

Commit dffca73

Browse files
committed
feat: support custom text input via string or file path
1 parent 5d03863 commit dffca73

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

cmd/main.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"fmt"
5+
"os"
56
"path/filepath"
67
"strings"
78

@@ -16,6 +17,8 @@ var (
1617
themeName string // Theme name or path to custom theme file
1718
listThemes bool // List available themes and exit
1819
debugMode bool // Enable debug mode for performance analysis
20+
customText string // Custom text to display in the game
21+
filePath string // Custom text file to read from
1922
)
2023

2124
var startCmd = &cobra.Command{
@@ -62,14 +65,24 @@ var startCmd = &cobra.Command{
6265
}
6366
}
6467

68+
// check for custom text and set it
69+
if filePath != "" {
70+
data, err := os.ReadFile(filePath)
71+
if err != nil {
72+
cmd.Printf("Warning: Could not read custom text file '%s': %v\n", filePath, err)
73+
} else {
74+
customText = string(data)
75+
}
76+
}
77+
6578
// check for cursor type and set it
6679
if cursorType != "" {
6780
ui.CurrentSettings.CursorType = cursorType
6881
}
6982

7083
// apply settings and start loading
7184
ui.ApplySettings()
72-
ui.StartLoadingWithOptions(ui.CurrentSettings.CursorType)
85+
ui.StartLoadingWithOptions(ui.CurrentSettings.CursorType, customText)
7386
},
7487
}
7588

@@ -83,6 +96,10 @@ func init() {
8396
startCmd.Flags().BoolP("verbose", "v", false, "Enable verbose output")
8497
startCmd.Flags().BoolVar(&debugMode, "debug", false, "Enable debug mode for performance analysis")
8598

99+
// Custom text option
100+
startCmd.Flags().StringVarP(&customText, "text", "x", "", "Custom text to display in the game (default: random text)")
101+
startCmd.Flags().StringVarP(&filePath, "file", "f", "", "Custom text file to read from")
102+
86103
// Add command to root
87104
rootCmd.AddCommand(startCmd)
88105
}

ui/endgame.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (m *EndGameModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7171
case 0:
7272
return NewTypingModel(m.width, m.height, m.text), InitGlobalTick()
7373
case 1:
74-
StartLoadingWithOptions(CurrentSettings.CursorType)
74+
StartLoadingWithOptions(CurrentSettings.CursorType, m.text)
7575
return m, tea.Quit
7676
}
7777

ui/game.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,5 +219,5 @@ func RunTypingGame() {
219219
CurrentSettings.ThemeName, CurrentSettings.CursorType,
220220
CurrentSettings.GameMode, CurrentSettings.UseNumbers)
221221

222-
StartLoadingWithOptions(CurrentSettings.CursorType)
222+
StartLoadingWithOptions(CurrentSettings.CursorType, "")
223223
}

ui/loading.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
tea "github.com/charmbracelet/bubbletea"
1111
"github.com/charmbracelet/lipgloss"
1212
devlog "github.com/prime-run/go-typer/log"
13+
"github.com/prime-run/go-typer/utils"
1314
"github.com/spf13/cobra"
1415
)
1516

@@ -25,7 +26,7 @@ type LoadingModel struct {
2526
progressBar progress.Model
2627
}
2728

28-
func NewLoadingModel() *LoadingModel {
29+
func NewLoadingModel(customText string) *LoadingModel {
2930
p := progress.New(
3031
progress.WithDefaultGradient(),
3132
progress.WithWidth(60),
@@ -37,13 +38,14 @@ func NewLoadingModel() *LoadingModel {
3738
progress: 0.0,
3839
lastTick: time.Now(),
3940
progressBar: p,
41+
text: customText,
4042
}
4143
}
4244

4345
func (m *LoadingModel) Init() tea.Cmd {
4446
return tea.Batch(
4547
InitGlobalTick(),
46-
fetchTextCmd(),
48+
fetchTextCmd(m.text),
4749
)
4850
}
4951

@@ -96,7 +98,19 @@ func (m *LoadingModel) View() string {
9698
content)
9799
}
98100

99-
func fetchTextCmd() tea.Cmd {
101+
// fetchTextCmd fetches random text based on the current settings or uses a custom text if provided.
102+
func fetchTextCmd(customText string) tea.Cmd {
103+
// If a custom text is provided, use it directly
104+
if customText != "" {
105+
return func() tea.Msg {
106+
if len(customText) > 300 {
107+
customText = utils.FormatText(customText[:300]) // Limit to 300 characters
108+
}
109+
return textFetchedMsg(customText)
110+
}
111+
}
112+
113+
// Otherwise, fetch random text based on the current settings
100114
return func() tea.Msg {
101115
textCount := map[string]int{
102116
TextLengthShort: 1,
@@ -111,7 +125,7 @@ func fetchTextCmd() tea.Cmd {
111125

112126
estimatedTotalLen := count * 200
113127

114-
for i := 0; i < count; i++ {
128+
for range count {
115129
text := GetRandomText()
116130
texts = append(texts, text)
117131
}
@@ -131,18 +145,18 @@ func fetchTextCmd() tea.Cmd {
131145
}
132146

133147
func StartLoading(cmd *cobra.Command, args []string) {
134-
StartLoadingWithOptions("block")
148+
StartLoadingWithOptions("block", "")
135149
}
136150

137-
func StartLoadingWithOptions(cursorTypeStr string) {
151+
func StartLoadingWithOptions(cursorTypeStr string, customText string) {
138152
selectedCursorType := BlockCursor
139153
if cursorTypeStr == "underline" {
140154
selectedCursorType = UnderlineCursor
141155
}
142156

143157
DefaultCursorType = selectedCursorType
144158

145-
model := NewLoadingModel()
159+
model := NewLoadingModel(customText)
146160
p := tea.NewProgram(model, tea.WithAltScreen())
147161
if _, err := p.Run(); err != nil {
148162
fmt.Println("Oh no!", err)

ui/startscreen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ func RunStartScreen() {
773773
item := m.mainMenuItems[m.selectedItem]
774774

775775
if item.title == "Start Typing" {
776-
StartLoadingWithOptions(m.cursorType)
776+
StartLoadingWithOptions(m.cursorType, "")
777777
}
778778
}
779779
}

0 commit comments

Comments
 (0)