Skip to content

Commit

Permalink
Merge pull request #1407 from fox0430/readonly-mode
Browse files Browse the repository at this point in the history
Add Readonly mode
  • Loading branch information
fox0430 authored Jul 31, 2021
2 parents a24699b + 263e368 commit 3c2750e
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 19 deletions.
24 changes: 16 additions & 8 deletions src/moe.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ proc loadPersistData(status: var EditorStatus) =
currentMainWindowNode.restoreCursorPostion(currentBufStatus,
status.lastPosition)

proc addBufferStatus(status: var EditorStatus,
parsedList: CmdParsedList) =

if parsedList.path.len > 0:
for path in parsedList.path:
if dirExists(path):
status.addNewBuffer(path, Mode.filer)
else:
status.addNewBuffer(path)
else:
status.addNewBuffer

proc initEditor(): EditorStatus =
let parsedList = parseCommandLineOption(commandLineParams())

Expand All @@ -33,14 +45,10 @@ proc initEditor(): EditorStatus =
exitUi()
quit())

if parsedList.len > 0:
for p in parsedList:
if dirExists(p.filename):
result.addNewBuffer(p.filename, Mode.filer)
else:
result.addNewBuffer(p.filename)
else:
result.addNewBuffer
if parsedList.isReadonly:
result.isReadonly = true

result.addBufferStatus(parsedList)

result.loadPersistData

Expand Down
1 change: 1 addition & 0 deletions src/moepkg/bufferstatus.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type BufferStatus* = object
mode* : Mode
prevMode* : Mode
lastSaveTime*: DateTime
isReadonly*: bool

proc initBufferStatus*(path: seq[Rune], mode: Mode): BufferStatus {.inline.} =
BufferStatus(isUpdate: true, path: path, mode: mode, lastSaveTime: now())
Expand Down
8 changes: 6 additions & 2 deletions src/moepkg/cmdlineoption.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import parseopt, pegs, os, strformat

type CmdParsedList* = seq[tuple[filename: string]]
type CmdParsedList* = object
path*: seq[string]
isReadonly*: bool

proc staticReadVersionFromNimble: string {.compileTime.} =
let peg = """@ "version" \s* "=" \s* \" {[0-9.]+} \" @ $""".peg
Expand Down Expand Up @@ -34,6 +36,7 @@ Usage:
moe [file] Edit file
Arguments:
-R Readonly mode
-h, --help Print this help
-v, --version Print version
"""
Expand All @@ -59,11 +62,12 @@ proc parseCommandLineOption*(line: seq[string]): CmdParsedList =
for kind, key, val in parsedLine.getopt():
case kind:
of cmdArgument:
result.add((filename: key))
result.path.add(key)
of cmdShortOption:
case key:
of "v": writeVersion()
of "h": writeHelp()
of "R": result.isReadonly = true
else: writeCmdLineError(kind, key)
of cmdLongOption:
case key:
Expand Down
3 changes: 3 additions & 0 deletions src/moepkg/editorstatus.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type EditorStatus* = object
autoBackupStatus*: AutoBackupStatus
isSearchHighlight*: bool
lastPosition*: seq[LastPosition]
isReadonly*: bool

proc initEditorStatus*(): EditorStatus =
result.currentDir = getCurrentDir().toRunes
Expand Down Expand Up @@ -723,6 +724,8 @@ proc addNewBuffer*(status: var EditorStatus, filename: string, mode: Mode) =

let index = status.bufStatus.high

status.bufStatus[index].isReadonly = status.isReadonly

if mode != Mode.filer:
if not fileExists(filename):
status.bufStatus[index].buffer = newFile()
Expand Down
6 changes: 6 additions & 0 deletions src/moepkg/messages.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import color, unicodeext, settings, commandline, independentutils
proc writeMessageOnCommandWindow*(commandLine: var CommandLine,
message: string,
color: EditorColorPair) {.inline.} =

commandLine.updateCommandBuffer(ru message, color)

proc writeMessageOnCommandWindow*(commandLine: var CommandLine,
message: string) {.inline.} =

commandLine.writeMessageOnCommandWindow(message, EditorColorPair.commandBar)

proc writeNoWriteError*(commandLine: var CommandLine, messageLog: var seq[seq[Rune]]) =
Expand Down Expand Up @@ -296,3 +298,7 @@ proc writeCurrentCharInfo*(commandLine: var CommandLine, r: Rune) {.inline.} =
eOct = int64(e[0]).toOct(5)
mess = fmt "<{$r}> {e[0]} Hex {normalizeHex($eHex)} Oct {$eOct}"
commandLine.writeMessageOnCommandWindow(mess)

proc writeReadonlyModeWarning*(commandLine: var CommandLine) {.inline.} =
const mess = "Warning: Readonly mode"
commandLine.writeMessageOnCommandWindow(mess, EditorColorPair.errorMessage)
Loading

0 comments on commit 3c2750e

Please sign in to comment.