motd.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python3
  2. ###############################################
  3. # TODO: Argparse Support #
  4. ###############################################
  5. import datetime;
  6. import sys;
  7. import os;
  8. import re;
  9. import pwd;
  10. import subprocess;
  11. # Parse out the terminal size
  12. def terminal_size():
  13. import fcntl, termios, struct
  14. th, tw, hp, wp = struct.unpack('HHHH',
  15. fcntl.ioctl(0, termios.TIOCGWINSZ,
  16. struct.pack('HHHH', 0, 0, 0, 0)));
  17. return int(tw*0.8);
  18. time = datetime.datetime.now().time();
  19. hashes= '#' * terminal_size();
  20. blue=("0","0","255");
  21. name= "Claire";
  22. ansi_escape = re.compile(r'\x1b[^m]*m')
  23. # Definition for the hash printing functions
  24. def find_cpu():
  25. with open('/proc/cpuinfo', 'r') as inF:
  26. for line in inF:
  27. if 'model name' in line:
  28. return re.sub( '\s+', ' ', line[(line.index(": ")+2):].rstrip().replace("(R)","®").replace("(TM)","™") ).strip();
  29. break;
  30. def divideInHalf(str):
  31. return str[:round(len(str)/2)], str[round(len(str))/2:]
  32. def print_color(r,g,b):
  33. return "\x1b[38;2;"+r+";"+g+";"+b+"m";
  34. def print_initial_last():
  35. print(print_color(blue[0],blue[1],blue[2]) + hashes + u"\u001b[0m");
  36. def print_nextline(toprint):
  37. print(print_color(blue[0],blue[1],blue[2]) + '# ' + toprint + (' ' * (terminal_size() - len(ansi_escape.sub('',toprint)) -3 ) + print_color("0","0","255") + '#' + u"\u001b[0m"));
  38. def print_in_hashes(toprint):
  39. print_lines=toprint.splitlines();
  40. print_initial_last();
  41. for i in range(0, len(print_lines)):
  42. if len(ansi_escape.sub("",print_lines[i])) > (terminal_size()-3):
  43. print_nextline(print_lines[i][:len(ansi_escape.sub('',print_lines[i]))]);
  44. print_nextline(print_lines[i][len(ansi_escape.sub('',print_lines[i])):]);
  45. else:
  46. print_nextline(print_lines[i]);
  47. print_initial_last();
  48. def get_uname():
  49. return pwd.getpwuid( os.getuid() )[ 0];
  50. # Parsing out command arguments (SWITCH THIS TO ARGPARSE SOON
  51. if len(sys.argv) > 1:
  52. if "12" in sys.argv:
  53. time = time.strftime("%-I:%M %p");
  54. else:
  55. time= time.strftime("%-H:%M");
  56. shell = os.readlink('/proc/%d/exe' % os.getppid());
  57. # merge this into one statement when you get some time
  58. distro =(re.sub(r"\n"," ", (subprocess.run(["lsb_release","-sirs"], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.decode('utf-8'))));
  59. cpu = find_cpu()
  60. #This uses ANSI color codes, with u"\u001b[0m" resetting the colors.
  61. #\u001b[41m specifcies a background, and the three part one specifies a foreground.
  62. message= (u"\u001b[38;5;14m" + "Welcome To "+ name + "'s Terminal!\n" +
  63. u"\u001b[38;5;10m" + "The time is: " + u"\u001b[38;5;14m" + u"\u001b[41m" + time + u"\u001b[0m" +
  64. "\n" + print_color("51","255","249") + "Shell: " + shell + u"\u001b[0m" +
  65. "\n"+ print_color("172","229","238") + "Username: " + get_uname() + u"\u001b[0m" +
  66. "\n"+ print_color("128","255","0") + "Distro: " + distro + u"\u001b[0m" +
  67. "\n"+ print_color("51","255","249") + "Python Version: " + sys.version[0:(sys.version.index(" "))] + u"\u001b[0m" +
  68. "\n"+ print_color("0","255","0")+ "Processor: " + cpu
  69. );
  70. print_in_hashes(message);