-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #437 from noborus/save-buffer
Add save buffer
- Loading branch information
Showing
11 changed files
with
196 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package oviewer | ||
|
||
import "github.com/gdamore/tcell/v2" | ||
|
||
// setSaveBuffer is a wrapper to move to setSaveBufferMode. | ||
func (root *Root) setSaveBuffer() { | ||
if root.Doc.seekable { | ||
root.setMessage("Saving regular files is not supported") | ||
return | ||
} | ||
root.setSaveBufferMode() | ||
} | ||
|
||
// setSaveBufferMode sets the inputMode to SaveBuffer. | ||
func (root *Root) setSaveBufferMode() { | ||
input := root.input | ||
input.value = "" | ||
input.cursorX = 0 | ||
input.Event = newSaveBufferEvent(input.SaveBufferCandidate) | ||
} | ||
|
||
// saveBufferCandidate returns the candidate to set to default. | ||
func saveBufferCandidate() *candidate { | ||
return &candidate{ | ||
list: []string{}, | ||
} | ||
} | ||
|
||
// eventSaveBuffer represents the mode input mode. | ||
type eventSaveBuffer struct { | ||
tcell.EventTime | ||
clist *candidate | ||
value string | ||
} | ||
|
||
// newSaveBufferEvent returns SaveBufferModeEvent. | ||
func newSaveBufferEvent(clist *candidate) *eventSaveBuffer { | ||
return &eventSaveBuffer{clist: clist} | ||
} | ||
|
||
// Mode returns InputMode. | ||
func (e *eventSaveBuffer) Mode() InputMode { | ||
return SaveBuffer | ||
} | ||
|
||
// Prompt returns the prompt string in the input field. | ||
func (e *eventSaveBuffer) Prompt() string { | ||
return "(Save)file:" | ||
} | ||
|
||
// Confirm returns the event when the input is confirmed. | ||
func (e *eventSaveBuffer) Confirm(str string) tcell.Event { | ||
e.value = str | ||
e.SetEventNow() | ||
return e | ||
} | ||
|
||
// Up returns strings when the up key is pressed during input. | ||
func (e *eventSaveBuffer) Up(str string) string { | ||
return e.clist.up() | ||
} | ||
|
||
// Down returns strings when the down key is pressed during input. | ||
func (e *eventSaveBuffer) Down(str string) string { | ||
return e.clist.down() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package oviewer | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"github.com/gdamore/tcell/v2" | ||
) | ||
|
||
type saveSelection string | ||
|
||
const ( | ||
saveCancel saveSelection = "cancel" | ||
saveOverWrite saveSelection = "overwrite" | ||
saveAppend saveSelection = "append" | ||
) | ||
|
||
// saveBuffer saves the buffer to the specified file. | ||
func (root *Root) saveBuffer(input string) { | ||
fileName := strings.TrimSpace(input) | ||
|
||
perm := os.FileMode(0644) | ||
flag := os.O_WRONLY | os.O_CREATE | ||
_, err := os.Stat(fileName) | ||
if err == nil { | ||
root.setMessagef("overwrite? (O)overwrite, (A)Append, (N)cancel:") | ||
switch root.saveConfirm() { | ||
case saveOverWrite: | ||
flag = os.O_WRONLY | os.O_TRUNC | ||
case saveAppend: | ||
flag |= os.O_APPEND | ||
case saveCancel: | ||
root.setMessage("save cancel") | ||
return | ||
} | ||
} | ||
|
||
file, err := os.OpenFile(fileName, flag, perm) | ||
if err != nil { | ||
root.setMessageLogf("cannot save: %s:%s", fileName, err) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
if err := root.Doc.Export(file, root.Doc.BufStartNum(), root.Doc.BufEndNum()); err != nil { | ||
root.setMessageLogf("cannot save: %s:%s", fileName, err) | ||
return | ||
} | ||
|
||
root.setMessageLogf("saved %s", fileName) | ||
} | ||
|
||
// saveConfirm waits for the user to confirm the save. | ||
func (root *Root) saveConfirm() saveSelection { | ||
for { | ||
ev := root.Screen.PollEvent() | ||
switch ev := ev.(type) { | ||
case *tcell.EventKey: | ||
if ev.Key() == tcell.KeyRune { | ||
switch ev.Rune() { | ||
case 'o', 'O': | ||
return saveOverWrite | ||
case 'a', 'A': | ||
return saveAppend | ||
case 'n', 'N', 'q', 'Q': | ||
return saveCancel | ||
} | ||
} else if ev.Key() == tcell.KeyEscape { | ||
return saveCancel | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.