ctucx.git: nimgit

[nimlang] nim-wrapper for libgit2

commit e17f48f3c8eb517d74f8fe2b360d4e8646d16454
parent bb2463fe407428ae0611b36ba9f6304ae827d1dc
Author: Leah (ctucx) <leah@ctu.cx>
Date: Thu, 18 Mar 2021 01:36:46 +0100

utils.nim: add proc filemodeStr
1 file changed, 38 insertions(+), 1 deletion(-)
M
nimgit/utils.nim
|
39
++++++++++++++++++++++++++++++++++++++-
diff --git a/nimgit/utils.nim b/nimgit/utils.nim
@@ -1,4 +1,4 @@
-import times
+import times, posix, strutils
 import nimgit2
 import types
 

@@ -16,3 +16,40 @@ proc parseGitSignature* (signature: ptr git_signature): GitSignature =
     result.name  = $signature.name
     result.email = $signature.email
     result.when  = parseGitTime(signature.when)
+
+proc filemodeStr* (m: int): string =
+  var mode = align("", sizeof(m), '-')
+
+  if S_ISREG(cast[Mode](m)):
+    mode[0] = '-'
+  elif S_ISBLK(cast[Mode](m)):
+    mode[0] = 'b';
+  elif S_ISCHR(cast[Mode](m)):
+    mode[0] = 'c';
+  elif S_ISDIR(cast[Mode](m)):
+    mode[0] = 'd';
+  elif S_ISFIFO(cast[Mode](m)):
+    mode[0] = 'p';
+  elif S_ISLNK(cast[Mode](m)):
+    mode[0] = 'l';
+  elif S_ISSOCK(cast[Mode](m)):
+    mode[0] = 's';
+  else:
+    mode[0] = '?';
+
+
+  if cast[bool](m and S_IRUSR): mode[1] = 'r'
+  if cast[bool](m and S_IWUSR): mode[2] = 'w'
+  if cast[bool](m and S_IXUSR): mode[3] = 'x'
+  if cast[bool](m and S_IRGRP): mode[4] = 'r'
+  if cast[bool](m and S_IWGRP): mode[5] = 'w'
+  if cast[bool](m and S_IXGRP): mode[6] = 'x'
+  if cast[bool](m and S_IROTH): mode[7] = 'r'
+  if cast[bool](m and S_IWOTH): mode[8] = 'w'
+  if cast[bool](m and S_IXOTH): mode[9] = 'x'
+
+  if cast[bool](m and S_ISUID): mode[3] = if(mode[3] == 'x'): 's' else: 'S'
+  if cast[bool](m and S_ISGID): mode[6] = if(mode[6] == 'x'): 's' else: 'S'
+  if cast[bool](m and S_ISVTX): mode[9] = if(mode[9] == 'x'): 't' else: 'T'
+
+  result = mode